How do I combine N, 2D numpy arrays (of dimension R x C) to create a 3D numpy array of shape (N, R, C)? Right now, the N-2D numpy arrays are contained inside a list, and I want that to become a 3D numpy array. Let's say X is my list of 2D numpy arrays, if I just do np.array(X), I get something of shape (N,). If I do np.vstack(X), I get something of shape (N x R, C). How do I solve this problem?
2 Answers
You can use np.stack:
test = np.stack([np.ones([2, 3]) for _ in range(4)])
print(test.shape) # (4, 2, 3)
Comments
you could just use :
np.array([np.array(x) for x in ArrayList])
1 Comment
Code-Apprentice
If
2DarrayList is already a list of numpy arrays, you don't need the list comp. Note: 2DarrayList is not a valid variable name
np.array(X)does not give you a 3d array, I suspect one of more of the 2d arrays differs in size. You might trynp.stack(X), but it too expects the shapes to match. If they just differ in the R shape,vstackshould work, but theN*Rdimension is doubtful. You could though reshape thevstackresult.