Python:执行引导时出错scipy.optimize.curve_-fi

2024-09-26 18:02:19 发布

您现在位置:Python中文网/ 问答频道 /正文

有两个明显遵循指数趋势的数据集,我用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打了个电话:

^{pr2}$

其中x1和{}是我的两个数据集。现在,based on this answer,我想执行Bootstrap Method,以确保获得拟合参数的标准误差,我将用它来量化拟合优度。在

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) 

Tags: 数据inoutputifdefargsbootstrapfit
1条回答
网友
1楼 · 发布于 2024-09-26 18:02:19

在下一行中,不应将func作为参数传递:

pfit, perr = fit_bootstrap(pstart, xx, yy, func)

如果你检查你所提到的答案,它们会传递名为ff的函数。ff定义为:

^{pr2}$

添加“ff”的定义后,可以将调用更改为:

pfit, perr = fit_bootstrap(pstart, xx, yy, ff)

相关问题 更多 >

    热门问题