2014-11-04 94 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)