Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

python - ValueError: all the input array dimensions for the concatenation axis must match exactl

I have a numpy array with shape (84, 13, 1036800) and I want to add a 1d array with len 1036800 to it so that the shape becomes (84, 14, 1036800). I've tried to concatenate and the stack options but keep getting this dimension error.

question from:https://stackoverflow.com/questions/66054977/valueerror-all-the-input-array-dimensions-for-the-concatenation-axis-must-match

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use np.concatenate but you should broadcast 1d array first, to multiply first axis with 84 times to accomodate shapes.

a = np.random.randn(84, 13, 1036800)
b = np.random.randn(1036800)
print(a.shape, b.shape)
>>>
(84, 13, 1036800) (1036800,)

b_broadcasted = np.broadcast_to(b, [a.shape[0], 1, a.shape[2]])
print(b_broadcasted.shape)
>>>
(84, 1, 1036800)

np.concatenate([a, b_broadcasted], axis=1).shape
>>>
(84, 14, 1036800)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...