该方法返回的是矩阵a要素排序后的索引数据,干说无用,以二维数组为例,按照指定列,如第0列,进行排序。
Python代码:
首先看代码,再逐行解释。
import numpy as n
a = np.array([[0, 11, 12], [2, 2, 3], [7, 8, 9], [1, 2, 3]])
ind = np.argsort(a, axis=0)
print(a)
print(ind)
print(a[ind[:, 0]])
# Output:
# [[ 0 11 12]
# [ 2 2 3]
# [ 7 8 9]
# [ 1 2 3]]
# [[0 1 1]
# [3 3 3]
# [1 2 2]
# [2 0 0]]
# [[ 0 11 12]
# [ 1 2 3]
# [ 2 2 3]
# [ 7 8 9]]
待排序数组"a":
索引矩阵"ind":
排序后的数组"a":
参考资料:
- https://numpy.org/doc/stable/reference/generated/numpy.argsort.html
- https://gist.github.com/stevenvo/e3dad127598842459b68
欢迎关注微信公众号鼓励我继续分享知识:
版权声明:本文为weixin_42545675原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。