简体   繁体   English

numpy中数组运算混乱

[英]Confusion in array operation in numpy

I generally use MATLAB and Octave , and i recently switching to python numpy .我通常使用MATLABOctave ,我最近切换到python numpy In numpy when I define an array like this在 numpy 中,当我这样定义一个数组时

>>> a = np.array([[2,3],[4,5]])

it works great and size of the array is它工作得很好,数组的大小是

>>> a.shape
(2, 2)

which is also same as MATLAB But when i extract the first entire column and see the size这也与 MATLAB 相同但是当我提取第一整列并查看大小时

>>> b = a[:,0]
>>> b.shape
(2,)

I get size (2,) , what is this?我得到大小(2,) ,这是什么? I expect the size to be (2,1) .我希望大小为(2,1) Perhaps i misunderstood the basic concept.也许我误解了基本概念。 Can anyone make me clear about this??任何人都可以让我清楚这一点吗?

A 1D numpy array* is literally 1D - it has no size in any second dimension, whereas in MATLAB, a '1D' array is actually 2D, with a size of 1 in its second dimension. 1D numpy array* 实际上是 1D - 它在任何第二个维度上都没有大小,而在 MATLAB 中,“1D”数组实际上是 2D,其第二个维度的大小为 1。

If you want your array to have size 1 in its second dimension you can use its .reshape() method:如果您希望数组的第二维大小为 1,则可以使用其.reshape()方法:

a = np.zeros(5,)
print(a.shape)
# (5,)

# explicitly reshape to (5, 1)
print(a.reshape(5, 1).shape)
# (5, 1)

# or use -1 in the first dimension, so that its size in that dimension is 
# inferred from its total length
print(a.reshape(-1, 1).shape)
# (5, 1)

Edit编辑

As Akavall pointed out, I should also mention np.newaxis as another method for adding a new axis to an array.正如 Akavall 指出的那样,我还应该提到np.newaxis作为向数组添加新轴的另一种方法。 Although I personally find it a bit less intuitive, one advantage of np.newaxis over .reshape() is that it allows you to add multiple new axes in an arbitrary order without explicitly specifying the shape of the output array, which is not possible with the .reshape(-1, ...) trick:虽然我个人觉得它不太直观, np.newaxis优于.reshape()的一个优点是它允许您以任意顺序添加多个新轴,而无需明确指定 output 数组的形状,这是不可能的.reshape(-1, ...)技巧:

a = np.zeros((3, 4, 5))
print(a[np.newaxis, :, np.newaxis, ..., np.newaxis].shape)
# (1, 3, 1, 4, 5, 1)

np.newaxis is just an alias of None , so you could do the same thing a bit more compactly using a[None, :, None, ..., None] . np.newaxis只是None的别名,因此您可以使用a[None, :, None, ..., None]更紧凑地做同样的事情。


* An np.matrix , on the other hand, is always 2D, and will give you the indexing behavior you are familiar with from MATLAB: * 另一方面, np.matrix始终是二维的,并且会为您提供您在 MATLAB 中熟悉的索引行为:

a = np.matrix([[2, 3], [4, 5]])
print(a[:, 0].shape)
# (2, 1)

For more info on the differences between arrays and matrices, see here .有关 arrays 和矩阵之间差异的更多信息,请参见此处

Typing help(np.shape) gives some insight in to what is going on here.输入help(np.shape)可以深入了解这里发生的事情。 For starters, you can get the output you expect by typing:对于初学者,您可以通过键入以下内容获得您期望的 output:

b = np.array([a[:,0]])

Basically numpy defines things a little differently than MATLAB. In the numpy environment, a vector only has one dimension, and an array is a vector of vectors, so it can have more.基本上 numpy 定义的东西与 MATLAB 略有不同。在 numpy 环境中,向量只有一维,而数组是向量的向量,因此它可以有更多维。 In your first example, your array is a vector of two vectors, ie:在您的第一个示例中,您的数组是两个向量的向量,即:

a = np.array([[vec1], [vec2]])

So a has two dimensions, and in your example the number of elements in both dimensions is the same, 2. Your array is therefore 2 by 2. When you take a slice out of this, you are reducing the number of dimensions that you have by one.所以a有两个维度,在你的例子中,两个维度中的元素数量是相同的,2。因此你的数组是 2 x 2。当你从中取出一个切片时,你正在减少你拥有的维度数量一个。 In other words, you are taking a vector out of your array, and that vector only has one dimension, which also has 2 elements, but that's it.换句话说,您要从数组中取出一个向量,而该向量只有一维,也有 2 个元素,仅此而已。 Your vector is now 2 by _ .您的向量现在是 2 by _ There is nothing in the second spot because the vector is not defined there.第二个地方什么也没有,因为那里没有定义向量。

You could think of it in terms of spaces too.你也可以从空间的角度来考虑它。 Your first array is in the space R^(2x2) and your second vector is in the space R^(2) .您的第一个数组位于空间R^(2x2)中,您的第二个向量位于空间R^(2)中。 This means that the array is defined on a different (and bigger) space than the vector.这意味着数组定义在与向量不同(且更大)的空间中。

That was a lot to basically say that you took a slice out of your array, and unlike MATLAB, numpy does not represent vectors (1 dimensional) in the same way as it does arrays (2 or more dimensions).基本上说您从数组中取出了一个切片,并且与 MATLAB 不同,numpy 并不像 arrays(2 维或更多维)那样表示向量(1 维)。

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

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