简体   繁体   English

Scipy:使用optimize.leastsq时拟合参数的界限

[英]Scipy: bounds for fitting parameter(s) when using optimize.leastsq

I am using optimize.leastsq to fit data. 我使用optimize.leastsq来拟合数据。 I would like to constrain the fitting parameter(s) to a certain range. 我想将拟合参数约束到一定范围。 Is it possible to define bounds when using optimize.leastsq? 使用optimize.leastsq时是否可以定义边界? Bounds are implemented in optimize.fmin_slsqp, but I'd prefer to use optimize.leastsq. Bounds在optimize.fmin_slsqp中实现,但我更喜欢使用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)

I just found this a short time ago 我刚刚发现这个

http://code.google.com/p/nmrglue/source/browse/trunk/nmrglue/analysis/leastsqbound.py http://code.google.com/p/nmrglue/source/browse/trunk/nmrglue/analysis/leastsqbound.py

It uses parameter transformation to impose box constraints. 它使用参数转换来强加框约束。 It also calculates the adjusted covariance matrix for the parameter estimates. 它还计算参数估计的调整后的协方差矩阵。

BSD licensed, but I haven't tried it out yet. BSD获得许可,但我还没有尝试过。

You might find 你可能会发现
https://lmfit.github.io/lmfit-py/ useful for this. https://lmfit.github.io/lmfit-py/对此有用。 It allows upper / lower bounds for each variable, and allows algebraic constraints between parameters. 它允许每个变量的上限/下限,并允许参数之间的代数约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用python的optimize.leastsq()和optimize.curve_fit将数据拟合到Faddeeva函数 - Fitting data to Faddeeva function using python's optimize.leastsq() and optimize.curve_fit Python / Scipy - 将optimize.curve_fit的sigma实现到optimize.leastsq中 - Python / Scipy - implementing optimize.curve_fit 's sigma into optimize.leastsq Scipy:optimize.fmin 和 optimize.leastsq 之间的区别 - Scipy: difference between optimize.fmin and optimize.leastsq "python optimize.leastsq:将圆拟合到 3d 点集" - python optimize.leastsq: fitting a circle to 3d set of points Scipy ValueError:对象不足,无法使用optimize.leastsq进行所需的数组 - Scipy ValueError: object too deep for desired array with optimize.leastsq scipy.optimize.leastsq in Python 在拟合数据时不返回协方差矩阵 - scipy.optimize.leastsq in Python not returning covariance matrix when fitting data 为什么我的optimize.leastsq无法正常工作? - why is my optimize.leastsq not working? scipy.optimize.leastsq拟合:minpack.error - scipy.optimize.leastsq Fitting: minpack.error 使用scipy.optimize.leastsq有麻烦 - having trouble using scipy.optimize.leastsq 使用python中的optimize.leastsq方法获取拟合参数的标准错误 - Getting standard errors on fitted parameters using the optimize.leastsq method in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM