Home » Numpy Argsort with Example

Numpy Argsort with Example

Numpy Argsort with Example

What is Numpy Argsort?

Numpy Argsort, sounds a bit complicated, right? And neural network students made the function a lot more complicated than it should be. But they shouldn’t have.

Simply NumPy argsort is a numpy sorting function. But there’s more to the story.

When you run numpy.argsort() on your NumPy array it returns indices or indexes of the elements after sorting them for you. You simply get a sorted array with the indices of the sorted elements in it.

The default nature of numpy argsort function

So, now we know that we can use the function for sorting arrays, we should also know that it is indirect in nature. And it specifies one axis at a time. If the axis is not specified, the code executes with a set of default algorithms.

To support the intended outcome of numpy argsort function, the resulting array maintains the exact shape of the original array, preserving the data along the sorted axis. We must use the axis parameter in conjunction with this technique. If there’s no specified axis is as a parameter, the default axis, which is the last axis is taken.

Numpy Argsort Parameters

Let us take a look at the parameters of the Numpy Argsort function now. In other words, here are the few tweaks we can do before sorting our NumPy arrays and returning their indices.

Below is the full function example with all the parameters in it. So you can have a basic idea of numpy argsort functions.

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

Here, numpy argsort is responsible for an indirect sort. The sort is done along the given axis. As mentioned earlier, the sorting is done along the default axis, also known as the last axis. The sorting is done using the algorithm indicated by the kind keyword. If not specified, kind algorithm is set to quicksort by default.

Above example Numpy Argsort function returns a sorted array of indices. The resulting sorted array has the same shape as “a” which index data along the last axis.

Quick introduction to each Numpy Argsort parameter

a”: The array to be sorted

This is where you put your array name to be sorted.

axis” parameter: int or None (optional)

The axis of the array along which to run the sorting operation. The default value for the axis is -1, which is also known as the last axis. If specified as None, the flattened array will be used.

A NumPy array can be sorted from a single column or row into multiple columns or rows using the args.sort () function. In 2D NumPy arrays, axis 0 is the downward direction in the rows, and axis 1 is the upward direction in the columns.

kind” parameter: {‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’}, optional

This is where you specify your sorting algorithm. I assume that you are familiar with the basic sorting methods that come under data structures and algorithms. Just like in numpy sort function here also the default sorting method is set to ‘quicksort’.

order” parameter: str or list of str, optional

When you have an array with fields defined, “order” argument tells which fields to compare first, second, and third, etc.

Let’s understand the Numpy Argsort function using a simple Real-World example

Importing Numpy

Before doing anything, we have to import Numpy library into our python code.

to do that I’m going to import Numpy as “np” into my PyCharm project. You can use any kind of python IDE of your choice.

import numpy as np

Creating a Numpy Array for the example

Then I’m going to create an array called “Score_arr” to store seven different imaginary scores in it. See the code snippet below.

Score_arr = np.array([500, 600, 350, 450, 560, 750, 650])

Running Numpy Argsort on our Numpy array

Let’s run Numpy Argsort on this “Score_arr” array. I want to sort the values in ascending order. And return their indices in my resulting array called “Sorted_Score_arr“. To do these two steps, all we need is the following piece of python code. And yet this is another example of how easy, flexible, and powerful python is compared to other programming language syntaxes.

Sorted_Score_arr = np.argsort(Score_arr)

We don’t want to do anything fancy. We just want to test out the Numpy Argsort function and understand it’s a basic operation. Therefore, we can go ahead and print the output.

Following is the full code. Take a look at it.

import numpy as np

Score_arr = np.array([500, 600, 350, 450, 560, 750, 650])

Sorted_Score_arr = np.argsort(Score_arr)

print(Sorted_Score_arr)

Printing the out put of Numpy Argsort

Here is the output I got, shown below.

Numpy Argsort Output in PyCharm Terminal
Numpy Argsort Output in PyCharm Terminal

Going more deep into the real-world example with Numpy Argsort

Now that we have written and tested the function by ourselves, I think now it’s time a dive a bit deep into it. And maybe to explore its real-world implications.

In most machine-learning projects the difficult part is to prepare the data to suit the scenario presented before we even write any machine learning code.

Independant NumPy Arrays and sorting them for real world situations

Often we have to work with independent NumPy arrays as data. But in the real world, often these arrays of data are interconnected and have direct relationships. So we need to keep the relationship as it is. Otherwise, the results would be non-sense.

The scores don’t appear by themselves. People collect scores. Right? Let’s assume that our previous example scores belong to some players. Let’s create another array called “Player_arr” with player names to represent this.

In other words, these are the players who scored in the first array, “Score_arr“.

Score_arr = np.array([500, 600, 350, 450, 560, 750, 650])
Player_arr = np.array(["Michael", "Thompson","Drake", "Luke", "Ben", "Nate", "Duke"])

So Michael’s got 500 points, Thompson’s got 600, ………….. Duke’s got 650. You get the idea!

Sorting the player names by their corrosponding scores in acesinding order

We can get the sorted player names by sorting their corresponding scores. And by representing the indices of the score elements by their corresponding player names.

Sounds a bit complicated, right? But see how this complicated thing is well-represented in a simple piece of python code. Read it from start to end, and then end to start. Several times if you need. You will understand what I just said.

Sorted_Playersbyscore_arr = Player_arr[np.argsort(Score_arr)]

And now we can print and investigate.

print(Sorted_Playersbyscore_arr)

The full python code

import numpy as np

Score_arr = np.array([500, 600, 350, 450, 560, 750, 650])
Player_arr = np.array(["Michael", "Thomspson","Drake", "Luke", "Ben", "Nate", "Duke"])

Sorted_Playersbyscore_arr = Player_arr[np.argsort(Score_arr)]
print(Sorted_Playersbyscore_arr)
Numpy Argsort Different Output in PyCharm Terminal
Sorted Player Names By Their Scores

We can see the player names according to their scores in ascending order. Drake got the lowest score which is 350 and the Nate got the maximum score which is 750. If you have any doubt go up and see the players and their scores.

This is only possible because we are running Numpy Argsort function correctly on the “Score_arr” array. It sorts the scores and returns their corresponding indices.

Then we use these indexes to display the player names by choosing the elements in ” Player_arr“. We are doing this by requesting the elements in that array corresponding to these specific indexes.

I hope you understood the NumPy argsort function and my real-world examples really well.

Join the Machine Learning & AI with Python discussion in our NeuralNetLab Reddit Community.

Summery

We use the NumPy Argsort function to return the indexes used to sort the array. There are many practical implications in this function in many real-world scenarios including python machine learning projects. To use it correctly we have to apply the function practically inside the code in a way that makes sense to our data.

1 thought on “Numpy Argsort with Example”

  1. Pingback: Numpy Sort for Machine Learning - Neural Net Lab

Leave a Reply