Using scipy
,据我所知没有内置选项。您将始终需要像您已经做过的那样进行变通。
如果您愿意使用包装包装,我可以推荐我自己的包装吗symfit http://symfit.readthedocs.io/en/latest/tutorial.html#simple-example?这是一个包装器scipy
以可读性和更少的样板代码作为其核心原则。在symfit http://symfit.readthedocs.io/en/latest/tutorial.html#simple-example,您的问题将解决为:
from symfit import parameters, variables, exp, Fit, Parameter
a, b, c, d = parameters('a, b, c, d')
x, y = variables('x, y')
model_dict = {y: a * exp(-(x - b)**2 / (2 * c**2)) + d}
fit = Fit(model_dict, x=xdata, y=ydata)
fit_result = fit.execute()
线路a, b, c, d = parameters('a, b, c, d')
做四个Parameter
对象。修复例如参数c
为其初始值,在调用之前在任何地方执行以下操作fit.execute()
:
c.value = 4.0
c.fixed = True
所以可能的最终结果可能是:
from symfit import parameters, variables, exp, Fit, Parameter
a, b, c, d = parameters('a, b, c, d')
x, y = variables('x, y')
c.value = 4.0
c.fixed = True
model_dict = {y: a * exp(-(x - b)**2 / (2 * c**2)) + d}
fit = Fit(model_dict, x=xdata, y=ydata)
fit_result = fit.execute()
如果你想让你的代码更加动态,你可以使Parameter
立即使用以下对象:
c = Parameter(4.0, fixed=True)
有关更多信息,请查看文档:http://symfit.readthedocs.io/en/latest/tutorial.html#simple-example http://symfit.readthedocs.io/en/latest/tutorial.html#simple-example