When I try to minimize a function with scipy.minimize.brute() using the following code:
import scipy
scipy.optimize.brute(lambda x: x**2, ranges=(-2,3))
I get the following error:
TypeError: object of type 'int' has no len()
I suppose it has to do with the specification of the range, but I don't see why. The documentation says
Each component of the ranges tuple must be either a “slice object” or a range tuple of the form (low, high).
Where is my mistake?
As the documentation says:
Each component of the ranges tuple must be either a “slice object” or a range tuple of the form (low, high).
So the function expects a tuple of tuples, one of the form (low, high)
for each dimension. You only have one dimension, so the correct call in your case would be
scipy.optimize.brute(lambda x: x**2, ranges=((-2,3),) )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With