np.argsort: How to Use numpy argshort() in Python

0
40
np.argsort - How to Use numpy argshort() in Python

The np.argshort() is the numpy library function that returns an index of the array elements in the sorted order. The numpy argshort() function takes the original array as a parameter and returns an array. This array consists of an index of the elements corresponding to the index of the sorted array elements. This array consists of indices that would sort the array.

Syntax

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

Parameters

The np.argsort() function takes one required argument as a parameter and has three optional arguments:

  1. arr: The array is passed in this argument. This array is the argument needed for returning the array. The array to be given to the argsort function is given in this argument. 
  2. axis: The axis along which the sorting should be done is given in this argument. This axis is kept as -1 by default. We can also give None in the axis. 
  3. kind: This is the kind of sorting algorithm that is to be used by the argsort() function. This is None by default. For example, we can use quicksort, mergesort, heapsort, or stable. This algorithm passed in the kind argument is used inside the argsort function for returning the array.
  4. order: It is used as the precedence order. We can set the value as str or a list of str as ordered.

Return value

It returns an array. This array consists of the indices that would sort the array. 

Python program for returning the indices that would sort the array using np.argsort()

# Importing numpy as np
import numpy as np

# Creating a numpy array called arr
arr = np.array([5, 3, 7, 8, 1, 9, 2])
print("The array is : ", arr)

# Printing the shape of the array using shape function
print("Shape of the array is : ", arr.shape)
res = np.argsort(arr)
print("Indices that would sort the array is: ", res)

Output

The array is : [5 3 7 8 1 9 2]
Shape of the array is : (7,)
Indices that would sort the array is: [4 6 1 0 2 3 5]

In this program, we imported numpy for using the numpy functions. Then, we created a numpy array. We printed the array and then printed the shape of the array using the shape function. Then, we have passed this array into the np.argsort() function. The argshort() function returns an array. The array consists of indices that would sort the elements in the array.

The argshort() function sorts the elements in ascending order in this program. Instead of sorting the elements, the corresponding indices are sorted. These indices can be used to sort the elements.

We can see that the first element in the indices list is 4, and the 4th element in the array is 1. Hence 1 is the first element. Similarly, in the next position, the indices are 6, and the element in the 6th position is 2.

Python program for returning the indices that would sort the array with mergesort as kind and 0 as an axis in np.argsort() function

# Importing numpy as np
import numpy as np

# Creating a numpy array called arr
arr = np.array([5, 3, 7, 8, 1, 9, 2])
print("The array is : ", arr)

# Printing the shape of the array using shape function
print(" Shape of the array is : ", arr.shape)
res = np.argsort(arr, kind='mergersort', axis=0)
print("Indices that would sort the array is: ", res)
print(" The sorted array is: ", arr[res])

Output

The array is : [5 3 7 8 1 9 2]
Shape of the array is : (7,)
Indices that would sort the array is: [4 6 1 0 2 3 5]

We passed the values for kind and axis in this program as merge sort and 0. This function then sorts the array using the merge sort algorithm in the axis 0. Then we printed the indices array. Then we printed the sorted array bypassing the indices array as an index to the array.

That’s it for the numpy argshort() function tutorial.

Related posts

How to sort list alphabetically in Python

How to Sort String in Python

Python sort set

Sort dictionary by value

Python sorted()

Python list sort

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.