NumPy - Sorting Along an Axis



Sorting Along an Axis in NumPy

In 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

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,

  • a: It is the array to be sorted.
  • axis: It is the axis along which to sort. Default is -1, which means sorting along the last axis.
  • kind: It is the sorting algorithm to use. Options include 'quicksort', 'mergesort', 'heapsort', and 'stable'.
  • order: It is used when sorting a structured array to define which fields to compare.

Sorting Along Axis 0 (Rows)

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 −

  • Axis 0 corresponds to the rows.
  • Axis 1 corresponds to the columns.

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.

Example

In the following example, we are using the sort() function to sort the elements in each column −

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)

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 (Columns)

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.

Example

In this example, we are using the sort() function to sort the elements in each row −

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)

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

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 −

  • Axis 0 (First Dimension): Sorting along axis 0 affects rows.
  • Axis 1 (Second Dimension): Sorting along axis 1 affects columns.
  • Axis 2 (Third Dimension): In 3D arrays, sorting along axis 2 affects the depth slices.

Example

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 −

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)

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]]]

Sorting with Different Algorithms

The NumPy sort() function supports several sorting algorithms, each with its own strengths −

  • Quicksort
  • Mergesort
  • Heapsort
  • Stable Sort

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.

Example

In this example, the array is sorted using "quicksort" and "mergesort" algorithms along axis "1". Different sorting algorithms can affect performance and stability −

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)

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]]
Advertisements