Numpy is a mathematical library used in Python to compute array and matrix operations. It provides functions and properties that can help perform operations like the dot product, power, exponent, etc. In this tutorial, we will see how to flipup the array using the flipud() function.
np.flipud
The np.flipud() is a numpy library function used to flip a given array in the up/down direction. The flip() function flips the entries in each column in the up or down direction. The rows are preserved but appear in a different order than before. The shape of the array is preserved.
Flip array(entries in each column) in the up-down direction, shape preserved. Numpy.flipud() function is used to flip ndarray vertically. The ud means Up / Down. The np.flipud() returns a view. Because a view shares memory with the original array, changing one value changes the other.
Syntax
numpy.flipud(array)
Parameters
It takes one required parameter, which is the input array.
Return Value
It returns the array the same array as flipped in the up-down direction.
How to flip array vertically in Python
See the following code.
import numpy as np arr = np.arange(4).reshape((2, 2)) print("Original array : \n", arr) print("\nFlipped array up-down: \n", np.flipud(arr))
Output
Original array : [[0 1] [2 3]] Flipped array up-down: [[2 3] [0 1]]
In the above example, we can see that by taking a 2×2 array and using flipud(), we flip the array elements up and down that is column-wise.
Example 2: Write a program to take a 3×3 array and flipud() and flip the elements.
See the following code.
import numpy as np arr = np.arange(9).reshape((3, 3)) print("Original array : \n", arr) print("\nFlipped array up-down : \n", np.flipud(arr))
Output
Original array : [[0 1 2] [3 4 5] [6 7 8]] Flipped array up-down : [[6 7 8] [3 4 5] [0 1 2]]
In this example, we can see that by taking a 3×3 array and using flipud(), we are getting a flipped 3×3 array.
That’s it for this tutorial.