Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

numpy

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?

like image 367
RaduS Avatar asked Feb 01 '17 21:02

RaduS


People also ask

How do you stack arrays in Numpy?

stack() function is used to join a sequence of same dimension arrays along a new axis. The axis parameter specifies the index of the new axis in the dimensions of the result. For example, if axis=0 it will be the first dimension and if axis=-1 it will be the last dimension.

What is Numpy R_?

r_ = <numpy.lib.index_tricks.RClass object> Translates slice objects to concatenation along the first axis. This is a simple way to build up arrays quickly. There are two use cases. If the index expression contains comma separated arrays, then stack them along their first axis.


2 Answers

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]]) 
like image 186
Divakar Avatar answered Sep 23 '22 08:09

Divakar


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]]) 
like image 32
Paul Panzer Avatar answered Sep 21 '22 08:09

Paul Panzer