我想根据总和排序一个numpy数组.就像是
import numpy as np
a = np.array([1,2,3,8], [3,0,2,1])
b = np.sum(a, axis = 0)
idx = b.argsort()
Run Code Online (Sandbox Code Playgroud)
现在np.take(a,idx)导致[2,1,3,8].
但我想要一个数组:result = np.array([2,1,3,8],[0,3,2,1]]
什么是最聪明,最快速的方法?
使用您问题中的相同代码,您可以使用可选axis参数np.take(默认使用flattened数组,这就是您只获得第一行的原因,请参阅文档):
>>> np.take(a, idx, axis=1)
array([[2, 1, 3, 8],
[0, 3, 2, 1]])
Run Code Online (Sandbox Code Playgroud)
或者您可以使用花式索引:
>>> a[:,idx]
array([[2, 1, 3, 8],
[0, 3, 2, 1]])
Run Code Online (Sandbox Code Playgroud)