问题描述
我尝试使用 scipy.optimize.curvefit
函数用我的数据点拟合最佳拟合线:
x = np.array([0,2246,2600,3465,4392])
y = [-0.763,0.052,0.081,0.266,0.179]
yerror = [0.201,0.113,0.139,0.162,0.204]
plt.errorbar(wavelength,A,yerr=B,xerr=None,fmt='o')
def func(x,a,b,c):#the best fit function
return a + (b * x)**c
popt,pcov = scipy.optimize.curve_fit(func,x,y)
x_fit = np.linspace(0,np.max(x),1000) # create curve line of best fit
plt.plot(x_fit,func(x_fit,*popt),"b")
我的弹出值是:array([-7.63283206e-01,2.23580046e-04,2.63164486e-01])
其中第一个值 -7.63283206e-01
是我希望它在图表中显示的截距。
绘制数据点和最佳拟合 here using code above 并给出对数曲线,但我希望最佳拟合线 pass through the y axis like this instead 以说明更直的曲线。
提前致谢!
解决方法
“最佳拟合”在指定拟合标准之前没有任何意义(最小均方误差或最小均方相对误差或最小平均绝对误差等)。每个人的“最适合”是不同的。
正因为没有指定拟合标准,为什么不选择最简单的方法,不需要迭代过程,不需要猜测参数的初始值。
下面的方法,来自 https://fr.scribd.com/doc/14674814/Regressions-et-equations-integrales 给出:
如果您指定特定的拟合标准,则需要使用猜测初始值的迭代方法。您可以使用上述值 a、b、c 作为稳健且快速收敛的初始值。