ValueError: the truth value of an array with more than one element is ambiguous. use a.any() or a.all()

Total
0
Shares
Table of Contents Hide
  1. Live Demo
    1. Related Posts

Python numpy throws valueerror: the truth value of an array with more than one element is ambiguous. use a.any() or a.all(), when an array is compared using some boolean form. You can understand this properly with example. Consider this code –

>>> import numpy as np
>>> superHeroArr = np.array(["Thor", "Hulk", "Flash"])
>>> bool(arr)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

The problem with this code is that the numpy array has more than one element and its ambiguous to tell the boolean value of whole array.

If you compare an array with some value then it returns the array of bool. For example –

>>> avengerMoviesDates = np.array([2012, 2015, 2018, 2019])
>>> moviesBefore2017 = avengerMoviesDates < 2017
>>> print(moviesBefore2017)
[True True False False]

In this code we have declared a numpy array, avengerMoviesDates containing releasing years of all avengers movies. In the next step, we are comparing the whole avengerMoviesDates array with 2017 so that we can get the movies which are released before 2017. This will return an array of boolean values.

Suppose, you are trying to compare two values of numpy array using and operator then it will work fine. Check this out –

>>> avengerMoviesDates = np.array([2012, 2015, 2018, 2019])
>>> print(avengerMoviesDates[0] < 2014 and avengerMoviesDates[1] > 2014)
True

But in some way if array is multidimensional and the values you are comparing are not single value, else an array then again it will throw error. Like this code –

import numpy as np
avengerMoviesDates = np.array([[2012, 2015], [2018, 2019]])
print(avengerMoviesDates[0] < 2017 and avengerMoviesDates[1] > 2017)

Traceback (most recent call last):
  File "superhero.py", line 3, in <module>
    print(avengerMoviesDates[0] < 2017 and avengerMoviesDates[1] > 2017)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

The only difference between this code and the previous one, is that we have combined [2012, 2015] in one array and [2018, 2019] in another array. So, avengerMoviesDates[0] will not return 2012, else it will return [2012, 2015]. Another difference is that we are now comparing with 2017 and not 2014.

To deal with such cases, Python provide any() and all() functions along with logical_and() and logical_or(). This code will show the output of all the discussed functions –

Using any() and and()

>>> avengerMoviesDates = np.array([[2012, 2015], [2018, 2019]])
>>> print(((avengerMoviesDates[0] < 2017) - (avengerMoviesDates[1] > 2017)).any())
>>> print(((avengerMoviesDates[0] < 2017) - (avengerMoviesDates[1] > 2017)).all())
True
True

But, numpy boolean subtract is deprecated so we can’t use -, we need to use logical_and and logical_or.

Using logical_and() and logical_or()

>>> avengerMoviesDates = np.array([[2012, 2015], [2018, 2019]])
>>> print(np.logical_and(avengerMoviesDates[0] < 2017, avengerMoviesDates[1] > 2017).any())
>>> print(np.logical_or(avengerMoviesDates[0] < 2017, avengerMoviesDates[1] > 2017).all())
True
True

    Tweet this to help others

Live Demo

Open Live Demo