[Numpy-discussion] Simple broadcasting? Or not so simple?? [SEC=UNCLASSIFIED]
Sun Nov 14 21:55:30 CST 2010
I thought I understood broadcasting, but now I'm not so sure.
I've simplified as much as I can, so here goes. I have two input arrays of shape (1, 3, 1). I want to select elements from one or other of the input arrays depending on whether the corresponding element of a third array exceeds a threshold. My simplest code is:
import numpy as np
a = np.array([[,,]])
b = np.array([[,,]])
x = np.array([[,,]])
result = np.where(x > 1.5, a, b)
and works as expected.
Now, my understanding of broadcasting is that if the 'x' array is defined as np.array([[]]) then broadcasting will ensure the result array will contain elements from array 'b'. That is, the program will behave as if 'x' had shape of (1,3,1) with three elements each of value 1. I tested that and got the result I expected.
However, when I ran the test on another machine, it failed with an "array dimensions must agree" error. On the failing machine numpy.__version__ returns '1.2.0'. Machines on which the broadcasting works as I expect I see '1.3.0' (or later) in numpy.__version__.
Have broadcast rules changed since 1.2.0? Or maybe I just don't understand broadcasting?
Thanks in advance,
More information about the NumPy-Discussion