获取排序的Numpy数组的原始索引 [英] Get original indices of a sorted Numpy array
问题描述
我有一个距离为a = np.array([20.5 ,5.3 ,60.7 ,3.0 ], 'double')
的数组,我需要排序后的数组的索引(例如[3, 1, 0, 2]
,对于a.sort()
). Numpy中有一个功能可以做到这一点吗?
I have an array of distances a = np.array([20.5 ,5.3 ,60.7 ,3.0 ], 'double')
and I need the indices of the sorted array (for example [3, 1, 0, 2]
, for a.sort()
). Is there a function in Numpy to do that?
推荐答案
是的,有x = numpy.argsort(a)
函数或x = numpy.ndarray.argsort(a)
方法.它完全符合您的要求.您也可以像在ndarray
对象上那样,将argsort
作为方法调用:a.argsort()
.
Yes, there's the x = numpy.argsort(a)
function or x = numpy.ndarray.argsort(a)
method. It does exactly what you're asking for. You can also call argsort
as a method on an ndarray
object like so: a.argsort()
.
以下是文档的链接: http ://docs.scipy.org/doc/numpy/reference/generation/numpy.argsort.html#numpy.argsort
这篇关于获取排序的Numpy数组的原始索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!