有两个明显遵循指数趋势的数据集,我用scipy.optimize.curve_fit()
对它们进行了曲线拟合。x
数据集不包含零且有界0<x<=100
,而{0<=y<=1
。这是拟合方程:
def func(x, a, c, d):
return a * numpy.exp(-c*x)+d
我给curve_fit
打了个电话:
其中x1
和{
Based on the code provided in this answer,考虑到SciPy显然不包含这类内容,我调用了如下引导方法:
pfit, perr = fit_bootstrap(pstart, xx, yy, func)
其中pfit
是新的拟合参数(与curve_fit
给出的参数进行比较),而{p-start
在我的例子中是(1,1e-6,1),xx
是用来绘制函数的x值,yy
是应用于xx
值的拟合方程得出的y值。最后,拟合函数是func=a*numpy.exp(-c*x)+d
。在
调用引发错误:TypeError: func() takes exactly 4 arguments (2 given)
。我知道在论据上存在不匹配,但我不知道错误在哪里。有人能帮忙吗?在
回溯:
TypeError Traceback (most recent call last)
in <module>()
163 return pfit_bootstrap, perr_bootstrap
164
--> 165 pfit, perr = fit_bootstrap(pstart, xx, yy, func)
166
167 print("\nFit parameters and parameter errors from bootstrap method :")
in fit_bootstrap(p0, datax, datay, function, yerr_systematic)
127
128 # Fit first time
--> 129 pfit, perr = optimize.leastsq(errfunc, p0, args=(datax, datay), full_output=0)
130
131
in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag)
375 if not isinstance(args, tuple):
376 args = (args,)
--> 377 shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)
378 m = shape[0]
379 if n > m:
in _check_func(checker, argname, thefunc, x0, args, numinputs, output_shape)
24 def _check_func(checker, argname, thefunc, x0, args, numinputs,
25 output_shape=None):
---> 26 res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
27 if (output_shape is not None) and (shape(res) != output_shape):
28 if (output_shape[0] != 1):
in <lambda>(p, x, y)
124 def fit_bootstrap(p0, datax, datay, function, yerr_systematic=0.0):
125
--> 126 errfunc = lambda p, x, y: function(x,p) - y
127
128 # Fit first time
TypeError: func() takes exactly 4 arguments (2 given)
在下一行中,不应将func作为参数传递:
如果你检查你所提到的答案,它们会传递名为ff的函数。ff定义为:
^{pr2}$添加“ff”的定义后,可以将调用更改为:
相关问题 更多 >
编程相关推荐