[英]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.