问题
I am trying to fit some model to my data with lmfit
. See the MWE below:
import lmfit
import numpy as np
def lm(params, x):
slope = params['slope']
interc = params['interc']
return interc + slope * x
def lm_min(params, x, data):
y = lm(params, x)
return data - y
x = np.linspace(0,100,1000)
y = lm({'slope':1, 'interc':0.5}, x)
ydata = y + np.random.randn(1000)
params = lmfit.Parameters()
params.add('slope', 2)
params.add('interc', 1)
fitter = lmfit.Minimizer(lm_min, params, fcn_args=(x, ydata), fit_kws={'xatol':0.01})
fit = fitter.minimize(method='nelder')
In order to be finished earlier (accuracy is not the most important thing for now), I want to change the criteria for stopping the fit. Based on the docs and some searches on SO, I tried to give some keyword arguments (fit_kws
in the line below) that will be passed to the minimizer that is used. I also tried to use kws
and **{'xatol':0.01}
. Next to that I also tried the before-mentioned options in the last line where I call fitter.minimize()
. However, in all cases I get a TypeError
, saying that it got unexpected keyword arguments:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~/STACK/WUR/PhD/Experiments/Microclimate experiment/Scripts/Fluctuations/mwe.py in <module>()
25
26 fitter = lmfit.Minimizer(lm_min, params, fcn_args=(x, ydata), fit_kws={'xatol':0.01})
---> 27 fit = fitter.minimize(method='nelder')
28
~/anaconda3/envs/py/lib/python3.6/site-packages/lmfit/minimizer.py in minimize(self, method, params, **kws)
1924 val.lower().startswith(user_method)):
1925 kwargs['method'] = val
-> 1926 return function(**kwargs)
1927
1928
~/anaconda3/envs/py/lib/python3.6/site-packages/lmfit/minimizer.py in scalar_minimize(self, method, params, **kws)
906 else:
907 try:
--> 908 ret = scipy_minimize(self.penalty, variables, **fmin_kws)
909 except AbortFitException:
910 pass
TypeError: minimize() got an unexpected keyword argument 'fit_kws'
Does anybody know how I can add keyword arguments for the specific solvers?
Version info:
python: 3.6.9
scipy: 1.3.1
lmfit: 0.9.12
回答1:
The best way to pass keyword arguments to the underlying scipy
solver would be just to use
# Note: valid but will not do what you want
fitter = lmfit.Minimizer(lm_min, params, fcn_args=(x, ydata), xatol=0.01)
fit = fitter.minimize(method='nelder')
or
# Also: valid but will not do what you want
fitter = lmfit.Minimizer(lm_min, params, fcn_args=(x, ydata))
fit = fitter.minimize(method='nelder', xatol=0.01)
The main problem here is that xatol
is not a valid keyword argument for the underlying solver, scipy.optimize.minimize()
. Instead, you probably mean to use tol
:
fitter = lmfit.Minimizer(lm_min, params, fcn_args=(x, ydata), tol=0.01)
fit = fitter.minimize(method='nelder')
or
fitter = lmfit.Minimizer(lm_min, params, fcn_args=(x, ydata))
fit = fitter.minimize(method='nelder', tol=0.01)
回答2:
In a github issue I found the following solution:
fit = fitter.minimize(method='nelder', **{'options':{'xatol':4e-4}})
Update
As mentioned by @dashesy, this is the same as writing:
fit = fitter.minimize(method='nelder', options={'xatol':4e-4})
This also works for other solver options.
来源:https://stackoverflow.com/questions/58727732/lmfit-minimizer-does-not-accept-scipy-minimizer-keyword-arguments