[英]sort numpy 2d array by indice of column
I am using numpy in python.我在 python 中使用 numpy。 I have a 1D(nx1) array and a 2D(nxm) array.我有一个 1D(nx1) 数组和一个 2D(nxm) 数组。 I used argsort to get a indice of the 1D array.我使用 argsort 来获取一维数组的索引。 Now I want to use that indice to sort my 2D(nxm) array's colum.现在我想使用该索引对我的 2D(nxm) 数组的列进行排序。
I want to know how to do it?我想知道怎么做?
For example:例如:
>>>array1d = np.array([1, 3, 0])
>>>array2d = np.array([[1,2,3],[4,5,6]])
>>>array1d_indice = np.argsort(array1d)
array([2, 0, 1], dtype=int64)
I want use array1d_indice to sord array2d colum to get:
[[3, 1, 2],
[6, 4, 5]]
Or anyway easier to achieve this is welcome或者无论如何更容易实现这一点是受欢迎的
If what you mean is that you want the columns sorted based on the vector, then you use argsort on the vector:如果您的意思是希望根据向量对列进行排序,那么您可以对向量使用 argsort:
vi = np.argsort(vector)
then to arrange the columns of array
in the right order,然后按正确的顺序array
的列,
sorted = array[:, tuple(vi)]
to get rows, switch around the order of :
and tuple(vi)
要获取行,请切换:
和tuple(vi)
的顺序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.