[Numpy-discussion] improved NumPy support for boolean arrays?
Robert Kern
robert.kern@gmail....
Wed Aug 5 16:11:39 CDT 2009
On Wed, Aug 5, 2009 at 16:02, Sturla Molden<sturla@molden.no> wrote:
>
>> If x and y are numpy
>> arrays of bools, I'd like to be able to create expressions like the
>> following:
>>
>> not x (to invert each element of x)
>> x and y
>> x or y
>> x xor y
>> (not x) or y
>>
>> The usual array broadcasting rules should apply. Is there any chance of
>> getting something like this into NumPy?
> There is a reason for this related to Python. In Python an object will
> often have a boolean truth value. How would you cast an ndarray to bool?
> If you write something like (x and y) the Python interpreter expects
> this to evaluate to True or False. Thus is cannot evaluate to an ndarray
> with booleans. NumPy cannot change the syntax of Python.
>
> Another thing: An empty list evaluates to False in a boolean context,
> whereas a non-empty list evaluates to True. ndarrays behave differently.
> Why?
Numeric used to evaluate bool(some_array) as True if any of the
elements were nonzero and False if all of them were zero. This
confused some people who expected that bool(some_array) to be True iff
*all* of the elements were nonzero and False otherwise. People had
bugs in their code for years without realizing it. They would try one
example, get their expected result, and not test the other corner
cases that would demonstrate that their mental model of what was going
on was incorrect.
By the time that numarray was being designed, the numarray team
decided to make array always raise an exception instead of returning
any truth value. numpy followed this decision.
There really aren't many use cases for following the list object's
semantics with arrays. Empty arrays aren't nearly as common as empty
lists or even tuples. I know of no case where it is useful to test
specifically for emptiness versus non-emptiness. In any case, directly
checking the .size or .shape attributes would be sufficient and far
more clear because there are other plausible interpretations of
bool(some_array) like Numeric's.
--
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