NumPy Sorting Arrays
Sorting is one of the most important operations in data processing.
In NumPy, sorting helps you:
- Arrange data in order
- Find patterns easily
- Improve data analysis
- Prepare datasets for machine learning
NumPy provides fast and efficient sorting functions for arrays.
What is Array Sorting?
Array sorting means arranging elements in:
- Ascending order (small → large)
- Descending order (large → small)
Example:
Before: [5, 2, 9, 1]
After: [1, 2, 5, 9]
Why Use NumPy Sorting?
- Fast performance
- Works on large datasets
- Simple syntax
- Supports multi-dimensional arrays
- Essential for data science
1. Basic Sorting with np.sort()
import numpy as np
arr = np.array([5, 2, 9, 1, 6])
print(np.sort(arr))
Output
[1 2 5 6 9]
2. Sorting in Descending Order
NumPy does not directly support descending sort, but we can reverse it.
import numpy as np
arr = np.array([5, 2, 9, 1, 6])
print(np.sort(arr)[::-1])
Output
[9 6 5 2 1]
3. Sorting 2D Arrays
import numpy as np
arr = np.array([
[3, 1, 2],
[9, 5, 6]
])
print(np.sort(arr))
Output
[[1 2 3]
[5 6 9]]
4. Sorting by Axis
You can sort row-wise or column-wise.
Row-wise (axis=1)
np.sort(arr, axis=1)
Column-wise (axis=0)
np.sort(arr, axis=0)
5. Using argsort()
argsort() returns indices of sorted elements.
import numpy as np
arr = np.array([50, 10, 40, 20])
print(np.argsort(arr))
Output
[1 3 2 0]
Explanation
Sorted order indices:
10 → index 1
20 → index 3
40 → index 2
50 → index 0
6. Sorting Strings
import numpy as np
arr = np.array(["banana", "apple", "cherry"])
print(np.sort(arr))
Output
['apple' 'banana' 'cherry']
7. Sorting Boolean Values
arr = np.array([True, False, True, False])
print(np.sort(arr))
Output
[False False True True]
8. Sorting with Structured Data
arr = np.array([
("John", 25),
("Alice", 20),
("Bob", 30)
], dtype=[("name", "U10"), ("age", "i4")])
print(np.sort(arr, order="age"))
9. Sorting Real Numbers
import numpy as np
arr = np.array([3.5, 1.2, 9.8, 2.4])
print(np.sort(arr))
Output
[1.2 2.4 3.5 9.8]
10. Real-World Example: Student Marks
import numpy as np
marks = np.array([88, 56, 99, 72, 85])
print(np.sort(marks))
Output
[56 72 85 88 99]
11. Real-World Example: Sales Data
sales = np.array([500, 200, 900, 300])
print(np.sort(sales))
Output
[200 300 500 900]
Sorting Functions Summary
| Function | Purpose |
|---|---|
np.sort() | Sort array values |
np.argsort() | Return sorted indices |
[::-1] | Reverse order |
axis=0 | Column-wise sort |
axis=1 | Row-wise sort |
Advantages of NumPy Sorting
- Fast execution
- Works on large datasets
- Easy syntax
- Supports multi-dimensional arrays
- Useful in data analysis
Summary
NumPy sorting functions allow you to quickly arrange data in ascending or descending order. With np.sort() and np.argsort(), you can efficiently process and analyze large datasets.
This functionality is part of NumPy and is widely used in applications built with Python.
Conclusion
Sorting arrays is a fundamental skill in data science and machine learning. NumPy provides powerful and efficient tools to sort both simple and complex datasets with ease.


0 Comments