39

I am trying to concatenate 4 arrays, one 1D array of shape (78427,) and 3 2D array of shape (78427, 375/81/103). Basically this are 4 arrays with features for 78427 images, in which the 1D array only has 1 value for each image.

I tried concatenating the arrays as follows:

>>> print X_Cscores.shape
(78427, 375)
>>> print X_Mscores.shape
(78427, 81)
>>> print X_Tscores.shape
(78427, 103)
>>> print X_Yscores.shape
(78427,)
>>> np.concatenate((X_Cscores, X_Mscores, X_Tscores, X_Yscores), axis=1)

This results in the following error:

Traceback (most recent call last): File "", line 1, in ValueError: all the input arrays must have same number of dimensions

The problem seems to be the 1D array, but I can't really see why (it also has 78427 values). I tried to transpose the 1D array before concatenating it, but that also didn't work.

Any help on what's the right method to concatenate these arrays would be appreciated!

KCDC
  • 666
  • 2
  • 7
  • 13

3 Answers3

34

Try concatenating X_Yscores[:, None] (or X_Yscores[:, np.newaxis] as imaluengo suggests). This creates a 2D array out of a 1D array.

Example:

A = np.array([1, 2, 3])
print A.shape
print A[:, None].shape

Output:

(3,)
(3,1)
Falko
  • 17,076
  • 13
  • 60
  • 105
  • 6
    Just to point out that `A[:, np.newaxis]` has the same behaviour than `A[:, None]` and can sometimes be more intuitive (actually `np.newaxis == None`). – Imanol Luengo May 18 '15 at 14:17
  • however this is true only if both have same dimension. In most cases I am ending up with Array A having shape (8400,) and Array B having shape (8399, 21). How do I truncate/delete the last few rows of A so that both A and B have same shapes like (8399,) and (8399, 21) . Please advise. – kRazzy R Feb 08 '18 at 17:33
  • `np.newaxis` is intuitive but I still can't understand why `A[:, None]` works. Can anyone help me understand this? – deadcode Apr 11 '19 at 08:40
  • It works because ["`newaxis` is an alias for `None`"](https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.indexing.html#numpy.newaxis) and using `None` for indexing tells NumPy to add a dimension. So the 1D array is converted into a 2D array, which has axes 0 and 1. – Falko Apr 11 '19 at 10:59
17

I am not sure if you want something like:

a = np.array( [ [1,2],[3,4] ] )
b = np.array( [ 5,6 ] )

c = a.ravel()
con = np.concatenate( (c,b ) )

array([1, 2, 3, 4, 5, 6])

OR

np.column_stack( (a,b) )

array([[1, 2, 5],
       [3, 4, 6]])

np.row_stack( (a,b) )

array([[1, 2],
       [3, 4],
       [5, 6]])
George
  • 5,808
  • 15
  • 83
  • 160
  • any thoughts on this : https://stackoverflow.com/questions/48676461/concatenate-combine-mx1-numpy-array-with-mxn-numpy-array – kRazzy R Feb 08 '18 at 02:59
  • 1
    Interesting. `np.vstack( (a,b) )` works just like row_stack, but `np.stack( (a,b) )` fails with "ValueError: all input arrays must have the same shape", and `np.hstack( (a,b) )` fails with "ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)" – Dave X Mar 31 '23 at 14:01
6

You can try this one-liner:

concat = numpy.hstack([a.reshape(dim,-1) for a in [Cscores, Mscores, Tscores, Yscores]])

The "secret" here is to reshape using the known, common dimension in one axis, and -1 for the other, and it automatically matches the size (creating a new axis if needed).

heltonbiker
  • 26,657
  • 28
  • 137
  • 252
  • Could help here : https://stackoverflow.com/questions/48676461/concatenate-combine-mx1-numpy-array-with-mxn-numpy-array – kRazzy R Feb 08 '18 at 02:59
  • A generalisation: np.concatenate([a.reshape(*shape,-1) for a in my_arrays],axis=-1), where 'shape' is the shape of known dimensions except the last. – Ben Farmer Apr 16 '18 at 14:41