A brief introduction to the usage of argsort () in python

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.