u3728666
u3728666

Reputation: 119

Fail to concatenate two numpy array

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!

Upvotes: 1

Views: 94

Answers (1)

P. Camilleri
P. Camilleri

Reputation: 13218

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.

Upvotes: 2

Related Questions