更新时间:2023-11-23 08:28:58
您的起始数组不为空(好的,它确实有0个元素),并且尺寸未知.它具有定义明确的形状和尺寸数(1d).
Your starting array is not empty (ok, it does have 0 elements), and not of unknown dimensions. It has a well defined shape and number of dimensions (1d).
In [704]: a=np.array([])
In [705]: a.shape
Out[705]: (0,)
In [706]: a.ndim
Out[706]: 1
有关连接a
In [708]: np.concatenate((a,a,a,a)).shape
Out[708]: (0,)
In [709]: np.concatenate((a,np.zeros(3))).shape
Out[709]: (3,)
作为一般规则,请勿以空"数组开头,并尝试重复附加到该数组.这是一种list
方法,对于数组而言效率不高.而且由于尺寸问题,可能无法正常工作.
As a general rule, don't start with an 'empty' array and try to append to it repeatedly. That's a list
approach, and is not efficient with arrays. And because of the dimensionality issue, might not work.
进行重复追加的正确方法如下:
A correct way of doing a repeated append is something like:
alist = []
for i in range(3):
alist.append([1,2,3])
np.array(alist)
子列表的长度是否相同?在您的最后一个示例中,它们不同,并且阵列版本为dtype=object
.它是1d,指向内存中其他列表的指针-即荣耀列表.
Are the sublists all the same length or not? In your last example, they differ, and the array version is dtype=object
. It is 1d, with pointers to lists else where in memory - i.e. glorified list.
In [710]: np.array([[0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0]])
Out[710]: array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0]], dtype=object)
这与通过vstack
相同长度的3个数组得到的结果完全不同.
This is a very different thing from what you'd get by vstack
of 3 arrays of the same length.
我认为您需要更多有关基本数组构造以及shape
和尺寸的含义的练习.标题本身显示出一些混乱-array of length N
array of N-dimensions
.这些是完全不同的描述.
I think you need more practice with basic array construction, and the meaning of shape
and dimensions. The title itself shows some confusion - array of length N
array of N-dimensions
. Those are very different descriptions.
============
============
concatenate
的基本要点是(n1,m)
数组可以与(n2,m)
数组连接在一起以生成(n1+n2,m)
数组.其他尺寸也一样.如果一个数组为1d (m,)
,则需要将其扩展为(1,m)
来执行此串联. vstack
为您进行这种扩展.
The basic point with concatenate
is that a (n1,m)
array can be joined with a (n2,m)
array to produce a (n1+n2,m)
array. Similarly for other dimensions. If one array is 1d (m,)
it needs to be expanded to (1,m)
to perform this concatenation. vstack
does this kind of expansion for you.
这意味着(0,)
形状数组可以与其他1d数组水平连接,但是除了(n2,0)
数组之外,不能参与2d垂直连接.一个维度的大小可能为0,但这很少有用,因为它不包含任何数据.
This means that a (0,)
shape array can be concatenated horizontally with other 1d arrays, but can't participate in a 2d vertical concatenation except with a (n2,0)
array. A dimension may have size 0, but this is rarely useful, since it doesn't contain any data.