Numpy concatenate 2D arrays with 1D array

31,044

Solution 1

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)

Solution 2

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]])

Solution 3

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).

Share:
31,044
KCDC
Author by

KCDC

Updated on July 09, 2022

Comments

  • KCDC
    KCDC almost 2 years

    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!

  • Imanol Luengo
    Imanol Luengo almost 9 years
    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).
  • kRazzy R
    kRazzy R about 6 years
  • kRazzy R
    kRazzy R about 6 years
  • kRazzy R
    kRazzy R about 6 years
    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.
  • Ben Farmer
    Ben Farmer about 6 years
    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.
  • deadcode
    deadcode about 5 years
    np.newaxis is intuitive but I still can't understand why A[:, None] works. Can anyone help me understand this?
  • Falko
    Falko about 5 years
    It works because "newaxis is an alias for None" 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.