首页 > 解决方案 > 使用 lmfit 进行曲线拟合后从拟合统计参数中提取变量

问题描述

我正在使用 lmfit 针对我的数据绘制高斯拟合。我只想提取一个可变参数(例如: I0 )并将其存储在一个数组中。任何建议都会有帮助。我在下面附上了我的示例代码:

def Gauss(x,I0,x0,sigma,Background):
            return I0*exp((-(x-x0)**2)/(2*sigma**2))+Background   
mod=Model(Gauss)
result=mod.fit(yData,x=xData,I0=1,x0=mean,sigma=sigma,Background=1)
result.plot()
plt.grid()
plt.xlabel('x,y,z distribution')
plt.ylabel('PL intensity')
basename=os.path.basename(file)
plt.title(basename)
print('The fit statistics for',basename)
print(result.fit_report()) 


[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 264
    # data points      = 30
    # variables        = 4
    chi-square         = 8379722.68
    reduced chi-square = 322297.026
    Akaike info crit   = 384.203840
    Bayesian info crit = 389.808629
[[Variables]]
    I0:          6128.15928 +/- 343.334644 (5.60%) (init = 1)
    x0:         -5.1147e-07 +/- 3.1252e-08 (6.11%) (init = -4.039265e-07)
    sigma:      -7.4953e-07 +/- 6.0842e-08 (8.12%) (init = 9.136697e-07)
    Background:  1730.50204 +/- 338.181818 (19.54%) (init = 1)

标签: pythoncurve-fittinglmfit

解决方案


阅读 lmfit 文档并浏览一些示例可能会有所帮助。在 SO 上,期望是您将展示您尝试过的内容以及可能没有按预期工作的内容。

你说:

我想提取所有可变参数(例如: I0 )并将其与我的数据进行对比,并可视化适合我的误差线。

结果参数是 in result.params,一个包含参数名称键和 lmfit.Parameter 值的字典,它将具有多个属性。

如果您只想要最合适的参数值,您可以尝试

print(result.params.valuesdict())

有关更多信息,请参阅文档。

我不确定“根据我的数据绘制它并可视化适合我的误差条”是什么意思。

你还说:

我还想计算每个数据点的变量参数并存储在一个数组中。

也许您的意思是您想要模型的数组值?如果是这样,那就在result.best_fit.


推荐阅读