Scipy.optimize.minimize Jacobian Function Causes 'value Error: The Truth Value Of An Array With More Than One Element Is Ambiguous'
Solution 1:
Look at the side bar. See all those SO questions about that same ValueError
?
While the circumstances vary, in nearly every case it is the result of using a boolean array in a Python context that expects a scalar boolean.
A simple example is
In [236]: if np.arange(10)>5:print('yes')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-236-633002262b65> in <module>()
----> 1 if np.arange(10)>5:print('yes')
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
np.arange(10)>5
produces a boolean array, np.array([False, False, ...])
.
Combining boolean expressions can also produce this. np.arange(10)>5 | np.arange(10)<2
produces this, (np.arange(10)>5) | (np.arange(10)<2)
does not - because of the presidence of the logical operators. Using and
instead of |
in this context is hopeless.
I'll look at your code in more detail, but in the meantime maybe this will help you find the problem yourself.
==================
from the error stack:
if (phi_a1 > phi0 + c1 * alpha1 * derphi0) or \
the code at this point expects (phi_a1 > phi0 + c1 * alpha1 * derphi0)
(and whatever follows the or
) to be a scalar. Presumably one of those variables is an array with multiple values. Admittedly this is occurring way down the calling stack so it will difficult to trace those values back to your code.
Prints, focusing on variable type and shape, might be most useful. Sometimes on these iterative solvers the code runs fine for one loop, and then some variable changes to array, and it chokes on the next loop.
==================
Why are you using np.matrix.trace
? In my tests that produces a 2d single element np.matrix
. It's not obvious that it would produce this ValueError, but it's still suspicious.
Post a Comment for "Scipy.optimize.minimize Jacobian Function Causes 'value Error: The Truth Value Of An Array With More Than One Element Is Ambiguous'"