Reverse 1 dimensional numpy arrays using reverse slicing [ : :-1]
Reverse N dimensional numpy arrays row wise using “fliplr” method
Reverse N dimensional numpy array column wise using “flipud” method
# Imports
import numpy as np
# Let's create numpy arrays
nparray = np.array([1, 2, 3, 4])
nparraynd = np.array([[1, 2, 4],[0, 3, 5],[8, 9, 7]])
# Reverse 1 dim array
reversedarray = nparray[::-1]
print("Original array: \n{}\n".format(nparray))
print("Reversed 1 dim array: \n{}\n".format(reversedarray))
Original array: [1 2 3 4] Reversed 1 dim array: [4 3 2 1]
# Reverse N dim array row wise
rowrevndarray = np.fliplr(nparraynd)
print("Original array: \n{}\n".format(nparraynd))
print("N dim array row reversed: \n{}\n".format(rowrevndarray))
Original array: [[1 2 4] [0 3 5] [8 9 7]] N dim array row reversed: [[4 2 1] [5 3 0] [7 9 8]]
# Reverse N dim array column wise
colrevndarray = np.flipud(nparraynd)
print("Original array: \n{}\n".format(nparraynd))
print("N dim array column reversed: \n{}\n".format(colrevndarray))
Original array: [[1 2 4] [0 3 5] [8 9 7]] N dim array column reversed: [[8 9 7] [0 3 5] [1 2 4]]
# Reverse N dim array element wise
elerevndarray = np.fliplr(nparraynd)[::-1]
print("Original array: \n{}\n".format(nparraynd))
print("N dim array element wise reversed: \n{}\n".format(elerevndarray))
Original array: [[1 2 4] [0 3 5] [8 9 7]] N dim array element wise reversed: [[7 9 8] [5 3 0] [4 2 1]]
Related Resources:
- Concatenate Numpy arrays | Join Numpy arrays Concatenate Numpy arrays Learn how to concatenate numpy arrays in various ways. “concatenate” method to join Numpy arrays “hstack” to...
- Get value from index in Numpy array | Numpy tutorial Value from Index in Numpy Get value from index in numpy array using python like slicing. In below examples we...
- Reshape Numpy array | Numpy Tutorial Reshape Numpy Array Reshape numpy array with “reshape” method of numpy library. Reshaping numpy array is useful to convert array...
- Read csv file to numpy array Read csv file to numpy array Use “savetxt” method of numpy to save a numpy array to csv file Use...
- Get Numpy array dimensions Find Numpy array dimensions Shape attribute of numpy can be used to find dimensions of numpy array easily. # Imports...
- Convert an Array to one dimensional vector Convert an Array to One dimensional Vector | Flatten Convert an Array to One dimensional Vector with flatten method of...
- Create Vector in Python | Numpy Tutorial Create a Vector Create a Vector in Python using numpy array objects and reshape method. Vectors are backbone of math...
- Save Numpy array to csv file Save Numpy array to csv file Most machine learning engineers and Data Scientists use Numpy array because it’s efficient and...
- Numpy Arange Create an Array | Numpy tutorial Numpy Arange Use numpy arange method to create array with sequence of numbers. In the below example we create an...
- Create array with Random numbers | Numpy tutorial Create array with Random Numbers Create array with Random Numbers with random module of Numpy library. In this Numpy tutorial...