0

I have a list of numpy arrays that are of the following shapes:

(16, 250, 2)
(7, 250, 2)
(1, 250, 2)

I'm trying to stack them all together so they are a single numpy array of shape :

(24, 250, 2)

I tried using np.stack but I get the error:

ValueError: all input arrays must have the same shape
2
  • 1
    Try concatenate with axis=0. Commented May 28, 2020 at 6:47
  • perfect, thank you very much. Commented May 28, 2020 at 6:49

2 Answers 2

2

You can use np.concatenate: You will get this:-

a = np.random.rand(16,250,2)
b = np.random.rand(7,250,2)
c = np.random.rand(1,250,2)
print(np.shape(np.concatenate([a,b,c], axis=0))

Output:

(24,250,2)
Sign up to request clarification or add additional context in comments.

Comments

1

The trick is to use the right stacking method, in your case since you are stacking vertically you should use np.vstack

import numpy as np

a = np.random.random((16, 250, 2))
b = np.random.random((7, 250, 2))
c = np.random.random((1, 250, 2))

arr = np.vstack((a,b,c))
arr.shape
(24, 250, 2)

1 Comment

this also works for my list, if i do arr = np.vstack(mylist)

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.