. Advertisement .
..3..
. Advertisement .
..4..
You may see the message “ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()” the first time you try to write an if statement involving a NumPy array. It is a common error, which this tutorial will explain and show you how to avoid.
“ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()”
Where Does The Error Come From?
When you use NumPy arrays in a conditional expressions, a ValueError exception will be raised like this:
>>> import numpy as np
>>> x = np.arange(5)y
>>> x
array([0, 1, 2, 3, 4])
>>> x < 3
array([ True, True, True, False, False])
>>> if x < 3: print("smaller")
...
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()
In the particular snippet above, the expression is evaluated as a bool value. But when you compare a NumPy array with an integer in such a fashion, the module can’t decide which is your intention.
Normally, the bool() function, which converts a value to a boolean value, is compatible with regular lists. A False value will be given if the list has no member and True otherwise.
Example:
>>> bool([1, 1, 1, 1, 1])
True
>>> bool([0, 0, 0, 0, 0])
True
>>> bool([])
False
However, it doesn’t work that way with NumPy arrays. It will produce the error we have seen when you try to convert it to a boolean value using the bool() function.
>>> bool(x)
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()
The developers don’t assume what you mean by calling this conversion. Perhaps you want a True value only when all the NumPy array’s elements are true, but some also expect bool() to return True even when one element is True.
This is also the case with bitwise operators like and, or, and not. You are seeing the previous error for the same reason.
>>> x
array([0, 1, 2, 3, 4])
>>> type(x)
<class 'numpy.ndarray'>
>>> y
array([4, 5, 2, 5, 3])
>>> type(y)
<class 'numpy.ndarray'>
>>> x and y
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()
>>> x or y
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()
>>> not x
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()
How To Fix The Issue
Like the error message suggests, you can use the any() and all() methods of the NumPy array object type to avoid the issue. While all() only returns True when all the elements are True, any() returns it when any of them evaluates to True. Note: remember to update NumPy first.
>>> x
array([0, 1, 2, 3, 4])
>>> type(x)
<class 'numpy.ndarray'>
>>> x.any()
True
>>> x.all()
False
For element-wise operations, you can use &, |, and ~ operators in place of and, or, and not.
>>> x
array([0, 1, 2, 3, 4])
>>> type(x)
<class 'numpy.ndarray'>
>>> y
array([4, 5, 2, 5, 3])
>>> type(y)
<class 'numpy.ndarray'>
>>> x & y
array([0, 1, 2, 1, 0])
>>> x | y
array([4, 5, 2, 7, 7])
>>> ~x
array([-1, -2, -3, -4, -5])
Conclusion
The message “ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()” appears when you use a NumPy array in a Python conditional expression. Using the all() and any() methods should solve this program right away.
Leave a comment