Numpy flipud() Function



The Numpy flipud() function is used to reverse the order of elements in an array along the axis 0 (up/down). For a 2-D array, this flips the entries in each column in the up/down direction. Rows are preserved, but appear in a different order than before. This function is particularly useful for flipping the rows of an array for data manipulation or visualization purposes.

The function operates specifically along the up-down direction (reversing rows) while leaving other axes unchanged. The input array must have at least one dimension.

Syntax

Following is the syntax of the Numpy flipud() function −

numpy.flipud(m)  

Parameters

Following are the parameters of the Numpy flipud() function −

  • m: The input array to be flipped. It must have at least one dimension.

Return Type

This function returns a view of the input array with its rows reversed. The original array remains unchanged.

Example

Following is a basic example of reversing the rows of a 2D array using the Numpy flipud() function −

import numpy as np  
my_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])  
print("Original Array:\n", my_array)  
result = np.flipud(my_array)  
print("Array after flipping rows:\n", result)  

Output

Following is the output of the above code −

Original Array:  
 [[1 2 3]  
  [4 5 6]  
  [7 8 9]]  
Array after flipping rows:  
 [[7 8 9]  
  [4 5 6]  
  [1 2 3]]  

Example: Flipping a 1D Array

The flipud() function also works for 1D arrays, effectively reversing their elements. Here, we have flipped 1D array −

import numpy as np  
my_array = np.array([10, 20, 30, 40])  
print("Original Array:", my_array)  
result = np.flipud(my_array)  
print("Reversed Array:", result)  

Output

Following is the output of the above code −

Original Array: [10 20 30 40]  
Reversed Array: [40 30 20 10]  

Example: Flipping a 3D Array

The flipud() function can also be applied to multi-dimensional arrays. Here, we have flipped the rows of a 3D array −

import numpy as np  
my_array = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])  
print("Original Array:\n", my_array)  
result = np.flipud(my_array)  
print("3D Array after flipping rows:\n", result)  

Output

Following is the output of the above code −

Original Array:  
 [[[1 2]  
   [3 4]]  

  [[5 6]  
   [7 8]]]  
3D Array after flipping rows:  
 [[[5 6]  
   [7 8]]  

  [[1 2]  
   [3 4]]]  

Example: Single-row Array

The flipud() function works for single-row arrays, though the operation does not alter the array −

import numpy as np  
my_array = np.array([[10, 20, 30, 40]])  
print("Original Array:\n", my_array)  
result = np.flipud(my_array)  
print("Array after flipping rows:\n", result)  

Output

Following is the output of the above code −

Original Array:  
 [[10 20 30 40]]  
Array after flipping rows:  
 [[10 20 30 40]]  
numpy_array_manipulation.htm
Advertisements
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy