81
x = np.arange(0,2,0.5)
valeur = 2*x

if valeur <= 0.6:
    print ("this works")
else:   
    print ("valeur is too high")

here is the error I get:

if valeur <= 0.6:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I have read several posts about a.any() or a.all() but still can't find a way that really clearly explain how to fix the problem. I see why Python does not like what I wrote but I am not sure how to fix it.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
Moe Steen
  • 819
  • 1
  • 6
  • 3
  • 7
    `valeur` is a vector with some elements smaller than 0.6 and some larger. What do you mean with `valeur <= 0.6`? – Daniel Dec 26 '15 at 15:52
  • valeur is a vector equal to [ 0. 1. 2. 3.] I am interested in each single term. For the part below 0.6, then return "this works". For the part above 0.6, return "valeur is too high" this is a dumb down version, where instead of Print this or Print that based on the condition, I am feeding each element of Valeur into equation 1 or equation 2 depending on condition – Moe Steen Dec 26 '15 at 15:55
  • 2
    comparing a single value to a vector is like comparing oranges and apples, `any()` and `all()` are functions that verify if that condition is satisfied by any of the elements in the vector, or all of the elements satisfy the condition – saljuama Dec 26 '15 at 15:58

7 Answers7

104

If you take a look at the result of valeur <= 0.6, you can see what’s causing this ambiguity:

>>> valeur <= 0.6
array([ True, False, False, False], dtype=bool)

So the result is another array that has in this case 4 boolean values. Now what should the result be? Should the condition be true when one value is true? Should the condition be true only when all values are true?

That’s exactly what numpy.any and numpy.all do. The former requires at least one true value, the latter requires that all values are true:

>>> np.any(valeur <= 0.6)
True
>>> np.all(valeur <= 0.6)
False
poke
  • 369,085
  • 72
  • 557
  • 602
13

There is one more way you can get this

import numpy as np

a = np.array([1,2,3,4])
b = np.array([5,6,7,8])
c = np.array([1,2,3,4])

print((a == b ).all())  #False
print((a == c ).all())   # True
print((a == b ).any())   #False
print((a == c ).any())   #True
print((a > 3 ).all())    #False
Prajot Kuvalekar
  • 5,128
  • 3
  • 21
  • 32
4

You comment:

valeur is a vector equal to [ 0. 1. 2. 3.] I am interested in each single term. For the part below 0.6, then return "this works"....

If you are interested in each term, then write the code so it deals with each. For example.

for b in valeur<=0.6:
    if b:
        print ("this works")
    else:   
        print ("valeur is too high")

This will write 2 lines.

The error is produced by numpy code when you try to use it a context that expects a single, scalar, value. if b:... can only do one thing. It does not, by itself, iterate through the elements of b doing a different thing for each.

You could also cast that iteration as list comprehension, e.g.

['yes' if b else 'no' for b in np.array([True, False, True])]
hpaulj
  • 221,503
  • 14
  • 230
  • 353
1
import numpy as np
x = np.arange(0,2,0.5)
valeur = 2*x
print(valeur <= 0.6)
'''# [ True False False False] due to this "if statement" cannot decide if the 
value is true or false
 #This is where you got ValueError of ambiguity
 #This error recomended you to use a.any() or a.all() built-in functions
 #a.any() :- any True is True i.e. ([ True False False False].any) will be 
True/1
 #a.all() :- all Ture is Ture i.e. ([ True False False False].all) will be 
False/0'''

if valeur.all() <= 0.6:
'''# Now in "if statement" valeur.all() is Flase/0
# so is 0 <= 0.6? 
# Ture right? '''
    print ("this works")
'''# This is the reason "this works"'''
else:   
    print ("valeur is too high")
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 25 '22 at 18:42
0

This should also work and is a closer answer to what is asked in the question:

for i in range(len(x)):
    if valeur.item(i) <= 0.6:
        print ("this works")
    else:   
        print ("valeur is too high")
Gursel Karacor
  • 1,137
  • 11
  • 21
0

if you want do something for each element:

use numpy's function

import numpy as np
def temp(x):
    if x <= 0.6:
        print ("this works")
    else:   
        print ("valeur is too high")
arr = np.arange(0,1,0.1)
np.vectorize(temp)(arr)
AsukaMinato
  • 1,017
  • 12
  • 21
0

For this specific pattern (mapping elements to one value if they meet a certain condition, else mapping them to another value), you can use np.where():

import numpy as np

x = np.arange(0,2,0.5)
valeur = 2*x

print(*np.where(2 * x <= 0.6, "this works", "valeur is too high"), sep='\n')

This outputs:

this works
valeur is too high
valeur is too high
valeur is too high
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33