python:argsort(返回元素排序后的索引值)

https://numpy.org/doc/stable/reference/generated/numpy.argsort.html   官方英文

https://www.osgeo.cn/numpy/reference/generated/numpy.argsort.html  官方中文

https://www.cnblogs.com/yyxf1413/p/6253995.html   浅述python中argsort()函数的用法

https://blog.csdn.net/maoersong/article/details/21875705   numpy中argsort函数用法(有升序、降序用法)

https://blog.csdn.net/u014745194/article/details/73496836   argsort()函数的总结(看这个,举的例子很清晰!!!)

 

argsort():将数组x中的元素从小到大排序,然后返回对应的索引值。

一维数组:

import numpy as np

# 一维数组
x = np.array([3, 1, 6, 2])
y1 = np.argsort(x)   #升序排列
print(y1)
y2 = np.argsort(-x)   #降序排列
print(y2)

# 输出结果:
[1 3 0 2]
[2 0 3 1]

 二维数组:

# 二维数组
x = np.array([[0, 3, -1], [2, 5, 8]])

y1 = np.argsort(x, axis=0) #按列排序
print(y1)

y2 = np.argsort(x, axis=1) #按行排序
print(y2) 


# 输出结果:
[[0 0 0]
 [1 1 1]]

[[2 0 1]
 [0 1 2]]

 

おすすめ

転載: blog.csdn.net/weixin_39450145/article/details/115603579