Python--argsort function

Source: Internet
Author: User

NumPy in the Argsort function usage, there is a need for friends to refer to the next.




Using Help in Python

>>> Import NumPy

>>> Help (Numpy.argsort)

Help on function Argsort in module numpy.core.fromnumeric:





Argsort (A, axis=-1, kind= ' quicksort ', Order=none)

Returns the indices that would sort an array.



Perform an indirect sort along the given axis using the algorithm specified

By the ' kind ' keyword. It returns an array of indices of the same shape as

' A ' that's index data along the given axis in sorted order.

As you can see, the Argsort function returns the index value of the array value from small to large

Examples

--------

One dimensional array: single-dimensional arrays



>>> x = Np.array ([3, 1, 2])

>>> Np.argsort (x)

Array ([1, 2, 0])



Two-dimensional array: two-dimensional arrays



>>> x = Np.array ([[0, 3], [2, 2]])

>>> x

Array ([[0, 3],

[2, 2]])



>>> np.argsort (x, axis=0) #按列排序

Array ([[0, 1],

[1, 0]])



>>> np.argsort (x, Axis=1) #按行排序

Array ([[0, 1],

[0, 1]])

#######################################

Example 1:

>>> x = Np.array ([3, 1, 2])

>>> np.argsort (x) #按升序排列

Array ([1, 2, 0])

>>> Np.argsort (-X) #按降序排列

Array ([0, 2, 1])

>>> X[np.argsort (x)] #通过索引值排序后的数组

Array ([1, 2, 3])

>>> X[np.argsort (-X)]

Array ([3, 2, 1])

Another way to achieve this is to sort in descending order:

>>> A = X[np.argsort (x)]

>>> A

Array ([1, 2, 3])

>>> A[::-1]

Array ([3, 2, 1])

In addition to the sort, there are two commonly used functions sort and sorted, see the details: Http://maoersong.blog.163.com/blog/static/171557351201424105925681/?newFollowBlog

Python--argsort function

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.