Numpy argsort : How to use numpy.argsort() method ?

Numpy argsort tutorial featured image

Numpy argsort uses to perform an indirect sort along the axis. It returns an array of indices sorted along the axis. In this entire tutorial, I will show you different examples for NumPy argsort method.

But before coding the demonstration part let’s learn the syntax for the numpy.argsort() method. The syntax for it is below.

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

a: The small a tells you to enter the array you want to sort.

axis: It tells you along which axis you want to sort. The default value of it is -1( last axis).

kind: The type of sorting that is quicksort, mergesort, heapsort, stable. The default sorting method is quicksort.

order: When a is an array with fields defined, this argument specifies which fields to compare first, second, etc.

Steps by Steps to do the sorting using the NumPy argsort

Step 1: Import the necessary libraries.

The First step is to install and import the necessary libraries for this tutorial. Here I am using only NumPy python module. If you have not installed it then you can install numpy in pycharm easily.

import numpy as np

Step 2: Create a Numpy array.

Let’s create a NumPy array for executing the numpy.argsort method. I will use both one-dimensional and two-dimensional array.

One Dimensional Array

array_1d = np.array([10,30,40,20])

Two Dimensional Array

array_2d = np.array([[10,30],[60,50]])

Step 3: Sort the Arrays using numpy argsort

Now the last step is to return the indices for the sorted array for both one dimensional and two-dimensional array.

Numpy argsort for One Dimensional Array.

print(np.argsort(array_1d))
Sorted Indices for One Dimensional Array
Sorted Indices for One Dimensional Array

Numpy argsort for Two Dimensional Array.

If you are using numpy.argsort() method on 2-D Numpy array then you have to pass the axis argument also. The value of it will be 0 for sorting along down way and for across set it as 1. The default value of the axis is 0.

print(np.argsort(array_2d,axis=0))
print(np.argsort(array_2d, axis=1))
Numpy argsort Indices for Two Dimensional Array
Sorted  Indices for Two Dimensional Array

The above code will print out the indices for the sorted array. In order to print out the values of it then you have use square bracket for it. Look at the below code.

Printing the Sorted 1-D array

print(array_1d[np.argsort(array_1d)])

"<yoastmark

Sorted 2-D array

To print out the values of 2-D or N-Dimensional array you have to use the numpy.unravel_index() method. It will be used for finding the indices of the sorted array. After that, you will use the square bracket to print out the values. Use the following code.

ind = np.unravel_index(np.argsort(array_2d,axis=None),array_2d.shape)
print(array_2d[ind])
Printing Sorted Array for Two Dimensional Array
Printing Sorted Array for Two Dimensional Array

That’s all for now. Hope you have enjoyed this tutorial. If you have any queries about it then you can contact us for more information.

 

Source:

Official Numpy Documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner