30 Mar Sorting Numpy Arrays
The numpy.sort() function is used in Numpy to sort arrays in a sequence. This sequence can be ascending or descending. Here, we will see how to,
- Sort (Ascending order) a 1D integer array in NumPy
- Sort (Descending order) a 1D integer array in NumPy
- Sort (Ascending order) a 1D string array in NumPy
- Sort (Ascending order) a 1D string array in NumPy
- Sort a 2D array in NumPy (axis = 1)
- Sort a 2D array in NumPy (axis = 0)
- Sort a 3D array in NumPy(axis = 1)
- Sort a 3D array in NumPy(axis = 0)
- Sort a 3D array in NumPy(axis = 2)
Let us start with the examples:
Sort (Ascending order) a 1D integer array in NumPy
# Sort a 1d integer array in NumPy
# Ascending order sort (default)
import numpy as np
n = np.array([75, 89, 32, 54, 12, 8, 45, 78])
# sort
print("Sorted array = ",np.sort(n))
Output
Sorted array = [ 8 12 32 45 54 75 78 89]
Sort (Descending order) a 1D integer array in NumPy
# Sort a 1d integer array in NumPy
# Descending order sort
import numpy as np
n = np.array([75, 89, 32, 54, 12, 8, 45, 78])
# sort
print("Sorted array = ",np.sort(n)[::-1])
Output
Sorted array = [89 78 75 54 45 32 12 8]
Sort (Ascending order) a 1D string array in NumPy
# Sort a 1d string array in NumPy
# Ascending order sort (default)
import numpy as np
n = np.array(["John", "Amit", "Virat", "Rohit", "Dhoni"])
# sort
print("Sorted array = ",np.sort(n))
Output
Sorted array = ['Amit' 'Dhoni' 'John' 'Rohit' 'Virat']
Sort (Descending order) a 1D string array in NumPy
# Sort a 1d string array in NumPy
# Descending order sort
import numpy as np
n = np.array(["John", "Amit", "Virat", "Rohit", "Dhoni"])
# sort
print("Sorted array = ",np.sort(n)[::-1])
Output
Sorted array = ['Virat' 'Rohit' 'John' 'Dhoni' 'Amit']
Sort a 2D array in NumPy (axis = 1)
# Sort a 2d array in NumPy
# Sorts elements horizontally (axis = 1) along each row
import numpy as np
# Create a 2d array
n = np.array([[20, 10, 98, 7],
[42, 18, 2, 78],
[5, 38, 142, 88]
])
# sort
print("Sorted array =\n",np.sort(n, axis = 1))
Output
Sorted array = [[ 7 10 20 98] [ 2 18 42 78] [ 5 38 88 142]]
Sort a 2D array in NumPy (axis = 0)
# Sort a 2d array in NumPy
# Sorts elements vertically (axis = 0) along each row
import numpy as np
# Create a 2d array
n = np.array([[20, 10, 98, 7],
[42, 18, 2, 78],
[5, 38, 142, 88]
])
# sort
print("Sorted array =\n",np.sort(n, axis = 0))
Output
Sorted array = [[ 5 10 2 7] [ 20 18 98 78] [ 42 38 142 88]]
Sort a 3D array in NumPy(axis = 1)
# Sort a 3d array in NumPy
# axis = 1 sorts elements vertically withing each individual matrix
# individual matrix
import numpy as np
n = np.array([[
[130, 40, 99],
[4, 55, 70]],
[
[1020, 890, 990],
[100, 10, 120]]
])
# sort
print("Sorted array = ",np.sort(n, axis = 1))
Output
Sorted array = [[[ 4 40 70] [ 130 55 99]] [[ 100 10 120] [1020 890 990]]]
Sort a 3D array in NumPy(axis = 0)
# Sort a 3d array in NumPy
# axis = 0 sorts elements in the same (row,col) position across all matrices in the stack
# across all matrices
import numpy as np
n = np.array([[
[130, 40, 99],
[4, 55, 70]],
[
[1020, 890, 990],
[100, 10, 120]]
])
# sort
print("Sorted array =\n",np.sort(n, axis = 0))
Output
Sorted array = [[[ 130 40 99] [ 4 10 70]] [[1020 890 990] [ 100 55 120]]]
Sort a 3D array in NumPy(axis = 2)
# Sort a 3d array in NumPy
# axis = 2 (default) sorts elements horizontally within each individual matrix
# individual matrix
import numpy as np
n = np.array([[
[130, 40, 99],
[4, 55, 70]],
[
[1020, 890, 990],
[100, 10, 120]]
])
# sort
print("Sorted array =\n",np.sort(n, axis = 2))
Output
Sorted array = [[[ 40 99 130] [ 4 55 70]] [[ 890 990 1020] [ 10 100 120]]]
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments