使用 lmfit 进行曲线拟合后从拟合统计参数中提取变量

Extracting variables from fit statistics parameters after curve fitting using 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)

阅读 lmfit 文档并浏览一些示例可能会有所帮助。在 SO 上,期望你会展示你尝试过的和可能没有达到你预期的效果。

你说:

I want to extract all the variable parameters (eg: I0 ) and plot it against my data and visualise the error bars for my fits.

生成的参数在 result.params 中,这是一个包含参数名称键和 lmfit.Parameter 值的字典,它将具有多个属性。

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

print(result.params.valuesdict())

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

我不确定你所说的“根据我的数据绘制它并可视化我的拟合误差线”是什么意思。

你还说:

I also want to compute the variable parameters for each of my data points and store in an array.

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