np.argsort() – Python NumPy argsort() Function

np.argsort(): The argsort() function of the NumPy module returns the indices that would sort an array.

It is used to perform an indirect sort using the algorithm specified by the kind keyword along the given axis. It returns an array of indices with the same shape as array, indicating that will sort the array

Syntax:

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

Parameters

a: This is required. It is the array given as input that must be sorted.

axis: This is optional. It is the axis along which to sort. If None is specified, the array is flattened before being sorted. -1 sort along the last axis by default.

kind: This is optional. It Indicates the sorting algorithm. It can take the values ‘quicksort, “mergesort,’ ‘heapsort,’ and ‘stable.’ ‘quicksort’ is the default.

order: This is optional. It is a field-containing string or a list of strings. This parameter defines the order in which the fields must be compared when “a” is an array with fields specified.

Return Value: 

argsort(): Sorts “a” along the given axis and return an array of indices. a[index_array] returns a sorted “a” if “a” is one-dimensional. Regardless of dimensions, np.take along axis(a, index_array, axis=axis) always returns the sorted “a.”

NumPy argsort() Function in Python

Example1

Approach:

  • Import numpy module using the import keyword.
  • Pass some random list as an argument to the array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the given array as an argument to the argsort() function of numpy module to get the indices of the sorted array.
  • Store it in another variable
  • Print the indices of the sorted array.
  • Print the sorted array for the given array.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([9, 35, 4, 15, 1, 6])              
# Print the above given array.
print("The above given array is:")
print(gvn_arry)

# Pass the given array as an argument to the argsort() function of numpy module
# to get the indices of the sorted array
# Store it in another variable.
sortdarr_indx = np.argsort(gvn_arry)
# Print the indices of the sorted array
print("The indices of the sorted array:")
print(sortdarr_indx)

# Print the sorted array for the given array
print("The sorted array for the given array:")
print(gvn_arry[sortdarr_indx])

Output:

The above given array is:
[ 9 35 4 15 1 6]
The indices of the sorted array:
[4 2 5 0 3 1]
The sorted array for the given array:
[ 1 4 6 9 15 35]

Example2

Approach:

  • Import numpy module using the import keyword.
  • Pass some random list as an argument to the array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the given array, axis=0 as the arguments to the argsort() function of numpy module to get the indices of the sorted array along axis=0
  • Store it in another variable.
  • Print the sorted array for the given array along axis=0 by passing the given array, above sorted array indices, and axis=0 as the arguments to the take_along_axis() function.
  • Pass the given array, axis=1 as the arguments to the argsort() function of numpy module to get the indices of the sorted array along axis=1
  • Store it in another variable.
  • Print the sorted array for the given array along axis=1 by passing the given array, above sorted array indices, and axis=1 as the arguments to the take_along_axis() function.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([[70,20],
                [60,5]])              
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the given array, axis=0 as the arguments to the argsort() function of numpy module
# to get the indices of the sorted array along axis=0
# Store it in another variable.
sortdarr_indx0 = np.argsort(gvn_arry, axis=0)
# Print the sorted array for the given array along axis=0 by passing the given array,
# above sorted array indices and axis=0 as the arguments to the take_along_axis() function.
print("The sorted array for the given array along axis=0:")
print(np.take_along_axis(gvn_arry, sortdarr_indx0, axis=0))
# Pass the given array, axis=1 as the arguments to the argsort() function of numpy module
# to get the indices of the sorted array along axis=1
# Store it in another variable.
sortdarr_indx1 = np.argsort(gvn_arry, axis=1)
# Print the sorted array for the given array along axis=1 by passing the given array,
# above sorted array indices and axis=1 as the arguments to the take_along_axis() function.
print("The sorted array for the given array along axis=1:")
print(np.take_along_axis(gvn_arry, sortdarr_indx1, axis=1))

Output:

The above given array is:
[[70 20]
 [60 5]]
The sorted array for the given array along axis=0:
[[60 5]
 [70 20]]
The sorted array for the given array along axis=1:
[[20 70]
 [ 5 60]]

Example3

# Import numpy module using the import keyword
import numpy as np
# Pass some random list of tuples to the array() function and an 
# extra element dtype which has some conditions in it.
# Store it in a variable.
gvn_arry = np.array([(30,40),
                (70,20),
                (10,5),
                (35,65),
                (75,15),
                (80,5)], 
                dtype=[('a', '<i4'), ('b', '<i4')])

# Print the above given array.
print("The above given array is:")
print(gvn_arry)

# Get the indices of the sorted array using the argsort() function 
# by passing the second argument as order to it and print the sorted array
sortdarr_indics = np.argsort(gvn_arry, order=('a','b') )
print("The sorted array for the given array:")
print(gvn_arry[sortdarr_indics])

Output:

The above given array is:
[(30, 40) (70, 20) (10, 5) (35, 65) (75, 15) (80, 5)]
The sorted array for the given array:
[(10, 5) (30, 40) (35, 65) (70, 20) (75, 15) (80, 5)]