Numpy argsort() Function



The numpy.argsort() function is used to return the indices that would sort an array. This function perform an indirect sort along the given axis using the algorithm specified by the kind parameter.

The numpy.argsort() function returns an array of indices of the same shape as a that index data along the given axis in sorted order. This function is particularly useful when we need the sorting order but want to rearrange the array or other data manually.

The numpy.argsort() function raises a TypeError if the input is not a valid array-like object or contains elements that cannot be compared or sorted.

Syntax

Following is the syntax of the Python numpy.argsort() function −

numpy.argsort(arr, axis=-1, kind=None, order=None)

Parameters

Following are the parameters of the Python numpy.argsort() function −

  • arr: The input array for which the indices of the sorted elements are to be computed.
  • order (optional): This specifies the field order for structured arrays. For example, sorting by multiple fields in a structured array.
  • axis (optional): The axis along which to sort −
    • -1 (default): Sorts along the last axis.
    • 0: Sorts along columns (vertically).
    • 1: Sorts along rows (horizontally).
  • kind (optional): This specifies the sorting algorithm. Options include −
    • quicksort (default): Fast but not stable.
    • mergesort: Stable and maintains the relative order of equal elements.
    • heapsort: Memory-efficient but slower than 'quicksort'.
    • stable: A stable sorting algorithm introduced in NumPy 1.15.

Return Type

This function returns an array of indices of the same shape as the input array that represents the sorted order of elements.

Example

Following is a basic example to find the indices that would sort a 1-dimensional NumPy array using the Python numpy.argsort() function −

import numpy as np
my_array = np.array([25, 5, 15, 10])
sorted_indices = np.argsort(my_array)
print("Original Array:", my_array)
print("Indices of Sorted Array:", sorted_indices)
print("Sorted Array:", my_array[sorted_indices])

Output

Original Array: [25  5 15 10]
Indices of Sorted Array: [1 3 2 0]
Sorted Array: [ 5 10 15 25]

Example : Sorting Along Axis

We can sort along a specific axis in a multi-dimensional array using the axis parameter. The following example demonstrates sorting rows (axis=1) and columns (axis=0) −

import numpy as np
my_array = np.array([[8, 2, 7], [4, 9, 1]])
row_indices = np.argsort(my_array, axis=1)
col_indices = np.argsort(my_array, axis=0)

print("Original Array:\n", my_array)
print("Indices of Row-wise Sort:\n", row_indices)
print("Indices of Column-wise Sort:\n", col_indices)

Output

Original Array:
 [[8 2 7]
  [4 9 1]]
Indices of Row-wise Sort:
 [[1 2 0]
  [2 0 1]]
Indices of Column-wise Sort:
 [[1 0 1]
  [0 1 0]]

Example : Sorting Using Custom Data Type

The order parameter is used for structured arrays. Following is an example to find indices for sorting by specific fields −

import numpy as np
my_array = np.array([(1, 'Zebra', 100), (2, 'Apple', 50), (3, 'Monkey', 75)],
                    dtype=[('id', int), ('name', 'U10'), ('score', int)])
indices_by_name = np.argsort(my_array, order='name')
print("Original Array:\n", my_array)
print("Indices of Sorting by Name:\n", indices_by_name)
print("Array Sorted by Name:\n", my_array[indices_by_name])

Output

Original Array:
 [(1, 'Zebra', 100) (2, 'Apple',  50) (3, 'Monkey',  75)]
Indices of Sorting by Name:
 [1 2 0]
Array Sorted by Name:
 [(2, 'Apple',  50) (3, 'Monkey',  75) (1, 'Zebra', 100)]
numpy_array_manipulation.htm
Advertisements