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.
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. Ifaxis
is not specified or is set toNone
, the array is flattened before flipping. Ifaxis
is an integer, it specifies the axis to flip. Ifaxis
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.
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
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.
You can use numpy.flip()
on both 1-D and multi-dimensional arrays. For a 1-D array, the entire array is reversed.
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.
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.
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.
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!!
Related Articles
- How to Convert NumPy Matrix to Array
- How to Use NumPy stack() in Python
- How to use NumPy vstack() in Python
- NumPy broadcast() Function in Python
- How to Use NumPy Exponential Function
- How to get Diagonal of NumPy Array Using diag()
- NumPy Variance Function in Python
- Python NumPy floor() Function
- NumPy nanmean() – Get Mean ignoring NAN Values
- Python NumPy Reverse Array