2014-11-04 64 views
0

我在创建一个numpy数组时遇到了问题。我有一个具有某种形状的numpy数组,我试图创建一个具有形状的新数组(2,other_array_shape)。例如:指定一个numpy数组

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

但是,这会返回无效的语法错误。有人能告诉我如何实现这一目标吗?

回答

4

你只需要连接两个元组:

>>> arr = np.zeros((2,) + x.shape) 
>>> arr.shape 
(2, 100, 100) 
2

您需要添加的元组:

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