I have a least squares error function (based on another function) that I'd like to minimize (to obtain a globally minimized curve fit, basically), which looks something like this:
def err(a, b, X, Y):
return np.sum((f(a, b, X)-Y)**2)
with X
being an array of points at which f
is evaluated, depending on the parameters a
and b
, and Y
being the "ground truth" for the points defined in X
.
Now according to what I found in questions 25206482 and 31388319 the syntax should be as follows:
Xc = np.array([1.0, 2.0, 3.0, 4.0, 5.0]) # points at which to evaluate error function
Yc = np.array([0.2, 0.4, 0.8, 0.12, 0.15]) # ground truth
g0 = np.array([1.0, 3.0]) # initial guess for a and b
res = scipy.optimize.minimize(err, g0, args=(Xc, Yc), method="Powell")
Unfortunately, I get the following error message:
TypeError: err() takes exactly 4 arguments (3 given)
If I delete Xc
or Yc
from the tuple, the number of arguments given decreases, so I suspect it is somewhere in the definition of g0
, because this seems to be passed to err
as a single argument.
How do I call minimize
properly if I have more than one parameter for optimization and additional "constant" arguments I want to pass to my function during optimization?