1

Given two numpy array a1 and a2:

>>>np.shape(a1)
(4465, 5000)
>>>np.shape(a2)
(4465, )

However,

>>>np.concatenate((a1, a2), axis=1)
ValueError: all the input arrays must have the same number of dimensions

And I also tried:

np.concatenate((a1, a2), axis=1),
np.concatenate((a1, a2.T), axis=0),
np.concatenate((a1, a2.T), axis=1)

But also got the same error.

Could you please tell me what's wrong with my code? Thanks!

3
  • What output are you expecting? An array of length 4465 by 5001? Commented Jun 6, 2016 at 7:29
  • @EdSmith Yes! That's what I want Commented Jun 6, 2016 at 7:31
  • 1
    @Alex Totally different. I didn't lost the parentheses. Just remove the duplicate. Commented Jun 6, 2016 at 7:31

1 Answer 1

2

As the error message states, a1 and a2 do not have the same number of dimensions (accessible via attribute ndims). Make a2 2-dimensional with a2 = a2[:, None]. You can also use the more explicit syntax a2 = a2[:, np.newaxis], but it is strictly equivalent.

Sign up to request clarification or add additional context in comments.

3 Comments

a2 = a2[:, np.newaxis] works but a2 = a2[:, None] doesn't. Why??
Strange, which version of python/numpy as you using? You could also use a2 = np.expand_dims(a2, 1) or a2 = np.atleast_2d(a2).T
@ZICHAOLI try to avoid "doesn't work" on SO : do you get an error message or do you get unexpected results ? In both case, include them

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.