Find Indexes of Sorted Values in Numpy

You will learn how to sort indexes based on values in Numpy using the argsort() function.

numpy arsort on axis

What is the argsort() function?

The argsort() function in Numpy returns the indices of an array sorted by its values. The default is to sort the values in ascending order, but you can also specify descending order by passing the reverse=True argument.

How to sort indexes based on values

To sort indexes based on values in Numpy, you can use the following steps:

Import the numpy library.
Create an array of values.
Call the argsort() function on the array.

The argsort() function will return an array of indices, where the element at each index is the original index of the corresponding sorted value.

Example:

Numpy argort index sort

Indexes of sorted values have been returned.
This means that the values in the original array are sorted in ascending order, and the indexes correspond to the original indices of the sorted values.

See also  Linear Regression with NumPy

Sorting multidimensional arrays

You can also sort indexes based on values in multidimensional arrays. To do this, you can pass the axis argument to the argsort() function. The axis argument specifies the axis along which the sorting should be performed.

For example, the following code sorts the indexes of a 2D array based on the values in the first dimension:

import numpy as np

my_list = np.array([5, 7, 1, 3, 2, 8]).reshape(3, 2)
print(f"This is my array: \n {my_list}")

indexes_sorted = my_list.argsort()
print(f"There are indexes where you can find my values"
      f" in ascending order: \n {indexes_sorted}")

Numpy sort indexes of 2d array

This means that the values in the first dimension of the original array are sorted in ascending order, and the indexes 0, 1, 2, and 2 correspond to the original indices of the sorted values in the first dimension.

Sorting Different Axes

The argsort() function can also be used to sort indexes based on values in different axes of a multidimensional array. The axis argument specifies the axis along which the sorting should be performed.

See also  How to add two arrays in Numpy?

By default, the argsort() function sorts based on the last axis of the array. This means that if you have a 2D array, the values in the second dimension will be sorted.

To sort the indexes based on the values in the first dimension, you can pass the axis=0 argument to the argsort() function.

For example, the following code sorts the indexes of a 2D array based on the values in the first dimension:

import numpy as np

my_list = np.array([5, 7, 1, 3, 2, 8]).reshape(3, 2)
print(f"This is my 2d array: \n {my_list}")

indexes_sorted = my_list.argsort(axis=0)
print(f"There are indexes where you can find my values"
      f" in ascending order on 0 axis: \n {indexes_sorted}")

numpy arsort on axis

This means that the values in the first dimension of the original array are sorted in ascending order, and the indexes correspond to the original indices of the sorted values in the first dimension.

See also  How to Append to an Empty Array in Numpy (with Examples)

You can also sort the indexes based on the values in the second dimension by passing the axis=1 argument to the argsort() function.

For example, the following code sorts the indexes of a 2D array based on the values in the second dimension:

import numpy as np

my_list = np.array([5, 7, 1, 3, 2, 8]).reshape(3, 2)
print(f"This is my 2d array: \n {my_list}")

indexes_sorted = my_list.argsort(axis=1)
print(f"There are indexes where you can find my values"
      f" in ascending order on 1 axis: \n {indexes_sorted}")

This means that the values in the second dimension of the original array are sorted in ascending order, and the indexes correspond to the original indices of the sorted values in the second dimension.