[Numpy-discussion] masked index surprise
Fri Aug 14 14:24:15 CDT 2009
On Fri, Aug 14, 2009 at 14:20, Keith Goodman<email@example.com> wrote:
> On Fri, Aug 14, 2009 at 11:52 AM, Robert Kern<firstname.lastname@example.org> wrote:
>> 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.
> Are the last two, x[] and x[np.array()], broadcasting?
>>> x = np.array([1,2,3])
>>> x = np.array([4,5,6])
> ValueError: setting an array element with a sequence.
>>> x[(1,)] = np.array([4,5,6])
> ValueError: array dimensions are not compatible for copy
>>> x[] = np.array([4,5,6])
> array([1, 4, 3])
>>> x[np.array()] = np.array([4,5,6])
> array([1, 4, 3])
I guess I'm just makin' stuff up again. kern_is_right() == False. All
forms repeat, not broadcast, since they derive from put() and
putmask() which both have the repeating/ignoring semantics.
"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