Python - R平方和可通过scipy.optimize curve_fit获得的绝对平方和?

Woo*_*ker 7 python curve-fitting scipy

我使用curve_fit拟合曲线.有没有办法读出决定系数和平方的绝对和?谢谢,Woodpicker

kir*_*off 5

根据文档,优化与curve_fit

参数的最佳值使得f(xdata,*popt) - ydata的平方误差之和最小化

然后,使用 optimize.leastsq

import scipy.optimize
p,cov,infodict,mesg,ier = optimize.leastsq(
        residuals,a_guess,args=(x,y),full_output=True,warning=True)
Run Code Online (Sandbox Code Playgroud)

这个用于residuals:

def residuals(a,x,y):
    return y-f(x,a)
Run Code Online (Sandbox Code Playgroud)

residuals是返回真实输出数据y和模型输出之间差异的方法,包括f模型,a参数,x输入数据.

方法optimize.leastsq返回了大量可用于自行计算RSquared和RMSE的信息.对于RSQuared,你可以做到

ssErr = (infodict['fvec']**2).sum()
ssTot = ((y-y.mean())**2).sum()
rsquared = 1-(ssErr/ssTot )
Run Code Online (Sandbox Code Playgroud)

什么是更多细节 infodict['fvec']

In [48]: optimize.leastsq?
...
      infodict -- a dictionary of optional outputs with the keys:
                  'fvec' : the function evaluated at the output
Run Code Online (Sandbox Code Playgroud)