[Numpy-discussion] masked index surprise
Robert Kern
robert.kern@gmail....
Fri Aug 14 13:52:30 CDT 2009
On Fri, Aug 14, 2009 at 13:05, John Hunter<jdh2358@gmail.com> wrote:
> I just tracked down a subtle bug in my code, which is equivalent to
>
>
> In [64]: x, y = np.random.rand(2, n)
>
> In [65]: z = np.zeros_like(x)
>
> In [66]: mask = x>0.5
>
> In [67]: 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 [68]: z[mask].shape
> Out[68]: (54,)
>
> In [69]: (x/y).shape
> Out[69]: (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.
--
Robert Kern
"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
mailing list