0

I am having trouble creating a numpy array. I have a numpy array with some shape and I am trying to create a new array with the shape (2, other_array_shape). Something like:

import numpy as np
x = np.zeros((100, 100))
y = np.zeros((2, i for i in x.shape))

However, this comes back with invalid syntax error. Can someone tell me how to achieve this?

2 Answers 2

4

You simply need to concatenate two tuples:

>>> arr = np.zeros((2,) +  x.shape)
>>> arr.shape
(2, 100, 100)
Sign up to request clarification or add additional context in comments.

Comments

2

You need to append the tuple:

import numpy as np
x = np.zeros((100, 100))
y = np.zeros((2,) + x.shape)

Comments

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.