对数组进行排序时,有时想返回排序后的索引,如返回数组中最小的三个值,这时可以用到np.argsort()函数,现在简单介绍一下这个函数。
np.argsort(a, axis=-1, kind=‘quicksort’, order=None)
Returns the indices that would sort an array.返回将对数组进行排序的索引。
代码示例:
一维情形:
x = np.random.randint(0,10,10)
print(x)
[9 2 2 7 1 5 1 5 1 2]
y = np.argsort(x)#默认从小到大排列,返回相应索引
print(y)
[4 6 8 1 2 9 5 7 3 0]
print