You will learn how to sort indexes based on values in Numpy using the argsort() function.
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:
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.
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}")
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.
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}")
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.
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.