This article mainly introduces the example usage of argsort () function in python, and introduces the usage of argsort () function in detail, which has some reference value, for more information, see. This article mainly introduces the example usage of argsort () function in python, and introduces the usage of argsort () function in detail, which has some reference value, for more information, see.
Because I want to use python to classify many images in batches using the trained caffemodel and encountered the argsort function during the learning process, I checked the relevant literature, I also tested it in the python environment and probably learned about its usefulness. in order to fear that I forgot it later, I wrote down the right to better understand it. (Ps: I am also a python hacker. it may be easy to understand)
1. define an array data first
import numpy as npx=np.array([1,4,3,-1,6,9])
2. now let's take a look at the specific functions of the argsort () function:
x.argsort()
The output is defined as y = array ([3, 0, 2, 1, 4, 5]).
We found that the argsort () function is to sort the elements in x from small to large, extract the corresponding index, and then output it to y. For example, x [3] =-1 is the smallest, so y [0] = 3, x [5] = 9 is the largest, so y [5] = 5.
The above is not hard to understand. if you are not familiar with it, you can try it yourself in the python environment.
3. because the program encountered a problem similarnp.argsort()[num]You can try it yourself in the python environment:
Ps: the absolute value of num is less than or equal to the number of elements in x.
WhenWhen num> = 0, np. argsort () [num] can be understood as y [num];
WhenWhen num is <0, np. argsort () [num] is used to reverse output the elements of array y.Such as np. argsort () [-1] is the index, np. argsort () [-2] refers to the index corresponding to the second largest value in x, and so on ..
You can see the results through an intuitive experiment. The following is an example of the above verification:
This is the output when num> = 0.
The above is a brief overview of the usage of the argsort () function in python. For more information, see other related articles in the first PHP community!