简体   繁体   English

ValueError:零维数组不能串联

[英]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.

相关问题 ValueError: 零维 arrays 无法连接, - ValueError: zero-dimensional arrays cannot be concatenated, Python ValueError:零维 arrays 无法连接 - Python ValueError: zero-dimensional arrays cannot be concatenated ValueError:当 arrays 不是零维且在括号/括号内时,无法连接零维 arrays - ValueError: zero-dimensional arrays cannot be concatenated when arrays are not zero-dimensional and inside bracket/parenthesis 首次使用numpy打“ ValueError:零维数组无法连接” - Using numpy for first time hit “ValueError: zero-dimensional arrays cannot be concatenated” ValueError:即使在 np.concatenate 中使用元组后,也无法连接零维 arrays - ValueError: zero-dimensional arrays cannot be concatenated even after using tuple in np.concatenate 零维数组不能串联,但我的数组不是零维 - Zero-dimensional arrays cannot be concatenated, but my arrays aren't zero-dimensional NumPy:连接时出错 - 零维数组无法连接 - NumPy: Error While Concatenation - zero-dimensional arrays cannot be concatenated 合并两个2D数组时出错不能连接零维数组 - Error when merging two 2D arrays Zero-dimensional arrays cannot be concatenated 如何在pandas dataframe中替换零维arrays? - how to replace zero-dimensional arrays in pandas dataframe? 为什么记录中的零维numpy数组不支持项目分配? - Why don't zero-dimensional numpy arrays in records support item assignment?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM