Search code examples
lmfit

Is there a way to extract the variables from `lmfit` report?


I'm using the python package lmfit to fit my dataset with this model:

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())

And the fit report looks like:

[[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'

I was wondering if it is possible to extract the gauss_center and its error with two variables, instead of directly copying and pasting these results. Thanks!


Solution

  • out.params['gauss_center'].value will be the best-fit value for the gauss_center parameter, and out.params['gauss_center'].stderr will be its standard error.