Search code examples
pythoncurve-fittingdata-fittingmodel-fittinglmfit

LMFIT - Extrackt variables form Fitfunction


i wrote a short code to fit spectra from optical emission spec.. Therefor I fitted a VoigtModel into the Peak and LinearModel into the backgrund. Someting like this:

mod=VoigtModel()
pars = mod.guess(y, x=x)
out = mod.fit(y, pars, x=x)`

and

mod=LinearModel() 
pars = mod.guess(y, x=x)
func=mod.fit(y,pars,x=x)`

Now I would like to calculate the arear beween the linear function and the voigt peak function. Does anyone know how to do that. My idea was to reconstruct the functions and perform an integration. But I dont know how to extract the variable (slope,intercept, gamma, sigma,...) from the fit_report()?? thx BR maths


Solution

  • I'm not entirely sure what you're asking for, but here are some things you can extract from the fit result out or func (which is sort of a weird name to give the fit result, but OK):

    ModelResult has:

    • params: an ordered dictionary of the best-fit parameters. Keys are the parameter names, and values are lmfit.Parameter objects that have attributes of value, stderr, max, min, correl, etc.

    • best_values: a simple dictionary with parameter names as keys and best-fit values as values.

    • best_fit: the ndarray of the best-fit model.

    More attributes of the result are described at http://lmfit.github.io/lmfit-py/model.html#modelresult-attributes

    So, probably what you want is something like:

    for parname, param in out.params.items():
        print("%s = %f +/- %f " % (parname, param.value, param.stderr))
    

    which is basically what the fit_report method does.