我是numpy廣播的newby。我定義三個numpy的陣列如下:Python,numpy:3維廣播
from numpy import *
a=array([10,20]).reshape(2,1)
b=array([100,200,300]).reshape(1,3)
c=arange(1,11).reshape(1,1,10)
a + b爲一個(2,1)與(1,3)之和,因此被認爲是broadcastable(2vs1在昏暗1,1VS3在昏暗2,廣播規則被滿足)。事實上,這是:
>>> a+b
array([[110, 210, 310],
[120, 220, 320]])
A + C是一(2,1)與(1,1,10)總和所以它應該是broadcastable(2vs1在昏暗1,1VS1在昏暗2和1vs10在暗淡3,廣播規則被實現)。事實上,這是:
>>> a+c
array([[[11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
[21, 22, 23, 24, 25, 26, 27, 28, 29, 30]]])
B + C是一(1,3)與(1,1,10)總和所以它應該是broadcastable(1VS1在昏暗1,3VS1在昏暗2,1vs10在3.暗淡,但現在看來,這是不是:
>>> b+c
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: shape mismatch: objects cannot be broadcast to a single shape
的解釋是certainely明顯...但是請大家幫我
非常感謝!我知道'從numpy導入*',我用它來清楚,但你是對的,這應該是無處不在。 – 2012-04-17 09:37:41