[PYTHON] I didn't understand the behavior of numpy's argsort, so I will summarize it.

Description of the return value of argsort

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

Digression

The default is ascending, so use slices when you want to descend.

>>> np.argsort(a)[::-1]
array([3, 1, 4, 2, 0])

Recommended Posts

I didn't understand the behavior of numpy's argsort, so I will summarize it.
I didn't understand the Resize of TensorFlow so I tried to summarize it visually.
I researched Docker, so I will summarize it
I couldn't understand Fence Repair of the ant book easily, so I will follow it in detail.
[Super basics of Python] I learned the basics of the basics, so I summarized it briefly.
[Student version] I didn't have much information about the installation procedure of CPLEX, so I wrote it.
I didn't know the basics of Python
Every time I draw a graph with python, I check it, so I will summarize only the simplest usage
The tree.plot_tree of scikit-learn was very easy and convenient, so I tried to summarize how to use it easily.
Somehow the code I wrote worked and I was impressed, so I will post it
I tried to use deep learning to extract the part where the plant is shown from the photo of the balcony, but it didn't work, so I will summarize the contents of trial and error. Part 2
I customized it with Visual Studio Code (mainly for python), so I will summarize it
[Django] I didn't quite understand what the reverse method is, so read the official documentation.
I have read a survey paper on time-series anomaly detection, so I will summarize it.
I tried to summarize the basic form of GPLVM
I want to fully understand the basics of Bokeh
I tried to summarize the string operations of Python
I made a function to crop the image of python openCV, so please use it.
I wrote it in Go to understand the SOLID principle
PyTorch's book was difficult to understand, so I supplemented it
I tried a little bit of the behavior of the zip function
[Machine learning] I tried to summarize the theory of Adaboost
I want to know the legend of the IT technology world
[Django] A brief summary of the log output function so that even beginners can understand it.
I want Airpods Pro, so I will notify LINE when it arrives
W3C Validators didn't work with Sublime Text3 so I made it work
I looked at the meta information of BigQuery & tried using it
I investigated the behavior of the difference between hard links and symbolic links
I tried to summarize the frequently used implementation method of pytest-mock
I made a segment tree with python, so I will introduce it
[python] A note that started to understand the behavior of matplotlib.pyplot
Note that I understand the algorithm of the machine learning naive Bayes classifier. And I wrote it in Python.
[Cliff in 2025] The Ministry of Economy, Trade and Industry's "DX Report 2" was published, so I read it.
I passed Java Gold in the first year of joining the company, so I will talk about study methods
[Golang] I want to add omitempty to the json tag of the int type field of the structure so that it will be ignored if 0 is entered.