.argsort() python code example
Example 1: .argsort() python
x = np.array([[0,3],[2,2]])
>>> ind = np.argsort(x, axis=1) # sorts along last axis (across)
>>> ind
array([[0, 1],
[0, 1]])
>>> np.take_along_axis(x, ind, axis=1) # same as np.sort(x, axis=1)
array([[0, 3],
[2, 2]])
Example 2: python argsort a list
a=list((1, 2, 3, -20))
sorted(range(len(a)), key=a.__getitem__)
output:
[3, 0, 1, 2]
Example 3: python argsort
>>> x = np.array([3, 1, 2])
>>> np.argsort(x)
array([1, 2, 0])