Python numpy argsort function usage

Source: Internet
Author: User

Numpy.argsort
NumPy. Argsort ( a, axis=-1, kind= ' quicksort ', order=none ) [Source]

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 asa , the index data along the given axis in sorted order.

parameters:

A  : array_like

Array to sort.

Axis  : int or None, optional

Axis along which to sort. The default is-1 (the last axis). If None, the flattened array is used.

Kind  : {' quicksort ', ' mergesort ', ' heapsort '}, optional

So Rting algorithm.

order  : list, optional

When a  is An array with fields defined, this argument specifies which fields to compare first, second, etc. Not all fields need is specified.

Returns:

Index_array : ndarray, int

Array of indices that sort a along the specified axis. In other words, A[index_array] yields a sorted a.

See Also

Sort
Describes sorting algorithms used.
Lexsort
Indirect stable sort with multiple keys.
Ndarray.sort
Inplace sort.
Argpartition
Indirect Partial sort.

Notes

See sort for notes on the different sorting algorithms.

As of NumPy 1.4.0 Argsort works with Real/complex arrays containing Nan values. The enhanced sort order is documented in sort.

Examples

One dimensional array:

>>>
NP.  Array([312])NP.  Argsort(x)Array ([1, 2, 0])           

Two-dimensional array:

>>>
NP.  Array([[[03[22]])x Array ([[0, 3], [2, 2]])    
>>>
NP.  Argsort(xAxis=0)array ([[[0, 1], [1, 0]])       
>>>
NP.  Argsort(xAxis=1)Array ([[[0, 1], [0, 1]])     

Sorting with keys:

>>>
 >>> x = np.array ([(10 (01dtype=[( ' x '  ' <i4 '  ( ' y '  ' <i4 ' >>> xarray ([(1, 0), (0, 1)], dtype=[(' X ', ' <i4 '), (' Y ', ' <i4 ')])             
>>>
NP.  Argsort(xOrder=(' x ',' y '))Array ([1, 0])    
>>>
NP.  Argsort(xOrder=(' y ',' x '))Array ([0, 1])    

Python numpy argsort function usage

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.