NumPy flip() Function in Python

Spread the love

NumPy flip() function in Python is used to reverse the order of array elements along the given axis. The shape of the array is preserved, but the elements are reordered. In this article, I will explain NumPy flip() function using this how to return 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


# Below are the quick examples

# 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() with axis=1
arr2 = np.flip(arr, axis=1)

# Example 4: Use numpy flip() with axis=0
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: Input array.
  • axis: Axis or axes along which to flip over. The default, axis=None, will flip over all of the axes of the input array. If axis is negative it counts from the last to the first axis.

2.2 Return Value of the flip()

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 NumPy flip() function to reverse the array without changing its shape.


import numpy as np

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

# Use numpy flip() function on 1-d array
arr2 = np.flip(arr)
print(arr2)

# Output
# [10  8  6  4  2]

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(arr2)

# Output
# [[9 8 7]
#  [6 5 4]
#  [3 2 1]]

5. flip() with Axis=1

We can also flip() two NumPy arrays column-wise by specifying axis=1.


# Use numpy flip() with axis=1
arr2 = np.flip(arr, axis=1)
print(arr2)

# Output
# [[3 2 1]
#  [6 5 4]
#  [9 8 7]]

6. flip() with Axis=0

We can also flip() the two-dimensional arrays along with the specified axis = 0, we will get the reverse array along with row-wise.


# Use numpy flip() with axis=0
arr2 = np.flip(arr, axis=0)
print(arr2)

# Output
# [[7 8 9]
#  [4 5 6]
#  [1 2 3]]

7. Use slicing with flip()

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


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

# Output
# [8 5 2]

8. Conclusion

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

Happy Learning!!

References

Leave a Reply

You are currently viewing NumPy flip() Function in Python