Python 曲线拟合库,允许我为参数分配界限 [英] Python curve fit library that allows me to assign bounds to parameters
问题描述
我希望能够执行拟合,允许我将任意曲线函数拟合到数据,并允许我设置参数的任意边界,例如我想拟合函数:
I'd like to be able to perform fits that allows me to fit an arbitrary curve function to data, and allows me to set arbitrary bounds on parameters, for example I want to fit function:
f(x) = a1(x-a2)^a3cdotexp(-a4*x^a5)
然后说:
a2
在以下范围内:(-1, 1)
a3
和a5
为正
a2
is in following range:(-1, 1)
a3
anda5
are positive
有很好的 scipy curve_fit 函数,但它没有t 允许指定参数范围.还有很好的 http://code.google.com/p/pyminuit/ 库可以进行通用最小化,并且它允许设置参数的界限,但在我的情况下它没有覆盖.
There is nice scipy curve_fit function, but it doesn't allow to specify parameter bounds. There also is nice http://code.google.com/p/pyminuit/ library that does generic minimalization, and it allows to set bounds on parameters, but in my case it did not coverge.
推荐答案
注意:SciPy 0.17 版本中的新内容
Note: New in version 0.17 of SciPy
假设您想将一个模型拟合到如下所示的数据中:
Let's suppose you want to fit a model to the data which looks like this:
y=a*t**alpha+b
以及对 alpha 的约束
and with the constraint on alpha
0<alpha<2
而其他参数 a 和 b 保持空闲.那么我们应该以如下方式使用curve_fit的bounds选项:
while other parameters a and b remains free. Then we should use the bounds option of curve_fit in the following fashion:
import numpy as np
from scipy.optimize import curve_fit
def func(t, a,alpha,b):
return a*t**alpha+b
param_bounds=([-np.inf,0,-np.inf],[np.inf,2,np.inf])
popt, pcov = curve_fit(func, xdata, ydata,bounds=param_bounds)
来源是这里.
这篇关于Python 曲线拟合库,允许我为参数分配界限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!