If you use argsort for ndarray, the index when sorted in ascending order will be returned.
>>> a=np.array([1,4,2,5,3])
>>> print(a)
[1 4 2 5 3]
>>> np.argsort(a)
array([0, 2, 4, 1, 3])
Of the values in the ndarray ... Since the minimum value is 1, its index is 0, The next smallest value is 2, so its index is 2 The next smallest value is 3, so its index is 4 The result will be something like ...
If you want to know the index of the third smallest value, you can do as follows.
>>> np.argsort(a)[2]
4
Once confirmed, it is as follows
>>> a[4]
3
The default is ascending, so use slices when you want to descend.
>>> np.argsort(a)[::-1]
array([3, 1, 4, 2, 0])
Recommended Posts