-1

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
  • 2
    If np.array(X) does not give you a 3d array, I suspect one of more of the 2d arrays differs in size. You might try np.stack(X), but it too expects the shapes to match. If they just differ in the R shape, vstack should work, but the N*R dimension is doubtful. You could though reshape the vstack result. Commented Jun 6, 2019 at 23:55
  • 1
    Thanks the issue was indeed that. Once I fixed that issue, I was good. Commented Jun 7, 2019 at 0:05

2 Answers 2

0

You can use np.stack:

test = np.stack([np.ones([2, 3]) for _ in range(4)]) 
print(test.shape) # (4, 2, 3)
Sign up to request clarification or add additional context in comments.

Comments

0

you could just use :

np.array([np.array(x) for x in ArrayList])

1 Comment

If 2DarrayList is already a list of numpy arrays, you don't need the list comp. Note: 2DarrayList is not a valid variable name

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.