问题描述
我面临以下问题。
我有这个数据:
x = np.array([ 1.00E-03,1.00E-04,1.00E-05,1.00E-06 ])
y = np.array([ 0.01,0.002469136,0.000771605, 0.000257202 ])
我想对这些数据进行幂律拟合并得到回归系数。
但是,我在 WPS office 和 scipy 之间得到了不同的结果。
我的代码如下:
import numpy as np
from scipy.optimize import curve_fit
from sklearn.metrics import r2_score
xdata = x
ydata = y
# Power Law function
def f(x,a,b):
return (a*(x**b))
popt,pcov = curve_fit(f,xdata,ydata)
r_squared = r2_score(ydata,f(xdata,popt[0],popt[1]))
在 WPS 办公室我得到 R² = 0.9968
在 scipy 中,我得到 R² = 0.9995。
关于为什么会发生这种情况的任何解释?即使可能有不同的算法,它们也应该收敛到类似的解决方案,不是吗?
最好的问候!
解决方法
如果我将数据更改为 log-log 中的线性拟合:
parameters = np.polyfit(np.log(xdata),np.log(ydata),1)
a = np.exp(parameters[1])
b = parameters[0]
r2_score(y,a*x**b)
我将获得与 Excel 相同的 a
和 b
值,但现在 R² = 0.9883...
好的...我找到了答案。
R2 的计算如下:https://www.got-it.ai/solutions/excel-chat/excel-tutorial/r-squared/r-squared-in-excel
所以:
x_log = np.log(x)
y_log = np.log(y)
tmp1 = len(x)*(np.sum(x_log*y_log))-np.sum(x_log)*np.sum(y_log)
tmp2 = len(x)*np.sum(x_log**2)-np.sum(x_log)**2
tmp3 = len(x)*np.sum(y_log**2)-np.sum(y_log)**2
r2 = (tmp1/np.sqrt(tmp2*tmp3))**2
产生正确的值