You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从lmfit拟合报告中提取gauss_center变量及其误差?

问题:从lmfit拟合结果中提取gauss_center的数值及误差

我正在用Python的lmfit包拟合数据集,拟合代码如下:

def GaussianFit(results, highest_num, Peak_shot, nuni, dif = None):
  
    ...
    
    Gauss_mod = GaussianModel(prefix='gauss_')
    Const_mod = ConstantModel(prefix='const_')
    mod = Gauss_mod + Const_mod
   
    pars = mod.make_params(gauss_center = ig, gauss_sigma = 1/12) 
    
    out = mod.fit(y_sel, pars, x = x_sel, weights = get_weights(last_sel,Peak_shot,nuni))

    print(out.fit_report())

拟合生成的报告内容如下:

[[Model]]
    (Model(gaussian, prefix='gauss_') + Model(constant, prefix='const_'))
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 101
    # data points      = 18
    # variables        = 4
    chi-square         = 2.1571e-05
    reduced chi-square = 1.5408e-06
    Akaike info crit   = -237.421693
    Bayesian info crit = -233.860206
[[Variables]]
    gauss_amplitude:  0.02133733 +/- 0.01122602 (52.61%) (init = 0.25)
    gauss_center:     0.98316682 +/- 0.02152806 (2.19%) (init = 1.041587)
    gauss_sigma:      0.11847360 +/- 0.04182091 (35.30%) (init = 0.08333333)
    const_c:          0.09532047 +/- 0.01831759 (19.22%) (init = 0)
    gauss_fwhm:       0.27898399 +/- 0.09848070 (35.30%) == '2.3548200*gauss_sigma'

我不想手动复制粘贴结果,希望通过变量提取gauss_center的数值及其对应误差,请问有可行方法吗?


解决方法

lmfit的拟合结果对象out(即mod.fit()的返回值)自带参数属性,直接通过out.params就能访问所有拟合变量的数值和误差,无需解析文本报告:

  • 提取gauss_center的拟合数值:out.params['gauss_center'].value
  • 提取gauss_center的误差:out.params['gauss_center'].stderr

你可以在代码里直接添加这些提取逻辑,比如:

# 拟合完成后
center_value = out.params['gauss_center'].value
center_error = out.params['gauss_center'].stderr

print(f"gauss_center数值:{center_value}")
print(f"gauss_center误差:{center_error}")

如果需要更简洁的写法,也可以直接用索引访问数值:

center_value = out.params['gauss_center']  # 直接获取数值
center_error = out.params['gauss_center'].stderr

另外,out.params里的每个参数对象还包含其他信息,比如初始值(.init_value)、相对误差百分比(.percent_error)等,你可以根据需求调用。

内容的提问来源于stack exchange,提问作者IGY

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 18:00:52