Table of Contents
ToggleIn NumPy, arrays can be multi-dimensional, and sorting can be performed along any of these dimensions (axes). Sorting along an axis means arranging the elements of the array in a specific order based on the values along that axis.
In NumPy, the different axes are: 0th axis (rows), 1st axis (columns), and higher axes (depth or additional dimensions).
The np.sort() function sorts the elements of an array and returns a new array containing the sorted elements. Sorting can be done along a specified axis, or if no axis is specified, the function defaults to sorting along the last axis. Following is the syntax −
numpy.sort(a, axis=-1, kind=None, order=None)
Where,
Sorting along axis 0 in NumPy refers to sorting the array elements along the vertical axis, which corresponds to rows in a 2D array.
This operation is useful when you want to order the rows of a matrix based on the values in each column.
In a 2D array −
When sorting along axis 0, NumPy sorts the elements column-wise. For each column, the values are arranged in ascending order, and the rows are reordered accordingly.
In the following example, we are using the sort() function to sort the elements in each column −
# Open Compiler
import numpy as np
# Create a 2D array
arr = np.array([[3, 6, 4], [5, 1, 2]])
# Sort along axis 0 (rows)
sorted_arr_axis0 = np.sort(arr, axis=0)
print("Original Array:\n", arr)
print("Sorted Along Axis 0:\n", sorted_arr_axis0)
Output:
The output will have the rows reordered such that the values in each column are sorted in ascending order −
Original Array:
[[3 6 4]
[5 1 2]]
Sorted Along Axis 0:
[[3 1 2]
[5 6 4]]
Sorting along axis 1 in NumPy refers to sorting the array elements along the horizontal axis, which corresponds to columns in a 2D array. This operation is useful when you want to order the elements in each row based on their values.
When sorting along axis 1, NumPy sorts the elements row-wise. For each row, the values are arranged in ascending order, and the order of columns is adjusted accordingly.
In this example, we are using the sort() function to sort the elements in each row −
# Open Compiler
import numpy as np
# Create a 2D array
arr = np.array([[3, 2, 1], [6, 5, 4]])
# Sort along axis 1 (columns)
sorted_arr_axis1 = np.sort(arr, axis=1)
print("Original Array:\n", arr)
print("Sorted Along Axis 1:\n", sorted_arr_axis1)
Output:
The output will have the columns reordered within each row such that the values are sorted in ascending order −
Original Array:
[[3 2 1]
[6 5 4]]
Sorted Along Axis 1:
[[1 2 3]
[4 5 6]]
Sorting multi-dimensional arrays in NumPy involves organizing the elements of the array along one or more specific axes.
Sorting along specific axes in a multi-dimensional array is similar to sorting in 2D arrays but extends to higher dimensions. Here is how sorting works along different axes −
In the example below, we are sorting a 3D array along different axes: depth (axis 0), rows (axis 1), and columns (axis 2). Each sorting operation arranges the elements within the specified dimension, resulting in differently ordered arrays −
# Open Compiler
import numpy as np
# Create a 3D array
arr = np.array([[[3, 2, 1], [6, 5, 4]], [[9, 8, 7], [12, 11, 10]]])
# Sort along axis 0 (depth)
sorted_arr_axis0 = np.sort(arr, axis=0)
# Sort along axis 1 (rows)
sorted_arr_axis1 = np.sort(arr, axis=1)
# Sort along axis 2 (columns)
sorted_arr_axis2 = np.sort(arr, axis=2)
print("Original Array:\n", arr)
print("Sorted Along Axis 0:\n", sorted_arr_axis0)
print("Sorted Along Axis 1:\n", sorted_arr_axis1)
print("Sorted Along Axis 2:\n", sorted_arr_axis2)
Output:
Following is the output of the above code −
Original Array:
[[[ 3 2 1]
[ 6 5 4]]
[[ 9 8 7]
[12 11 10]]]
Sorted Along Axis 0:
[[[ 3 2 1]
[ 6 5 4]]
[[ 9 8 7]
[12 11 10]]]
Sorted Along Axis 1:
[[[ 3 2 1]
[ 6 5 4]]
[[ 9 8 7]
[12 11 10]]]
Sorted Along Axis 2:
[[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]
The NumPy sort() function supports several sorting algorithms, each with its own strengths −
These algorithms can be chosen by setting the kind parameter in the np.sort() function. By default, NumPy uses the “quicksort” algorithm.
Sorting algorithms are methods for arranging elements in a specific order. Different algorithms have different performance characteristics and are optimized for various types of data and array sizes.
In this example, the array is sorted using “quicksort” and “mergesort” algorithms along axis “1”. Different sorting algorithms can affect performance and stability −
# Open Compiler
import numpy as np
# Create a 2D array
arr = np.array([[3, 2, 1], [6, 5, 4]])
# Sort using different algorithms
sorted_quicksort = np.sort(arr, axis=1, kind='quicksort')
sorted_mergesort = np.sort(arr, axis=1, kind='mergesort')
print("Sorted with Quicksort:\n", sorted_quicksort)
print("Sorted with Mergesort:\n", sorted_mergesort)
Output:
The output obtained is as shown below −
Sorted with Quicksort:
[[1 2 3]
[4 5 6]]
Sorted with Mergesort:
[[1 2 3]
[4 5 6]]
Key Takeaway: Master sorting along axes in NumPy with Vista Academy!
