[英]ValueError: zero-dimensional arrays cannot be concatenated
I have the following values, each of which is a scalar of type double: a1, a2, a3, a4, a5
. 我有以下值,每个值都是double类型的标量: a1, a2, a3, a4, a5
。
I tried to concatenate them using Numpy, as follows: 我尝试使用Numpy将它们连接起来,如下所示:
f = np.concatenate((a1,a2,a3,a4,a5))
I however get the following error: 但是,我得到以下错误:
ValueError: zero-dimensional arrays cannot be concatenated
What could I be doing wrong? 我可能做错了什么?
Thanks. 谢谢。
concatenate
turns each of the items in the list into an array (if it isn't already), and tries to join them: concatenate
将列表中的每个项目变成一个数组(如果还没有的话),并尝试加入它们:
In [129]: np.concatenate([1,2,3,4])
...
ValueError: zero-dimensional arrays cannot be concatenated
hstack
takes the added step of: arrs = [atleast_1d(_m) for _m in tup]
, making sure they are at least 1d: hstack
增加了以下步骤: arrs = [atleast_1d(_m) for _m in tup]
,确保它们至少为1d:
In [130]: np.hstack([1,2,3,4])
Out[130]: array([1, 2, 3, 4])
But the standard way of creating an array from scalars is with np.array
, which joins the items along a new axis: 但是,从标量创建数组的标准方法是使用np.array
,它沿新轴连接各项:
In [131]: np.array([1,2,3,4])
Out[131]: array([1, 2, 3, 4])
Note that np.array
of 1 scalar is a 0d array: 注意,标量为1的np.array
是一个0d数组:
In [132]: np.array(1)
Out[132]: array(1)
In [133]: _.shape
Out[133]: ()
If I want to join 4 0d arrays together, how long will that be? 如果我想将4个0d数组连接在一起,那要花多长时间? 4*0 =0? 4 * 0 = 0? 4 1d arrays joined on their common axis is 4*1=4; 在其公共轴上连接的4个1d数组为4 * 1 = 4; 4 2d arrays (n,m), will be either (4n,m) or (n,4m) depending on the axis. 根据轴的不同,4个2d数组(n,m)将为(4n,m)或(n,4m)。
np.stack
also works. np.stack
也可以。 It does something similar to: 它的作用类似于:
In [139]: np.concatenate([np.expand_dims(i,axis=0) for i in [1,2,3,4]])
Out[139]: array([1, 2, 3, 4])
Scalars are 0-dimentional arrays, therefore they cannot be concatenated. 标量是0维数组,因此无法连接。 Increasing their dimention may allow you to concatenate them otherwise you can't use this method on the scalars. 增加它们的尺寸可以将它们连接起来,否则就不能在标量上使用此方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.