ValueError: zero-dimensional arrays cannot be concatenated
Asked Answered
D

2

13

I have the following values, each of which is a scalar of type double: a1, a2, a3, a4, a5.

I tried to concatenate them using Numpy, as follows:

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.

Diversity answered 27/1, 2018 at 0:15 Comment(6)
Why are you doing this weird, convoluted thing instead of np.array([a1, a2, a3, a4, a5])?Gravure
What are you trying to achieve?Xanthochroism
You attempt to use np.concatenate incorrectly. And yes, scalars are 0-dimensional arrays.Educate
Unless the goal of the question is to keep us vigilant and alert on a Friday night...Educate
@Educate ROTFL your last comment though ;)Joiejoin
Sheesh why are people being so snarky. I just had a case where I had a list of np arrays I was concatenating, but in one corner case I ended up with scalars so got this error.Drooff
S
15

concatenate turns each of the items in the list into an array (if it isn't already), and tries to join them:

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:

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:

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:

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*0 =0? 4 1d arrays joined on their common axis is 4*1=4; 4 2d arrays (n,m), will be either (4n,m) or (n,4m) depending on the axis.


np.stack also works. 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])
Strath answered 27/1, 2018 at 3:39 Comment(0)
R
0

I got the same error below:

ValueError: zero-dimensional arrays cannot be concatenated

Because I tried to concatenate 0D arrays with concatenate() as shown below. *concatenate() can concatenate 1D or more D arrays:

import numpy

array1 = numpy.array(3) # 0D array
array2 = numpy.array(5) # 0D array

numpy.concatenate((array1, array2)) # Error

So, I concatenated 1D arrays with concatenate(), then I could get the result as shown below:

import numpy

array1 = numpy.array([3]) # 1D array
array2 = numpy.array([5]) # 1D array

numpy.concatenate((array1, array2)) # array([3, 5])

Or, I concatenated 0D arrays with stack(), then I could get the result as shown below. *stack() can concatenate 0D or more D arrays:

import numpy

array1 = numpy.array(3) # 0D array
array2 = numpy.array(5) # 0D array

numpy.stack((array1, array2)) # array([3, 5])
Regarding answered 22/8 at 1:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.