How to Use NumPy Argsort() in Python

NumPy argsort() function in Python is used to calculate an indirect sort along the specified axis using the algorithm specified by the kind keyword. It returns an index of an array of elements of the same shape as arr that would sort the array.

Note that this doesn’t sort the elements of the array instead it just returns the indices of the sorted elements.

In this article, I will explain Python NumPy argsort() function syntax and using its parameters how we can get the indices of sorted elements of an array.

1. Quick Examples of Python NumPy Argsort Function

If you are in a hurry, below are some quick examples of Python numpy.argsort() function.


# Below are the quick examples

# Example 1: Get the argsort of the 1-D array
arr1 = np.argsort(arr) 

# Example 2: Get the argsort 1-D array in descending order
arr1 = np.argsort(arr)[::-1]

# Example 3: Compute argsort of the 2-D array along axis = 0
arr1 = np.argsort(arr, axis = 0)

# Example 4: Compute argsort of the 2-D array along axis = 1
arr1 = np.argsort(arr, axis = 1)

# Example 5: Compute argsort of 2-D array along kind
arr1 = np.argsort(arr, kind ='quicksort', axis = 1)

2. Syntax of argsort()

Following is the syntax of argsort().


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

2.1 Parameters of argsort() in

Following are the parameters of argsort() function.

  • arr – Array to sort.
  • axis – Axis along which to sort. The default is -1, which sorts along the last axis. If None, the flattened array is used.
  • kind – Selection algorithm. The default is ‘quicksort’.
  • order – (str or list of str), optional: This argument specifies which fields to compare first, second, etc.

2.2 Return Value of argsort()

It returns the indices of array elements that would sort an array.

3. Usage of NumPy argsort() Function

numpy.argsort() is available in the numpy module package in Python and it returns the indices that would sort an array by performing indirect sort along the given axis using the algorithm specified by the kind keyword. In Numpy Python argsort() means to sort the elements of an array with the given axis of the same shape.

Lets create NumPy array using numpy.array() function and get the indices of the sorted elements by using the argsort() function, it will return the indices of sorted elements of the given array.


import numpy as np  
# Create NumPy 1-D array
arr = np.array([6, 8, 3, 9, 5, 7])

# Get the argsort of 1-D array
arr1 = np.argsort(arr)
print(arr1)

# Output
# [2 4 0 5 1 3]

4. Get the NumPy argsort in Descending Order

By default argsort() function sorts the NumPy array elements in ascending order and returns their corresponding indices, moreover, we can easily sort the indices of a given array in descending order. Along with the syntax [::-1]. For example,


# Get the argsort 1-D array in decending order
arr1 = np.argsort(arr)[::-1]
print(arr1)

# Output 
# [3 1 5 0 4 2]

5. Argsort of Two-Dimensional Array

We can also apply argsort() with 2-D array along with the specified axis, for that we have to pass the axis parameter along with the array. Now we can specify axis=0, then this function will return the column-wise indices of the sorted array.


# Initialize 2-D numpy array
arr = np.array([[6, 8, 3], 
               [ 9, 5, 7]])
  
# Compute argsort of 2-D array along axis = 0
arr1 = np.argsort(arr, axis = 0)
print(arr1)

# Output
# [[0 1 0]
#  [1 0 1]]

To specify axis = 1, then this function will return the row-wise indices of the sorted array.


# Compute argsort of 2-D array along axis = 1
arr1 = np.argsort(arr, axis = 1)
print (arr1)

# Output
# [[2 0 1]
# [1 2 0]]

6. Argsort of Two-Dimensional Array Along with Kind Parameter

By using kind param, we can specify what sorting algorithm to use to sort the array indirectly and get the indices of the result. The below example uses quicksort.


# Compute argsort of 2-D array along kind
arr1 = np.argsort(arr, kind ='quicksort', axis = 1)
print(arr1)

# Output
# [[2 0 1]
# [1 2 0]]

7. Conclusion

In this article, I have explained how to use Python NumPy argsort() function to return the indexes of the sorted array and also learned how to sort by descending order.

Happy Learning!!

You May Also Like

References

Leave a Reply

You are currently viewing How to Use NumPy Argsort() in Python