lmfit-最小化器不接受scipy最小化器关键字参数 [英] lmfit - minimizer does not accept scipy minimizer keyword arguments

查看:170
本文介绍了lmfit-最小化器不接受scipy最小化器关键字参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用lmfit使某些模型适合我的数据.请参阅下面的MWE:

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')

为了尽早完成(目前精度不是最重要的事情),我想更改停止拟合的标准.基于文档和对SO ,我尝试提供一些关键字参数(下一行中的fit_kws),这些参数将传递给所使用的最小化器.我也尝试使用kws**{'xatol':0.01}.紧接着,我还在调用fitter.minimize()的最后一行中尝试了前面提到的选项.但是,在所有情况下,我都得到一个TypeError,表示它具有意外的关键字参数:

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?

python:3.6.9
scipy:1.3.1
lmfit:0.9.12

python: 3.6.9
scipy: 1.3.1
lmfit: 0.9.12

推荐答案

将关键字参数传递给基础scipy求解器的最佳方法就是使用

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')

# 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)

这里的主要问题是xatol对于基础求解器scipy.optimize.minimize()而言不是有效的关键字参数.相反,您可能打算使用tol:

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')

fitter = lmfit.Minimizer(lm_min, params, fcn_args=(x, ydata))
fit = fitter.minimize(method='nelder', tol=0.01)

这篇关于lmfit-最小化器不接受scipy最小化器关键字参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆