简体   繁体   English

选择numpy数组的列

[英]Selecting a column of a numpy array

I am somewhat confused about selecting a column of an NumPy array, because the result is different from Matlab and even from NumPy matrix. 我对选择NumPy数组的列感到有些困惑,因为结果与Matlab甚至NumPy矩阵不同。 Please see the following cases. 请参阅以下案例。

In Matlab , we use the following command to select a column vector out of a matrix. Matlab中 ,我们使用以下命令从矩阵中选择列向量。

x = [0, 1; 2 3]
out = x(:, 1)

Then out becomes [0; 2] 然后out成为[0; 2] [0; 2] , which is a column vector. [0; 2] ,这是一个列向量。

To do the same thing with a NumPy Matrix 使用NumPy Matrix做同样的事情

x = np.matrix([[0, 1], [2, 3]])
out = x[:, 0]

Then the output is np.matrix([[0], [2]]) which is expected, and it is a column vector. 然后输出是np.matrix([[0], [2]]) ,它是一个列向量。

However, in case of NumPy array 但是,在NumPy数组的情况下

x = np.array([[0, 1], [2, 3]])
out = x[:, 0]

The output is np.array([0, 2]) which is 1 dimensional, so it is not a column vector. 输出是np.array([0, 2]) ,它是1维的,因此它不是列向量。 My expectation is it should have been np.array([[0], [2]]) . 我的期望是它应该是np.array([[0], [2]]) I have two questions. 我有两个问题。

1. Why is the output from the NumPy array case different form the NumPy matrix case? 1. 为什么NumPy数组的输出与NumPy矩阵情况不同? This is causing a lot of confusion to me, but I think there might be some reason for this. 这给我带来了很多困惑,但我认为这可能有一些原因。

2. To get a column vector from a 2-Dim NumPy Array, then should I do additional things like expand_dims 2. 要从2-Dim NumPy数组中获取列向量,我应该执行其他操作,例如expand_dims

x = np.array([[0, 1], [2, 3]])
    out = np.expand_dims(x[:, 0], axis = 1)

In MATLAB everything has atleast 2 dimensions. 在MATLAB中,一切都至少有2个维度。 In older MATLABs, 2d was it, now they can have more. 在较旧的MATLAB中,2d是它,现在它们可以有更多。 np.matrix is modeled on that old MATLAB. np.matrix以旧的MATLAB为模型。

What does MATLAB do when you index a 3d matrix? 当您索引3d矩阵时,MATLAB会做什么?

np.array is more general. np.array更通用。 It can have 0, 1, 2 or more dimensions. 它可以有0,1,2或更多维度。

x[:, 0]
x[0, :]

both select one column or row, and return an array with one less dimension. 两者都选择一列或一行,并返回一个维度较少的数组。

x[:, [0]]
x[[0], :]

would return 2d arrays, with a singleton dimension. 将返回具有单个维度的2d数组。

In Octave (MATLAB clone) indexing produces inconsistent results, depending on which side of matrix I select: 在Octave(MATLAB克隆)中,索引会产生不一致的结果,具体取决于我选择的矩阵的哪一侧:

octave:7> x=ones(2,3,4);
octave:8> size(x)
ans =
   2   3   4

octave:9> size(x(1,:,:))
ans =
   1   3   4

octave:10> size(x(:,:,1))
ans =    
   2   3

MATLAB/Octave adds dimensions at the end, and apparently readily squeezes them down on that side as well. MATLAB / Octave最后增加了尺寸,显然也很容易将它们压在那边。

numpy orders the dimensions in the other direction, and can add dimensions at the start as needed. numpy在另一个方向上订购尺寸,并且可以根据需要在开始时添加尺寸。 But it is consistent in squeezing out singleton dimensions when indexing. 但是在索引时挤出单个维度是一致的。

The fact that numpy can have any number of dimensions, while MATLAB has a minimum of 2 is a crucial difference that often trips up MATLAB users. numpy可以具有任意数量的维度,而MATLAB至少有2个这一事实是一个至关重要的区别,通常会使MATLAB用户绊倒。 But one isn't any more logical than the other. 但是,没有一个比另一个更合乎逻辑。 MATLAB's practice is more a more matter of history than general principals. 与一般原理相比,MATLAB的实践更多的是历史问题。

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

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