41

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!

3 Answers 3

39

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)
Sign up to request clarification or add additional context in comments.

4 Comments

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).
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.
np.newaxis is intuitive but I still can't understand why A[:, None] works. Can anyone help me understand this?
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.
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]])

2 Comments

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

2 Comments

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.