How do I use the Minimizer object to minimize the # of function calls. I get this message from the logs:
Too many function calls (max set to %i)! Use: minimize(func, params, ..., maxfev=NNN)or set leastsq_kws[\'maxfev\'] to increase this maximum.'
from numpy import sqrt, pi, exp, loadtxt
from lmfit import Model
from lmfit import Minimizer
import matplotlib.pyplot as plt
data = loadtxt('data/model1d_gauss.dat')
x = data[:, 0]
y = data[:, 1]
def gaussian(x, amp, cen, wid):
"1-d gaussian: gaussian(x, amp, cen, wid)"
return (amp/(sqrt(2*pi)*wid)) * exp(-(x-cen)**2 /(2*wid**2))
gmodel = Model(gaussian)
result = gmodel.fit(y, x=x, amp=5, cen=1, wid=1,method= 'least_squares')
print(result.fit_report())
r = result.fit_report()
plt.plot(x, y, 'bo')
plt.plot(x, result.init_fit, 'k--')
plt.plot(x, result.best_fit, 'r-')
plt.show()
I want to make the fitting faster by minimizing the function calls (lots of the data is noisy crap anyway)
Not sure if i would recommend this, but as you are using scipy's least_squares internally, you could just pass the options needed, e.g.:
result = gmodel.fit(y, x=x, amp=5, cen=1, wid=1,method='least_squares',
fit_kws={'max_nfev': 100})
Of course this assumes, that lmfit will accept the state after observing the maxiter reached
status. But from your question it sounds, this is treated as warning only.
Keep in mind, that this optimizer is based on tolerance-criterions (see docs) and when not converging given x
number of steps, it actually thinks it can still improve the minimization!
As asked in the comment: yes you can change those criterions too, e.g. by doing:
result = gmodel.fit(y, x=x, amp=5, cen=1, wid=1,method='least_squares',
fit_kws={'ftol': 1-07, # default 1e-08
'xtol': 1-07, # 1e-08
'gtol': 1-07}) # 1e-08