[Numpy-discussion] masked index surprise
Fri Aug 14 13:52:30 CDT 2009
On Fri, Aug 14, 2009 at 13:05, John Hunter<email@example.com> wrote:
> I just tracked down a subtle bug in my code, which is equivalent to
> In : x, y = np.random.rand(2, n)
> In : z = np.zeros_like(x)
> In : mask = x>0.5
> In : z[mask] = x/y
> I meant to write
> z[mask] = x[mask]/y[mask]
> so I can fix my code, but why is line 67 allowed
> In : z[mask].shape
> Out: (54,)
> In : (x/y).shape
> Out: (100,)
> it seems like broadcasting would fail
Broadcasting doesn't take place with boolean masks. Instead, the
values repeat if there are too few and extra values are ignored.
Boolean indexing derives from Numeric's putmask() implementation,
which had these semantics, rather than other forms of indexing.
You may consider this a wart or a bad design decision (and I would
probably agree), but it is not a bug.
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the NumPy-Discussion