Python numpy 数组连接错误:无法连接 0-d 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25453173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
numpy array concatenation error: 0-d arrays can't be concatenated
提问by Bin Zhou
I am trying to concatenate two numpy arrays, but I got this error. Could some one give me a bit clue about what this actually means?
我正在尝试连接两个 numpy 数组,但出现此错误。有人能给我一些关于这实际上意味着什么的线索吗?
Import numpy as np
allValues = np.arange(-1, 1, 0.5)
tmp = np.concatenate(allValues, np.array([30], float))
Then I got
然后我得到了
ValueError: 0-d arrays can't be concatenated
If I do
如果我做
tmp = np.concatenate(allValues, np.array([50], float))
There is no error message but tmp variable does not reflect the concatenation either.
没有错误消息,但 tmp 变量也不反映串联。
采纳答案by Roger Fan
You need to put the arrays you want to concatenate into a sequence (usually a tuple or list) in the argument.
您需要将要连接的数组放入参数中的序列(通常是元组或列表)中。
tmp = np.concatenate((allValues, np.array([30], float)))
tmp = np.concatenate([allValues, np.array([30], float)])
Check the documentationfor np.concatenate. Note that the first argument is a sequence (e.g. list, tuple) of arrays. It does nottake them as separate arguments.
检查文档的np.concatenate。请注意,第一个参数是数组的序列(例如列表、元组)。它并没有把他们作为独立参数。
As far as I know, this API is shared by all of numpy's concatenation functions: concatenate, hstack, vstack, dstack, and column_stackall take a single main argument that should be some sequence of arrays.
据我所知,这个 API 由所有 numpy 的连接函数共享:concatenate, hstack, vstack, dstack, 并且column_stack都采用一个主参数,该参数应该是一些数组序列。
The reason you are getting that particular error is that arrays are sequences as well. But this means that concatenateis interpreting allValuesas a sequence of arrays to concatenate. However, each element of allValuesis a float rather than an array, and is therefore being interpreted as a zero-dimensional array. As the error says, these "arrays" cannot be concatenated.
您收到该特定错误的原因是数组也是序列。但这意味着将concatenate其解释allValues为要连接的数组序列。但是, 的每个元素allValues都是浮点数而不是数组,因此被解释为零维数组。正如错误所说,这些“数组”无法连接。
The second argument is taken as the second (optional) argument of concatenate, which is the axis to concatenate on. This only works because there is a single element in the second argument, which can be cast as an integer and therefore is a valid value. If you had put an array with more elements in the second argument, you would have gotten a different error:
第二个参数作为 的第二个(可选)参数concatenate,它是要连接的轴。这只有效,因为第二个参数中有一个元素,它可以转换为整数,因此是一个有效值。如果你在第二个参数中放置了一个包含更多元素的数组,你会得到一个不同的错误:
a = np.array([1, 2])
b = np.array([3, 4])
np.concatenate(a, b)
# TypeError: only length-1 arrays can be converted to Python scalars
回答by mithunpaul
Also make sure you are concatenating two numpy arrays. I was concatenating one python array with a numpy array and it was giving me the same error:
还要确保您正在连接两个 numpy 数组。我将一个 python 数组与一个 numpy 数组连接起来,它给了我同样的错误:
ValueError: 0-d arrays can't be concatenated
It took me some time to figure this out since all the answers in stackoverflow were assuming that you had two numpy arrays. Pretty silly but easily overlooked mistake. Hence posting just in case this helps someone.
我花了一些时间才弄清楚这一点,因为 stackoverflow 中的所有答案都假设您有两个 numpy 数组。很愚蠢但很容易被忽视的错误。因此发布以防万一这有助于某人。
Here are the links to converting an existing python array using np.asarrayor create np arrays, if it helps.
如果有帮助,这里是使用np.asarray转换现有 python 数组 或 创建 np arrays的链接。
回答by kchalk
Another way to get this error is to have two numpy objects of different... types?
获得此错误的另一种方法是拥有两个不同...类型的 numpy 对象?
I get this error when I try np.concatenate([A,B])
我尝试时收到此错误 np.concatenate([A,B])
and ValueError: all the input arrays must have same number of dimensionswhen I run np.concatenate([B,A])
而ValueError: all the input arrays must have same number of dimensions当我运行np.concatenate([B,A])
Just as @mithunpaul mentioned, my types are off: A is an array of 44279x204 and B is a <44279x12 sparse matrix of type '<class 'numpy.float64'>' with 88558 stored elements in Compressed Sparse Row format>)
正如@mithunpaul 提到的,我的类型是关闭的:A 是一个 44279x204 的数组,B 是一个 <44279x12 sparse matrix of type '<class 'numpy.float64'>' with 88558 stored elements in Compressed Sparse Row format>)
So that's why the error is happening. Don't know how to solve it yet though.
所以这就是错误发生的原因。不过还不知道怎么解决。

