简体   繁体   English

用矩阵对numpy向量进行排序

[英]Sorting numpy vector with matrix

I have written this piece of code: 我已经写了这段代码:

w = Dist.argsort(kind='mergesort');
return y[w];

where w is MxN matrix, and y is Nx1 vector. 其中w是MxN矩阵, y是Nx1向量。 Everything works fine if shape returned by np.shape(y) is (3,). 如果np.shape(y)返回的np.shape(y)为(3,),则一切正常。 However, when I try to input a vector of shape(3,1), my function returns a 3-dimensional MXNx1 matrix. 但是,当我尝试输入shape(3,1)的向量时,我的函数将返回3维MXNx1矩阵。

Is there a way to reduce (N,1) vector to (N,) size? 有没有办法将(N,1)向量减小为(N,)大小? Or to normalize output of the function for (N,1) vectors? 还是要归一化(N,1)个向量的函数输出?

You can reduce an (N,1) numpy array to (N,) with flatten : 您可以使用flatten将(N,1)numpy数组减少为(N,):

>>>np.zeros((3, 1)).shape
(3, 1)

>>>np.zeros((3, 1)).flatten().shape
(3,)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM