NumPy Sorting Arrays

NumPy Sorting Arrays

Numpy sorting has always been a core feature of the numpy library. Sorting is a process where a particular set of data is arranged in a specific order like ascending or descending with respect to some criteria. Numpy can handle the sorting of arrays with great ease and in a very efficient manner. Numpy provides various sorting functions for different types of arrays. Here, we will be looking at the most commonly used sorting functions in numpy.

Sorting 1D arrays

Sorting one-dimensional arrays is straightforward using the numpy sort() function. The numpy sort() function returns a sorted copy of the array.

import numpy as np

a = np.array([1, 5, 2, 7, 9])
print("Original array:", a)

# Sort the array
a_sorted = np.sort(a)
print("Sorted array:", a_sorted)

Output:

Original array: [1 5 2 7 9]
Sorted array: [1 2 5 7 9]

We can also use the sort() method of ndarray to sort the same array in-place.

import numpy as np

a = np.array([1, 5, 2, 7, 9])
print("Original array:", a)

# Sort the array in-place
a.sort()
print("Sorted array:", a)

Output:

Original array: [1 5 2 7 9]
Sorted array: [1 2 5 7 9]

Let’s now look at sorting 1D arrays in descending order.

import numpy as np

a = np.array([1, 5, 2, 7, 9])
print("Original array:", a)

# Sort the array in descending order
a_sorted_desc = np.sort(a)[::-1]
print("Sorted array (descending):", a_sorted_desc)

Output:

Original array: [1 5 2 7 9]
Sorted array (descending): [9 7 5 2 1]

Sorting 2D arrays

Sorting two-dimensional arrays in numpy can be a bit tricky. We can use the axis parameter to sort arrays along a specific axis. For instance, axis=0 indicates that we want to sort the array along the rows i.e vertically and axis=1 indicates that we want to sort the array along the columns i.e horizontally.

Let’s sort a two-dimensional array along its rows, where each row represents a data point.

import numpy as np

a = np.array([[10, 20, 30], [5, 15, 25], [45, 35, 40]])
print("Original array:")
print(a)

# Sort the array along rows
a_sorted_rows = np.sort(a, axis=0)
print("Sorted array (rows):")
print(a_sorted_rows)

Output:

Original array:
[[10 20 30]
 [ 5 15 25]
 [45 35 40]]
Sorted array (rows):
[[ 5 15 25]
 [10 20 30]
 [45 35 40]]

Let’s sort the same two-dimensional array along columns i.e horizontally.

import numpy as np

a = np.array([[10, 20, 30], [5, 15, 25], [45, 35, 40]])
print("Original array:")
print(a)

# Sort the array along columns
a_sorted_cols = np.sort(a, axis=1)
print("Sorted array (columns):")
print(a_sorted_cols)

Output:

Original array:
[[10 20 30]
 [ 5 15 25]
 [45 35 40]]
Sorted array (columns):
[[10 20 30]
 [ 5 15 25]
 [35 40 45]]

Sorting complex arrays

Numpy can also sort complex arrays. When sorting complex numbers, we have to specify the sorting order criterion. We can sort complex numbers based on their real or imaginary component as well as by their absolute value. Let’s first sort a complex array based on the real components of the numbers:

import numpy as np

a = np.array([2 + 1j, 3 - 2j, 1 + 1j, 1 - 2j])
print("Original array:", a)

# Sort the array based on real components
a_sorted_real = np.sort(a, order='real')
print("Sorted array (by real component):", a_sorted_real)

# Sort the array based on imaginary components
a_sorted_imaginary = np.sort(a, order='imag')
print("Sorted array (by imaginary component):", a_sorted_imaginary)

# Sort the array based on absolute value
a_sorted_abs = np.sort(a, order='abs')
print("Sorted array (by absolute value):", a_sorted_abs)

Output:

Original array: [2.+1.j 3.-2.j 1.+1.j 1.-2.j]
Sorted array (by real component): [1.+1.j 1.-2.j 2.+1.j 3.-2.j]
Sorted array (by imaginary component): [3.-2.j 2.+1.j 1.+1.j 1.-2.j]
Sorted array (by absolute value): [1.+1.j 1.-2.j 2.+1.j 3.-2.j]

Sorting structured arrays

Structured arrays are a way to deal with complex structured data such as data with different data-types. Numpy provides various sorting functions to sort structured arrays. We can use the numpy sort() function to sort structured arrays based on their field value.

<

pre>import numpy as np

person_data = np.array([(‘John’, 22), (‘Max’, 30), (‘Anna’, 31), (‘George’, 28)],
dtype=[(‘name’, ‘<U10’), (‘age’, int)])
print(“Original array:”)
print(person_data)

Sort the array by age

person_data_sorted_age = np.sort(person_data, order=’age’)
print(“Sorted array (by age):”)
print(person_data_sorted_age)

Sort the array by name

person_data_sorted_name = np.sort(person_data, order=’name’)
print(“Sorted array (by name):”)
print(person_data_sorted_name)

Output:

Original array:
[('John', 22) ('Max', 30) ('Anna', 31) ('George', 28)]
Sorted array (by age):
[('John', 22) ('George', 28) ('Max', 30) ('Anna', 31)]
Sorted array (by name):
[('Anna', 31) ('George', 28) ('John', 22) ('Max', 30)]

Conclusion

Sorting is an important operation in data analysis and manipulation. Numpy provides various sorting functions for different types of arrays. The functions provide great flexibility for sorting complex or structured data.

References

Leave a Reply

Your email address will not be published. Required fields are marked *