Numpy Fliplr() Explained With Examples

Hello geeks and welcome in this article, we will cover NumPy fliplr. Along with that, we will also look at its syntax and parameters. To make the topic crystal clear, we will also look at a couple of examples. But at first, let us try to understand NumPy.fliplr() through its definition. Suppose you have a matrix with you. A demand arrives where you need to flip the entries in each row with the column being preserved. This function will come in handy in this situation, and you can achieve it with just a single line of code. Now moving ahead, we will look at its syntax, followed by parameters.

Syntax Of Numpy Fliplr()

Given below is the general syntax for this function

numpy.fliplr(m)

A relatively simple syntax when compared to other functions of NumPy. It has only one parameter associated with it that we will be discussing next.

Parameters Of Numpy Fliplr()

M: array_like

It represents the input array over which the operation needs to be performed. The input array must be at least 2-dimensional for this function to work. For a 1-d array, you can use another function called numpy. flipud().

Return

f:ndarray

It returns an output array with the columns reversed. Since the operation is returned the operation is O(1).

Now with the syntax and parameters being done. Let us look at couple of examples that help us in understanding the concept better.

Examples

#input
import numpy as ppool
a=[[1,23],
    [34,6]]
print(ppool.fliplr(a))

Output:

[[23  1]
 [ 6 34]]

Above, we can see the straightforward example of NumPy fliplr. In the above example, we have first imported the NumPy module. Following this, we have defined a 2-dimensional array. In the last line of our code, we have used the print statement. Our output justifies our input and our function. We get [23,1] which reverse of [1,23] and [6,34] reverse of [34,6]. Here we can see the same row elements are flipped while the shape of the array is preserved.

Now let us look at another example with a 3-dimensional matrix.

#input
import numpy as ppool
a=[[1,2,3],
   [4,5,6],
   [7,8,9]]
print(ppool.fliplr(a))

Output:

[[3 2 1]
 [6 5 4]
 [9 8 7]]

In the above example, the only difference is that we have used a 3-d array. We have first imported the NumPy module. Following which we have defined our array and then used a print statement to get our desired output. Here we can see that again, and the values are switched from left to right. At the same time, the shape of the array is still maintained. Here we can see the position of the middle element remains unchanged.

Numpy.fliplr() Vs Flipud()

In this section, we will be comparing two of the NumPy function and the basic differences between them. As stated above we know that the fliplr() function is used to flip the array from the left to the right direction. On contrary, the flipud() function is used to shift the function from up to down.

Now let us look at the effect of the function on a similar array.

fliplr() flipud()
import numpy as ppool
a=[[1,2],
[3,4]]
print(ppool.fliplr(a))
import numpy as ppool
a=[[1,2],
[3,4]]
print(ppool.flipud(a))
#ouptut
[[2 1]
[4 3]]
#ouput
[[3 4]
[1 2]]
Comparison table

From the above table, we can draw a conclusion. Also, we can spot the basic difference between the 2 functions.

Is there a Fliplr function for 3D arrays in Numpy?

No. Fliplr method only supports flipping of 2d arrays. But you can use different alternatives to flip the 3D array. Following example will help you to flip 3d arrays –

Code:

import numpy as np

a = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]])
print(a[:,::-1,...])

Output:

[[[ 3  4]
  [ 1  2]]

 [[ 7  8]
  [ 5  6]]

 [[11 12]
  [ 9 10]]]

Explanation:

This way you can flip the 2d arrays present in 3 arrays. Moreover, you can tweak with : operator to create desired results.

Must Read

Conclusion

In this article, we covered NumPy fliplr. Along with that, we looked at its syntax and parameters. For a better understanding, we looked at a couple of examples. In the end, we can conclude that NumPy.fliplr() is used to shift the array left to right with preserving the shape of the array. I hope this article can clear all of your doubts and make this topic crystal clear to you. If you still have any unsolved queries or doubts, feel free to write them below in the comment section. Done reading this, why not read NumPy vstack next.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments