I have a function defined in Python that works correctly for scalar values:
>>> def signo(x):
... if x < 0:
... return -1
... elif x > 0:
... return 1
... else:
... return 0
...
>>> signo(10)
1
>>> signo(-5)
-1
But when I pass it an array from NumPy, it fails with a ValueError
:
>>> import numpy as np
>>> t = np.arange(-5, 5)
>>> t
array([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4])
>>> signo(t)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in signo
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Why does this happen?
In this case the error message clearly explains the problem: "the truth value of an array of more than one element is ambiguous". At the moment Python has to evaluate this conditional:
cannot return neither
True
norFalse
, because there are elements of the array that meet the condition and elements that do not. This is a very typical problem.Assuming that we want another array, the same size as the input, with values
True
orFalse
depending on whether the corresponding element meets the condition or not , there would be two ways to solve it:1) Implement all the logic internally: it would have to be evaluated in a loop for each of the elements of the array. An implementation would be this:
2) Vectorize the function, using
np.vectorize
. Note that this is how the scalar case is handled more elegantly:In this specific problem the recommendation to use
.any()
or.all()
not works, but on another occasion they can be useful functions.