[Numpy-discussion] Simple broadcasting? Or not so simple?? [SEC=UNCLASSIFIED]
Ross.Wilson@ga.g...
Ross.Wilson@ga.g...
Sun Nov 14 21:55:30 CST 2010
Dear list,
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([[[1],[2],[3]]])
b = np.array([[[4],[5],[6]]])
x = np.array([[[1],[1],[2]]])
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([[[1]]]) 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,
Ross Wilson
More information about the NumPy-Discussion
mailing list