As @hpaulj noted in the comments, you forgot a comma passing an invalid args
parameter that raises the error you see. Anyway the 'correct' code:
xopt = optimize.fmin(f, guess, args=(np.zeros((5,5,)),))
will probably do not do what you think, since val
will always be 0
.
The function sp.optimize.fmin
finds the minimum of a function. The parameter x0
it is the initial guess. For example, for a 2D parabola:
def par2d(X0):
x, y = X0
return (x**2 + y**2)
we can find the x and y values of the minimum:
from scipy import optimize
guess = (1, 1)
min_pars = optimize.fmin(par2d, guess)
print(min_pars)
yields:
Optimization terminated successfully.
Current function value: 0.000000
Iterations: 38
Function evaluations: 69
[-2.10235293e-05 2.54845649e-05]
which is close enough to [0, 0]
.
The parameter args
should be an iterable (1D) with extra parameters that will be passed to the function to minimize and will not be optimized. For example, for a generalized parabola:
def par2d(X0, eccentricity=(1, 1)):
x, y = X0
ex_x, ex_y = eccentricity
return (x**2 * ex_x + y**2 * ex_y)
we can pass the eccentricity as a non-adjustable parameter
min_pars = optimize.fmin(par2d, guess, args=((1,100), ))
In your case (after adding the missing comma) the sum of a zero-filled array will always be 0
, val
will always be 0
and the value of x1
will be rendered irrelevant. I do not know what 'optimize' the parameters means in this context, but I am confident that what you are doing will not yield what you want.