0

I have two arrays

>>> array1.shape
(97, 195)
>>> array2.shape
(195,)
>>> array1 = numpy.concatenate((array1, array2), axis=0)

when I perform concatenate operation it shows an error

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

is that the second array shape (195,) creating problem?

5
  • 1
    Transpose second array. From docs.scipy: The arrays must have the same shape, except in the dimension corresponding to axis (the first, by default). It should be (1,195). Than you can concatenate over 2nd dimension, obv Commented Jul 16, 2014 at 11:32
  • 1
    That's not a transpose, but you do want to reshape the array to (1, 195). Commented Jul 16, 2014 at 11:34
  • Excuse me? Does (195,) array have size 195*0? Commented Jul 16, 2014 at 11:48
  • 1
    @s0upa1t: It's not a row vector or a column vector; it's 1-dimensional. Ask NumPy to transpose it (with transpose or T), and you'll find no change. Commented Jul 16, 2014 at 20:24
  • @user2357112, just checked. You are right. Thanks! Commented Jul 17, 2014 at 5:55

3 Answers 3

3

Just make both have the same dimensions and the same size except along the axis to be concatenated:

np.concatenate((array1, array2[np.newaxis,...]), axis=0)
Sign up to request clarification or add additional context in comments.

Comments

1

In order for this to work, you need array2 to actually be 2d.

array1 = numpy.concatenate((array1, array2.reshape((1,195)))

should work

Comments

0

Another easy way to achieve the array concatenation that you’re looking for is to use Numpy’s vstack function as follows:

array1 = np.vstack([array1, array2])

Comments

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.