简体   繁体   English

Python numpy:向量(n-dim)与数组(nxn-dim)的维度[0]

[英]Python numpy: Dimension [0] in vectors (n-dim) vs. arrays (nxn-dim)

I'm currently wondering how the numpy array behaves. 我目前想知道numpy数组的行为。 I feel like the dimensions are not consistent from vectors ( Nx1 dimensional) to 'real arrays' ( NxN dimensional). 我觉得从向量( Nx1维)到“实际数组”( NxN维)的尺寸不一致。

I dont get, why this isn't working: 我不明白,为什么这行不通:

a = array(([1,2],[3,4],[5,6]))
concatenate((a[:,0],a[:,1:]), axis = 1)
# ValueError: all the input arrays must have same number of dimensions

It seems like the : (at 1:] ) makes the difference, but ( :0 is not working) 似乎: :(at 1:] )有所不同,但是( :0不起作用)

Thanks in advance! 提前致谢!

Detailled Version: So I would expect that shape(b)[0] references the vertical direction in ( Nx1 arrays), like in an 2D ( NxN ) array. 详细版本:因此,我希望shape(b)[0]在( Nx1数组)中引用垂直方向,就像在2D( NxN )数组中一样。 But it seems like dimension [0] is the horizontal direction in arrays ( Nx1 arrays)? 但是,似乎[0]维是数组( Nx1数组)中的水平方向吗?

from numpy import *

a = array(([1,2],[3,4],[5,6]))
b = a[:,0]
print shape(a)  # (3L, 2L), [0] is vertical
print a         # [1,2],[3,4],[5,6]
print shape(b)  # (3L, ), [0] is horizontal
print b         # [1 3 5]

c = b * ones((shape(b)[0],1)) 
print shape(c)  # (3L, 3L), I'd expect (3L, 1L)
print c         # [[ 1.  3.  5.], [ 1.  3.  5.], [ 1.  3.  5.]]

What did I get wrong? 我怎么了? Is there a nicer way than 有没有比这更好的方法

d = b * ones((1, shape(b)[0]))
d = transpose(d)
print shape(d)  # (3L, 1L)
print d         # [[ 1.], [ 3.], [ 5.]]

to get the ( Nx1 ) vector that I expect or want? 得到我期望或想要的( Nx1 )向量?

There are two overall issues here. 这里有两个总体问题。 First, b is not an (N, 1) shaped array, it is an (N,) shaped array. 首先, b 不是 (N, 1)形阵列,它是(N,)形阵列。 In numpy, 1D and 2D arrays are different things. 在numpy中,一维和二维数组是不同的东西。 1D arrays simply have no direction. 一维阵列根本没有方向。 Vertical vs. horizontal, rows vs. columns, these are 2D concepts. 垂直与水平,行与列,这是2D概念。

The second has to do with something called " broadcasting ". 第二个与所谓的“ 广播 ”有关。 In numpy arrays, you are able to broadcast lower-dimensional arrays to higher-dimensional ones, and the lower-dimensional part is applied elementwise to the higher-dimensional one. 在numpy数组中,您可以将低维数组广播到高维数组,并且将低维部分逐元素应用于高维数组。

The broadcasting rules are pretty simple: 广播规则非常简单:

When operating on two arrays, NumPy compares their shapes element-wise. 在两个数组上进行操作时,NumPy逐元素比较其形状。 It starts with the trailing dimensions, and works its way forward. 它从尾随尺寸开始,一直向前发展。 Two dimensions are compatible when 两种尺寸兼容

they are equal, or 它们相等,或者

one of them is 1 其中之一是1

In your case, it starts with the last dimension of ones((shape(b)[0],1)) , which is 1 . 在您的情况下,它以ones((shape(b)[0],1))的最后一个尺寸开始,即1 This meets the second criteria. 这符合第二个条件。 So it multiplies the array b elementwise for each element of ones((shape(b)[0],1)) , resulting in a 3D array. 因此,它将一个元素ones((shape(b)[0],1))每个元素逐元素地乘以数组b ,从而得到3D数组。

So it is roughly equivalent to: 因此大致相当于:

c = np.array([x*b for x in ones(shape(b))])

Edit: 编辑:

To answer your original question, what you want to do is to keep both the first and second arrays as 2D arrays. 要回答您的原始问题,您要做的是将第一个和第二个数组都保留为2D数组。

numpy has a very simple rule for this: indexing reduces the number of dimensions, slicing doesn't. numpy为此有一个非常简单的规则:索引减少了维数,切片却没有。 So all you need is to have a length-1 slice. 因此,您需要做的是长度为1的切片。 So in your example, just change a[:,0] to a[:,:1] . 因此,在您的示例中,只需将a[:,0]更改为a[:,:1] This means 'get every column up to the second one'. 这意味着“将每一列上移至第二列”。 Of course that only includes the first column, but it is still considered a slice operation rather than getting an element, so it still preservers the number of dimensions: 当然,它仅包括第一列,但仍被视为切片操作,而不是获取元素,因此它仍保留维数:

>>> print(a[:, 0])
[1 3 5]
>>> print(a[:, 0].shape)
(3,)
>>> print(a[:, :1])
[[1]
 [3]
 [5]]
>>> print(a[:, :1].shape)
(3, 1)
>>> print(concatenate((a[:,:1],a[:,1:]), axis = 1))
[[1 2]
 [3 4]
 [5 6]]

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

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