Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Numpy fliplr() Function



The Numpy fliplr() function is used to reverse the order of elements in a 2D array (or higher-dimensional arrays with at least two dimensions) along the second axis (columns). This is particularly useful for rearranging the columns of an array for analysis or visualization.

The function operates specifically along the left-right direction (reversing columns) while leaving other axes unchanged. The input array must have at least two dimensions; otherwise, it will raise a ValueError.

Syntax

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

numpy.fliplr(m)

Parameters

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

  • m: The input array to be flipped. It must have at least two dimensions.

Return Type

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

Example

Following is a basic example of reversing the columns of a 2D array using the Numpy fliplr() 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.fliplr(my_array)
print("Array after flipping columns:\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 columns:
 [[3 2 1]
  [6 5 4]
  [9 8 7]]

Example: Passing 3D Array as an Argument

The fliplr() function can also be applied to higher-dimensional arrays, provided they have at least two dimensions. Here, we have fliped the elements of the 3D array using fliplr() function −

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

Output

Following is the output of the above code −

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

  [[ 7  8  9]
   [10 11 12]]]
3D Array after flipping columns:
 [[[ 4  5  6]
   [ 1  2  3]]

  [[10 11 12]
   [ 7  8  9]]]

Example: Single-row Array

The fliplr() function also works for single-row arrays. In the following example, we have used it on a single-row 2D array −

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

Output

Following is the output of the above code −

Original Array:
 [[10 20 30 40]]
Array after flipping columns:
 [[40 30 20 10]]

Example: Handling Errors with 'fliplr()'

The fliplr() function raises an error if used on a 1D array, as it requires at least two dimensions. Here, when we tried to use it on a 1D array resulted an error −

import numpy as np
my_array = np.array([10, 20, 30, 40])
try:
    result = np.fliplr(my_array)
except ValueError as e:
    print("Error:", e)

Output

Following is the output of the above code −

Error: Input must be >= 2-d.
numpy_array_manipulation.htm
Advertisements