• Post author:
  • Post category:NumPy / Python
  • Post last modified:March 27, 2024
  • Reading time:13 mins read
You are currently viewing NumPy flip() Function in Python

In NumPy, the flip() function is used to reverse the order of array elements along a specified axis. The shape of the array is preserved, but the elements are reordered. In this article, I will explain the NumPy flip() function using this how to return a reversed array with shape preserved with examples.

Advertisements

1. Quick Examples of flip() Function

If you are in a hurry, below are some quick examples of the NumPy flip() function in Python.


# Quick examples of flip() function 

# Example 1: Use numpy flip() function on 1-d array
arr = np.array([2, 4, 6, 8, 10])
arr2 = np.flip(arr)

# Example 2: Use numpy flip() function with 2-D arrays
arr = np.array([[1,2,3],[4,5,6],[7,8,9]])
arr2 = np.flip(arr)

# Example 3: Use numpy.flip() function
# To reverse the order along columns
arr2 = np.flip(arr, axis=1)

# Example 4: Use numpy.flip() function
# To reverse the order along rows 
arr2 = np.flip(arr, axis=0)

# Example 5: Use slicing with flip() 
arr2 = np.flip(arr[:,1])

2. Syntax of NumPy flip()

Following is the syntax of the numpy.flip() function.


# Syntax of flip()
numpy.flip(arr, axis=None)

2.1 Parameters of flip()

Following are the parameters of the flip() function.

  • arr – The input array that you want to flip.
  • axis (Optional) – The axis or axes along which elements should be flipped. If axis is not specified or is set to None, the array is flattened before flipping. If axis is an integer, it specifies the axis to flip. If axis is a tuple of integers, multiple axes can be flipped.

2.2 Return Value

It returns reversed array while preserving its shape.

3 Use NumPy flip() Function on 1-D Array

Let’s create a single-dimensional array using array() function and then apply the NumPy flip() function to reverse the array without changing its shape.

The below example, arr is a simple 1-D array [2, 4, 6, 8, 10]. The numpy.flip() function is then applied to reverse the order of elements in the array. The original and flipped arrays are then printed.


# Import numpy module
import numpy as np

# Create an 1D input array  
arr = np.array([2, 4, 6, 8, 10])
print("Original array:", arr)

# Use numpy flip() function on 1-d array
arr2 = np.flip(arr)
print("Flipped 1D array:", arr2)

Yields below output.

numpy flip function

4. Use NumPy flip() Function with 2-D Arrays

Let’s reverse the two-dimensional NumPy array using numpy.flip() function. This function takes a 2-D array as input and returns the array in the same shape but reverses the order of elements for each dimension.


# Create NumPy 2-D array
arr = np.array([[1,2,3],[4,5,6],[7,8,9]])

# Use numpy flip() function with 2-d arrays
arr2 = np.flip(arr)
print("Flipped 2D array:\n", arr2)

# Output:
# Flipped 2D array:
#  [[9 8 7]
#  [6 5 4]
#  [3 2 1]]

5. Use flip() with axis=1

If you use axis=1 with numpy.flip() on a 2-D array, it will reverse the order of elements along the columns. The numpy.flip() function is used with axis=1 to reverse the order along columns. The original and flipped arrays are then printed. You will get the reverse array along with column-wise.


# Use numpy.flip() function
# To reverse the order along columns
arr2 = np.flip(arr, axis=1)
print("Flipped 2-D array along columns:\n", arr2)

# Output:
# Flipped 2-D array along columns:
#  [[3 2 1]
#  [6 5 4]
#  [9 8 7]]

6. Use flip() with axis=0

If you use axis=0 with numpy.flip() on a 2-D array, it will reverse the order of elements along the rows. The numpy.flip() function is used with axis=0 to reverse the order along rows. The original and flipped arrays are then printed. You will get the reverse array along with row-wise.


# Use numpy.flip() function
# To reverse the order along rows 
arr2 = np.flip(arr, axis=0)
print("Flipped 2-D array along rows::\n", arr2)

# Output:
# Flipped 2-D array along rows::
#  [[7 8 9]
#  [4 5 6]
#  [1 2 3]]

7. Use slicing with flip()

Using the slicing method with flip() function, You can get the specified array in row-wise. For instance, [8 5 2].


# Use slicing with flip() 
arr2 = np.flip(arr[:,1])
print("Flipped and sliced 2-D array:\n",arr2)

# Output:
# Flipped and sliced 2-D array:
#  [8 5 2]

Frequently Asked Questions

What does the numpy.flip() function do?

The numpy.flip() function in NumPy is used to reverse the order of elements along a specified axis or axes in a NumPy array. It essentially flips the elements along the chosen axis without changing the shape of the array. If the axis parameter is not specified, the entire array is flattened before flipping.

Can I use numpy.flip() on a 1-D array?

You can use numpy.flip() on both 1-D and multi-dimensional arrays. For a 1-D array, the entire array is reversed.

How does numpy.flip() affect the shape of the array?

The shape of the array remains the same after applying numpy.flip(). Only the order of elements along the specified axis or axes is changed.

Is it possible to flip along multiple axes simultaneously?

You can specify a tuple of integers for the axis parameter to flip along multiple axes. For example, axis=(0, 1) will reverse the order along both rows and columns.

Can I combine slicing with numpy.flip()?

You can use slicing along with numpy.flip() to achieve more specific flipping effects. For example, you can reverse the order along a specific axis and then extract a portion of the resulting array using slicing.

Does numpy.flip() modify the original array in place?

numpy.flip() returns a new array with the flipped elements. If you want to modify the original array in place, you can use the numpy.flip method on the array itself, like arr.flip().

Conclusion

In this article, I have explained NumPy flip() function using this how to return a reversed array with shape preserved with examples.

Happy Learning!!

References