[SciPy-User] [SciPy-user] ValueError: The truth value of an array with more than one element is ambiguous.
Sun Apr 8 09:38:57 CDT 2012
On Sat, Apr 7, 2012 at 4:32 PM, surfcast23 <firstname.lastname@example.org> wrote:
> I am still a little confused as how to use numpy.all() to evaluate all
> three arrays for a specific range vales. I looked at the documentation, but
> did not see any examples that were close to what I need.
Calling `np.all` will just return a single True or False so you can combine
all those return values with `and`. The important part is that you split up
your chained comparisons. So you might write:
if np.all(Xa >= min) and np.all(Xa <=max) and np.all(Ya >= min) and
np.all(Ya <=max) and np.all(Za >= min) and np.all(Za <=max):
Not the most elegant solution, but it works. Alternatively, you could use a
list comprehension to collapse some of this down
if np.all([np.all(a >= min) and np.all(a <=max) for a in (Xa, Ya, Za)]):
The inner calls to `np.all` check that all elements of an array are greater
than min or less than max. The outer call to `np.all` checks that all three
arrays (Xa, Ya, Za) satisfies this requirement.
Probably unnecessary information
In previous replies, I needed to use bitwise-and (i.e. `&`) only when
combining bool arrays; for example, `(a >= min) & (a >= max)`. In the above
examples, I have to call `np.all` on each comparison *before* combining
with `and`. In contrast, the `&`-operator will combine *each* element of
the first comparison with each element of the second. This gives a bool
array, which you'd need to call `np.all` on if you want to put it in an
if np.all([np.all((a >= min) & (a <=max)) for a in (Xa, Ya, Za)]):
Tony Yu-3 wrote:
> > On Fri, Apr 6, 2012 at 12:54 AM, Tony Yu <email@example.com> wrote:
> >> On Thu, Apr 5, 2012 at 6:46 PM, surfcast23 <firstname.lastname@example.org>
> >>> Hi, I have an if statement and what I want it to do is go through
> >>> and find the common elements in all three arrays. When I try the code
> >>> below
> >>> I get this error * ValueError: The truth value of an array with more
> >>> than one element is ambiguous. Use a.any() or a.all()* Can some one
> >>> explain the error to me and how I might be able to fix it. Thanks in
> >>> advance. *if min <= Xa <=max & min <= Ya <=max & min <= Za <=max:
> >>> print("in range") else: print("Not in range")*
> >> This explanation may or may not be clear, but your question is answered
> >> in this
> >> communication<
> >> .
> >> Roughly:
> >> 1) Python's default behavior for chained comparisons don't work as you'd
> >> expect for numpy arrays.
> >> 2) Python doesn't allow numpy to change this default behavior (at least
> >> currently, and maybe
> >> never<
> >> ).
> >> Nevertheless, you can get around this by separating the comparisons
> >> >>> if (min <= Xa) & (Xa <= max):
> >> Note the use of `&` instead of `and`, which is at the heart of the
> >> issue<http://www.python.org/dev/peps/pep-0335/>
> >> .
> >> Hope that helps,
> >> -Tony
> > Oops, I think I got myself mixed up in the explanation. Separating the
> > comparisons fixes one error; For example, the following:
> >>>> (min <= Xa) & (Xa <= max)
> > will return an array of bools instead of raising an error (as you would
> > get
> > with `min <= Xa <= max`). This is what I meant to explain above.
> > But, throwing an `if` in front of that comparison still doesn't work
> > because it's ambiguous: Should `np.array([True False])` be true or false?
> > Instead you should check `np.all(np.array([True False]))`, which
> > as False since not-all elements are True, or `np.any(np.array([True
> > False]))`, which evaluates as True since one element is True.
> > -Tony
> > _______________________________________________
> > SciPy-User mailing list
> > SciPy-User@scipy.org
> > http://mail.scipy.org/mailman/listinfo/scipy-user
> View this message in context:
> Sent from the Scipy-User mailing list archive at Nabble.com.
> SciPy-User mailing list
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the SciPy-User