CentauriAurelius
CentauriAurelius

Reputation: 504

Stack a list of numpy arrays

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

Upvotes: 0

Views: 712

Answers (2)

Aly Hosny
Aly Hosny

Reputation: 827

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)

Upvotes: 1

Dhaval Taunk
Dhaval Taunk

Reputation: 1672

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)

Upvotes: 2

Related Questions