The argsort() function in the numpy module (turn)

this

The argsort function is a function in the Numpy module:

>>> 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.

The AN Along the INDIRECT Sort the Perform GIVEN the using Axis The algorithm specified
by The keyword `kind`. It AN Array Returns indices of Shape of The Same AS
` a` The GIVEN that Along Axis Data index in the sorted Order.

  It can be seen argsort function Returns the index value of the array value from small to large

Examples:

One dimensional array: One dimensional array

>>> x = np.array([3, 1, 2])
>>> np.argsort(x)
array([1, 2, 0])

Two-dimensional array:二维数组

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

>>> np.argsort(x, axis=0) #sort by column
array([[0, 1],
[1, 0]])

>>> np.argsort(x, axis=1) #Sort by row
array([[0, 1],
[0, 1]])

Examples:

>>> x = np.array([3, 1, 2])
>>> np.argsort(x) #Arrange in ascending order
array([1, 2, 0])
>>> np.argsort(-x) #Arrange in descending order
array([0, 2, 1])

>>> x[np.argsort(x)] #Array sorted by index value
array([1, 2, 3])
>>> x[np.argsort(-x)]
array([3, 2, 1])

Another way to sort in descending order:

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

Guess you like

Origin blog.csdn.net/Gussss/article/details/89880681