NumPy Sorting Along an Axis
When working with multi-dimensional arrays, sorting becomes more powerful and flexible.
Instead of sorting the entire array, NumPy allows you to sort:
- Row-wise (axis=1)
- Column-wise (axis=0)
This is known as sorting along an axis.
What is Axis in NumPy?
In NumPy:
-
axis=0→ Column-wise operation (top to bottom) -
axis=1→ Row-wise operation (left to right)
Example:
Axis 0 → vertical (columns)
Axis 1 → horizontal (rows)
Why Use Axis-Based Sorting?
- Organize structured data
- Analyze row-wise trends
- Compare column values
- Prepare datasets for ML
- Work with matrices efficiently
1. Sorting Along Axis=1 (Row-wise)
Row-wise sorting means sorting each row individually.
import numpy as np
arr = np.array([
[3, 1, 2],
[9, 5, 6]
])
print(np.sort(arr, axis=1))
Output
[[1 2 3]
[5 6 9]]
Explanation
Each row is sorted independently:
[3, 1, 2] → [1, 2, 3]
[9, 5, 6] → [5, 6, 9]
2. Sorting Along Axis=0 (Column-wise)
Column-wise sorting means sorting each column separately.
import numpy as np
arr = np.array([
[3, 9, 2],
[1, 5, 6]
])
print(np.sort(arr, axis=0))
Output
[[1 5 2]
[3 9 6]]
Explanation
Each column is sorted:
Column 1: [3, 1] → [1, 3]
Column 2: [9, 5] → [5, 9]
Column 3: [2, 6] → [2, 6]
3. Default Sorting (No Axis)
import numpy as np
arr = np.array([
[8, 3, 7],
[2, 9, 1]
])
print(np.sort(arr))
Output
[[3 7 8]
[1 2 9]]
4. Row vs Column Sorting Comparison
| Axis | Direction | Behavior |
|---|---|---|
| axis=0 | Column-wise | Sorts vertically |
| axis=1 | Row-wise | Sorts horizontally |
5. Sorting 3D Arrays
import numpy as np
arr = np.array([
[[3, 2], [1, 4]],
[[9, 6], [5, 8]]
])
print(np.sort(arr, axis=2))
Output
[[[2 3]
[1 4]]
[[6 9]
[5 8]]]
6. Practical Example: Student Marks Table
import numpy as np
marks = np.array([
[88, 72, 90],
[65, 85, 78]
])
print(np.sort(marks, axis=1))
Output
[[72 88 90]
[65 78 85]]
7. Practical Example: Sales Data by Region
import numpy as np
sales = np.array([
[300, 500, 200],
[400, 100, 600]
])
print(np.sort(sales, axis=0))
Output
[[300 100 200]
[400 500 600]]
8. Key Concept: Axis Visualization
axis=0 → ↓ ↓ ↓ (columns)
axis=1 → → → (rows)
9. Using argsort with Axis
import numpy as np
arr = np.array([
[50, 20, 40],
[10, 80, 30]
])
print(np.argsort(arr, axis=1))
Output
[[1 2 0]
[0 2 1]]
10. Why Axis Sorting Matters
- Keeps data structure intact
- Useful for matrices
- Important in ML preprocessing
- Helps in feature organization
- Used in image and signal processing
Advantages of Sorting Along Axis
- Flexible control over data
- Efficient for multi-dimensional arrays
- Improves data analysis
- Essential for scientific computing
- Easy integration with ML pipelines
Summary
Sorting along an axis in NumPy allows you to organize multi-dimensional data either row-wise or column-wise using axis=1 or axis=0. This is a powerful feature for structured data processing.
This functionality is part of NumPy and is widely used in applications built with Python.
Conclusion
Understanding axis-based sorting is essential for working with matrices and datasets. It helps you control how data is arranged and improves efficiency in data science and machine learning workflows.


0 Comments