Skip to content Skip to sidebar Skip to footer

Numpy: Valueerror: The Truth Value Of An Array With More Than One Element Is Ambiguous. Use A.any() Or A.all()

I've been trying to use numpy on Python to plot some data. However I'm getting an error I don't understand: ValueError: The truth value of an array with more than one element is a

Solution 1:

Either z or z_tbl[i+1] is a numpy array. For numpy arrays, rich comparisons (==, <=, >=, ...) return another (boolean) numpy array.

bool on a numpy array will give you the exception that you are seeing:

>>> a = np.arange(10)
>>> a == 1
array([False,  True, False, False, False, False, False, False, False, False], dtype=bool)
>>> bool(a == 1)
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()

Numpy is trying to tell you what to do:

>>> (a == 1).any()  # at least one element is true?True>>> (a == 1).all()  # all of the elements are true?False

Solution 2:

For the if condition use this

if (z <= z_tbl.item(i+1)):

Post a Comment for "Numpy: Valueerror: The Truth Value Of An Array With More Than One Element Is Ambiguous. Use A.any() Or A.all()"