Scipy:使用 optimize.leastsq 时拟合参数的界限 [英] Scipy: bounds for fitting parameter(s) when using optimize.leastsq

查看:98
本文介绍了Scipy:使用 optimize.leastsq 时拟合参数的界限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 optimize.leastsq 来拟合数据.我想将拟合参数限制在某个范围内.使用 optimize.leastsq 时是否可以定义边界?边界在 optimize.fmin_slsqp 中实现,但我更喜欢使用 optimize.leastsq.

I am using optimize.leastsq to fit data. I would like to constrain the fitting parameter(s) to a certain range. Is it possible to define bounds when using optimize.leastsq? Bounds are implemented in optimize.fmin_slsqp, but I'd prefer to use optimize.leastsq.

推荐答案

我认为处理边界的标准方法是在参数超出边界时使函数最小化(残差)非常大.

I think the standard way of handling bounds is by making the function to be minimized (the residuals) very large whenever the parameters exceed the bounds.

import scipy.optimize as optimize
def residuals(p,x,y):
    if within_bounds(p):
        return y - model(p,x)
    else:
        return 1e6

p,cov,infodict,mesg,ier = optimize.leastsq(
    residuals,p_guess,args=(x,y),full_output=True,warning=True)

这篇关于Scipy:使用 optimize.leastsq 时拟合参数的界限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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