syn*_*ing 11
终于找到了这个长问的答案!我希望这至少可以为这个话题节省几个小时的无望研究.Scipy在其优化部分下有一个名为curve_fit的特殊函数.它使用最小二乘法确定系数,最重要的是,它为您提供协方差矩阵.协方差矩阵包含每个系数的方差.更准确地说,矩阵的对角线是方差,通过平方根值,可以确定每个系数的标准误差!Scipy没有太多的文档,所以这里是一个示例代码,以便更好地理解:
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plot
def func(x,a,b,c):
return a*x**2 + b*x + c #Refer [1]
x = np.linspace(0,4,50)
y = func(x,2.6,2,3) + 4*np.random.normal(size=len(x)) #Refer [2]
coeff, var_matrix = curve_fit(func,x,y)
variance = np.diagonal(var_matrix) #Refer [3]
SE = np.sqrt(variance) #Refer [4]
#======Making a dictionary to print results========
results = {'a':[coeff[0],SE[0]],'b':[coeff[1],SE[1]],'c':[coeff[2],SE[2]]}
print "Coeff\tValue\t\tError"
for v,c in results.iteritems():
print v,"\t",c[0],"\t",c[1]
#========End Results Printing=================
y2 = func(x,coeff[0],coeff[1],coeff[2]) #Saves the y values for the fitted model
plot.plot(x,y)
plot.plot(x,y2)
plot.show()
Run Code Online (Sandbox Code Playgroud)
看起来 gnuplot 使用levenberg - marquardt并且有一个可用的 python 实现- 您可以从 mpfit.covar 属性获取错误估计(顺便说一句,您应该担心错误估计的“平均值” - 是否允许调整其他参数以补偿, 例如?)
| 归档时间: |
|
| 查看次数: |
2931 次 |
| 最近记录: |