John | 6 Oct 21:29

multi-comparison expressions

hi,

why does the ValueError appear below, and how can i make that 2<a<5 
expression work when a is an array?

thanks.

 >>> from numpy import reshape,arange
 >>> a=reshape(arange(9),(3,3))
 >>> a
array([[0, 1, 2],
      [3, 4, 5],
      [6, 7, 8]])
 >>> 2<a
array([[False, False, False],
      [ True,  True,  True],
      [ True,  True,  True]], dtype=bool)
 >>> a<5
array([[ True,  True,  True],
      [ True,  True, False],
      [False, False, False]], dtype=bool)
 >>> 2<a<5
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is 
ambiguous. Use a.any() or a.all()

nor does it work with constant arrays:

 >>> from numpy import zeros
(Continue reading)

Nadav Horesh | 6 Oct 21:49
Favicon

Re: multi-comparison expressions

a < b < c 

is equivalent to:

(a < b) and (b < c)

If you wand an element-wise operation you have to use the & operator, so the expression is:

(a > 2) & (a < 5)

   Nadav

-----הודעה מקורית-----
מאת: numpy-discussion-bounces <at> scipy.org בשם John
נשלח: ב 06-אוקטובר-08 21:32
אל: numpy-discussion <at> scipy.org
נושא: [Numpy-discussion] multi-comparison expressions

hi,

why does the ValueError appear below, and how can i make that 2<a<5 
expression work when a is an array?

thanks.

 >>> from numpy import reshape,arange
 >>> a=reshape(arange(9),(3,3))
 >>> a
array([[0, 1, 2],
      [3, 4, 5],
(Continue reading)

Robert Kern | 6 Oct 21:59

Re: multi-comparison expressions

On Mon, Oct 6, 2008 at 14:32, John <john <at> saponara.net> wrote:
> hi,
>
> why does the ValueError appear below, and how can i make that 2<a<5
> expression work when a is an array?

(2<a<5) evaluates to ((2<a) and (a<5)). The "and" keyword coerces its
arguments to actual boolean True or False objects, so this is actually
(bool(2<a) and bool(a<5)). Arrays implement rich comparisons, so if
'a' is an array, then (2<a) is an array of bools. bool(some_array) is
ambiguous; sometimes you want it to be True if any of the elements are
True and sometimes you only want it to be True if all of the elements
are True. Consequently, numpy refuses to guess what you want and makes
you specify with the .any() and .all() methods. Unfortunately, this
means that (2<a<5) will never work for arrays.

--

-- 
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
Charles R Harris | 6 Oct 22:58

Re: multi-comparison expressions



On Mon, Oct 6, 2008 at 1:59 PM, Robert Kern <robert.kern <at> gmail.com> wrote:
On Mon, Oct 6, 2008 at 14:32, John <john <at> saponara.net> wrote:
> hi,
>
> why does the ValueError appear below, and how can i make that 2<a<5
> expression work when a is an array?

(2<a<5) evaluates to ((2<a) and (a<5)). The "and" keyword coerces its
arguments to actual boolean True or False objects, so this is actually
(bool(2<a) and bool(a<5)). Arrays implement rich comparisons, so if
'a' is an array, then (2<a) is an array of bools. bool(some_array) is
ambiguous; sometimes you want it to be True if any of the elements are
True and sometimes you only want it to be True if all of the elements
are True. Consequently, numpy refuses to guess what you want and makes
you specify with the .any() and .all() methods. Unfortunately, this
means that (2<a<5) will never work for arrays.

This reminds me that if we add matrix operators to python it would also be nice to have some for the logical operators.  Having && and || would be handy.

Chuck


_______________________________________________
Numpy-discussion mailing list
Numpy-discussion <at> scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Christopher Barker | 6 Oct 22:02
Favicon

Re: multi-comparison expressions

John wrote:
> hi,
> 
> why does the ValueError appear below,

because python short circuits this expression, and numpy can't overwrite 
that -- the same reason a and b doesn't work for a and b arrays.

 > and how can i make that 2<a<5
> expression work when a is an array?

 >>> (a > 2) & (a < 5)

array([[False, False, False],
        [ True,  True, False],
        [False, False, False]], dtype=bool)

& is the bitwise-and, which is overridden by numpy, and works like and 
for boolean data.

-Chris

--

-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker <at> noaa.gov

Gmane