Reputation: 9869
I wish to concatenate the following arrays:
a=np.array([[1,2],[1],[2,3,4]])
b=np.array([[20,2]])
np.concatenate((a,b),axis=0)
but I get the following error:
ValueError Traceback (most recent call last)
<ipython-input-40-42253341965b> in <module>()
----> 1 np.concatenate((a,b),axis=0)
ValueError: all the input arrays must have same number of dimensions
I was expecting the answer to be [[1,2],[1],[2,3,4],[20,2]]
. If b=np.array([20,2])
instead the concatenation works fine except I get the answer: [[1,2],[1],[2,3,4],20,2]
Upvotes: 1
Views: 74
Reputation:
Check the dtype, ndim and shape of a
: you'll find that those are numpy.object
, 1 and (3,)
, respectively. This is because array a
contains lists of different lengths, so each list is treated as an object, and a
is a one dimensional array of objects. I don't know what you are aiming for, but if you want a
to have an ndim of 2, you'll need to make sure all the lists have the same length.
Of course, b
has an ndim of 2, since it contains only one (nested) lists, which will always result in a regular n-dimensional array.
The error message is then obvious: you're trying to concatenate two arrays with different dimensions: that won't work.
To get the answer you're seeking, [[1,2],[1],[2,3,4],[20,2]]
, you'll need to convert the inner list of b
into an object as well: then you're concatenating two 1 dimensional arrays of objects.
Upvotes: 3