numpy array concatenate: "ValueError: all the input arrays must have same number of dimensions"

numpy array append
numpy empty array
numpy stack
only integer scalar arrays can be converted to a scalar index concatenate
numpy concatenate vs append
numpy array to list
numpy - stack 2d arrays
numpy join arrays

How to concatenate these numpy arrays?

first np.array with a shape (5,4)

[[  6487    400 489580      0]
 [  6488    401 492994      0]
 [  6491    408 489247      0]
 [  6491    408 489247      0]
 [  6492    402 499013      0]]

second np.array with a shape (5,)

[  16.   15.   12.  12.  17. ]

final result should be

[[  6487    400    489580    0   16]
 [  6488    401    492994    0   15]
 [  6491    408    489247    0   12]
 [  6491    408    489247    0   12]
 [  6492    402    499013    0   17]]

I tried np.concatenate([array1, array2]) but i get this error

ValueError: all the input arrays must have same number of dimensions

What am I doing wrong?

To use np.concatenate, we need to extend the second array to 2D and then concatenate along axis=1 -

np.concatenate((a,b[:,None]),axis=1)

Alternatively, we can use np.column_stack that takes care of it -

np.column_stack((a,b))

Sample run -

In [84]: a
Out[84]: 
array([[54, 30, 55, 12],
       [64, 94, 50, 72],
       [67, 31, 56, 43],
       [26, 58, 35, 14],
       [97, 76, 84, 52]])

In [85]: b
Out[85]: array([56, 70, 43, 19, 16])

In [86]: np.concatenate((a,b[:,None]),axis=1)
Out[86]: 
array([[54, 30, 55, 12, 56],
       [64, 94, 50, 72, 70],
       [67, 31, 56, 43, 43],
       [26, 58, 35, 14, 19],
       [97, 76, 84, 52, 16]])

If b is such that its a 1D array of dtype=object with a shape of (1,), most probably all of the data is contained in the only element in it, we need to flatten it out before concatenating. For that purpose, we can use np.concatenate on it too. Here's a sample run to make the point clear -

In [118]: a
Out[118]: 
array([[54, 30, 55, 12],
       [64, 94, 50, 72],
       [67, 31, 56, 43],
       [26, 58, 35, 14],
       [97, 76, 84, 52]])

In [119]: b
Out[119]: array([array([30, 41, 76, 13, 69])], dtype=object)

In [120]: b.shape
Out[120]: (1,)

In [121]: np.concatenate((a,np.concatenate(b)[:,None]),axis=1)
Out[121]: 
array([[54, 30, 55, 12, 30],
       [64, 94, 50, 72, 41],
       [67, 31, 56, 43, 76],
       [26, 58, 35, 14, 13],
       [97, 76, 84, 52, 69]])

How To Concatenate Arrays in NumPy?, NumPy's concatenate function can be used to concatenate two arrays either row-wise or column-wise. Concatenate function can take two or more� numpy.concatenate¶ numpy.concatenate ((a1, a2, ), axis=0, out=None) ¶ Join a sequence of arrays along an existing axis. Parameters a1, a2, … sequence of array_like. The arrays must have the same shape, except in the dimension corresponding to axis (the first, by default). axis int, optional. The axis along which the arrays will be joined.

There's also np.c_

>>> a = np.arange(20).reshape(5, 4)
>>> b = np.arange(-1, -6, -1)
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19]])                                                                                                                                   
>>> b                                                                                                                                                       
array([-1, -2, -3, -4, -5])                                                                                                                                 
>>> np.c_[a, b]
array([[ 0,  1,  2,  3, -1],          
       [ 4,  5,  6,  7, -2],                       
       [ 8,  9, 10, 11, -3],                      
       [12, 13, 14, 15, -4],                                
       [16, 17, 18, 19, -5]])

Concatenating two one-dimensional NumPy arrays, The line should be: numpy.concatenate([a,b]). The arrays you want to concatenate need to passed in as a sequence, not as separate arguments. numpy.concatenate¶ numpy.concatenate In cases where a MaskedArray is expected as input, use the ma.concatenate function from the masked array module instead.

You can do something like this.

import numpy as np

x = np.random.randint(100, size=(5, 4))
y = [16, 15, 12, 12, 17]

print(x)

val = np.concatenate((x,np.reshape(y,(x.shape[0],1))),axis=1)
print(val)

This outputs:

[[32 37 35 53]
 [64 23 95 76]
 [17 76 11 30]
 [35 42  6 80]
 [61 88  7 56]]

[[32 37 35 53 16]
 [64 23 95 76 15]
 [17 76 11 30 12]
 [35 42  6 80 12]
 [61 88  7 56 17]]

NumPy Joining Array, Joining Arrays Using Stack Functions. Stacking is same as concatenation, the only difference is that stacking is done along a new axis. We can concatenate two 1� NumPy concatenate. NumPy’s concatenate function can be used to concatenate two arrays either row-wise or column-wise. Concatenate function can take two or more arrays of the same shape and by default it concatenates row-wise i.e. axis=0.

numpy.concatenate, numpy.concatenate - Concatenation refers to joining. This function is used to join two or more arrays of the same shape along a specified axis. numpy.concatenate - Concatenation refers to joining. This function is used to join two or more arrays of the same shape along a specified axis. The function takes the following par

lagrange.univ-lyon1.fr/docs/numpy/1.11.0/reference, The axis along which the arrays will be joined. Default is 0. Returns: result : MaskedArray. The concatenated array with any masked entries preserved. numpy.concatenate((array1, array2, ), axis) Here array1 and array2 are the arrays that are in use for concatenation. Here axis is an integer value. The default value of axis is 0(rows). You can use axis =1 for manipulate columns. Step 1: Creation of Dummy Numpy Array. Let’s generate the NumPy array that we need to concatenate. This is for

https://docs.scipy.org/doc/numpy-1.15.1/reference/, Python NumPy Array Object Exercises, Practice and Solution: Write a Python program to concatenate two 2-dimensional arrays. NumPy's concatenate() is not like a traditional database join. It is like stacking NumPy arrays. This function can operate both vertically and horizontally. This means we can concatenate arrays together horizontally or vertically. The concatenate() function is usually written as np.concatenate(), but we can also write it as numpy.concatenate().

Comments
  • How the heck is that second array supposed to have shape (1,)? Is there some sort of weird thing with object arrays going on here?
  • That is what i got when i run array2.shape
  • Then your array is seriously messed up in some way, and you need to figure out what's going on.
  • just before that i run this array2 = np.array(np.round(data[:,0]/20))
  • The b[:, None] notation is great, but it might also be worth mentioning b.reshape()
  • @PaulPanzer That's worth a new post! Consider posting it.
  • the shape of the second was because of this array2 = np.array(np.round(data[:,0]/20)) i fixed with array2 = np.array(np.round(data[:,0]/20)).astype(int)
  • Dare you to decode this one: np.r_['1,2,0', a, -1:-6:-1] :)
  • I wonder if np.c_ can always substitute for np.column_stack?
  • @hpaulj Looks like it to me, but I'm not that familiar with np.column_stack. It is basically a 2d concatenate that special cases 1d inputs, right?