开发者

Numpy sorting help

开发者 https://www.devze.com 2023-01-18 20:13 出处:网络
In Numpy, how do I create an array of indices which can be used return the values of the source array in sorted order? eg:

In Numpy, how do I create an array of indices which can be used return the values of the source array in sorted order? eg:

Source:
     [[4  2  6  7]
      [1  4  8  9]
      [3  1开发者_Python百科  0  3]]

Indices:
     [10  4  9  1  8  11   0  5  2  3  6  7]


Take a look at numpy.argsort - it will return the indices that would sort your array. You can also specifiy the axis along which to sort. Try:

a = numpy.asarray([[4, 2, 6, 7], [1, 4, 8, 9], [3, 1, 0, 3]])
numpy.argsort(a.flat)

>> array([10,  4,  9,  1,  8, 11,  0,  5,  2,  3,  6,  7])


The answer's in the manual:

src = [[ ... ]]
ravel_src = np.ravel(src)
indices = np.argsort(ra)
0

精彩评论

暂无评论...
验证码 换一张
取 消