1.

How can you reverse a NumPy array?

Answer»

There are two ways of reversing a NumPy array.


  • Method 1: Using the slicing method: We can make use of [::-1] for reversing the array. The following example demonstrates this:
import numpy as np

# create numpy array
arr = np.array([1, 2, 4, 6])

# To reverse array
reverse_arr = arr[::-1]
print(reverse_arr)

Output:

[6 4 2 1]

  • Method 2: flipud function: This function is provided by NumPy to reverse the NumPy array. Let us see the below example about its usage.
import numpy as np

# create numpy array
arr = np.array([1, 2, 4, 5, 6])

#flipud method for reversing
reverse_arr = np.flipud(arr)
print(reverse_arr)

Output:

[6 5 4 2 1]


Discussion

No Comment Found