AppDividend
Latest Code Tutorials

Python: How to Reverse Array in Numpy

0

Numpy is the core library in Python for scientific computing. Numpy contains a collection of tools like arrays and techniques that can be used to solve mathematical models of problems in Science and Engineering. Numpy array is a central data structure of the numpy library. Numpy arrays are a bit like Python lists, but still very much different at the same time. If you have not installed numpy on your machine then check out how to install numpy post.

Define a Numpy array

To define an array, we can use the np.array() function.

import numpy as np

data = np.array([1, 2, 4])
print(data)

Output

[1 2 4]

To get the shape of the array, we can use np.shape property.

import numpy as np

data = np.array([1, 2, 4])
print(data.shape)

Output

(3,)

Since there is no value after a comma, this is a one-dimensional array.

Get the data type of Numpy array.

Numpy array.astype is a property that helps us to get the data type of the array.

import numpy as np

data = np.array([1, 2, 4])
print(data.astype)

Output

<built-in method astype of numpy.ndarray object at 0x101db6e40>

Creating a One-dimensional Array

Using np.arange() function, we can create one-dimensional array with filled values.

import numpy as np

data = np.arange(5)
print(data)

Output

[0 1 2 3 4]

Numpy Arrays are mutable, which means that you can modify the value of an item in the array after the array has been initialized.

import numpy as np

data = np.arange(5)
print(data)

print('After modifying the array:')
data[2] = 21
print(data)

Output

[ 0  1 21  3  4]

Numpy reverse array

Numpy flipud() method helps us to reverse the numpy array. But this works excellent, only a one-dimensional array.

import numpy as np

data = np.arange(5)
print(data)

print('After reversing the array using np.flipud(): ')
reversed_arr = np.flipud(data)
print(reversed_arr)

Output

[0 1 2 3 4]
After reversing the array using np.flipud():
[4 3 2 1 0]

There is another way you can use to reverse the numpy array.

Reverse two-dimensional numpy array

If you want to reverse the two-dimensional array from left-to-right, then you can use the np.fliplr() method.

import numpy as np

data = np.array([[1, 2], [3, 4]])
print(data)

print('After reversing the array using np.fliplr(): ')
reversed_arr = np.fliplr(data)
print(reversed_arr)

Output

[[1 2]
 [3 4]]
After reversing the array using np.fliplr():
[[2 1]
 [4 3]]

In the output, you can see that the first row is reversed, and the second row is reversed.

Reverse numpy array using arr[::-1]

When you create a reverse array using [::],  you are creating the view into an original array. You can then modify the original array, and the view will update to reflect the changes.

import numpy as np

data = np.arange(5)
print(data)

print('After reversing the array using [::-1]: ')
print(data[::-1])

Output

[0 1 2 3 4]
After reversing the array using [::-1]:
[4 3 2 1 0]

The data[::-1] just returns the reversed view. The operation is as fast as you can get and it doesn’t rely on the number of elements in the array, as it only changes the strides.

This is the repetitive operation of numpy array reversion that ate a giant chunk of the running time of your machine.

If you need an array to be contiguous (for example, because you’re performing many vector operations with it), a contiguous array is about as fast as flipup/fliplr methods.

Finally, these are the ways to reverse an array in Numpy.

See also

Numpy array to list

Numpy save array

Numpy array find an index

Numpy ndarray flat()

Numpy random rand()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.