Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

I want to use my fit from lmfit to generate data points. I can do it with the popt output from curve_fit. What is the equivalent from lmfit?

I tried it with curve_fit, and that works well. However, the fit from lmfit is better and I would like to use it. But I just don't know how?

With curve_fit:

from scipy.optimize import curve_fit

import matplotlib.pyplot as plt

import numpy as np

def _1gaussian(x, amp1,cen1,sigma1):

"""By Emily Grace Ripka: https://github.com/emilyripka/BlogRepo/blob/master/181119_PeakFitting.ipynb"""

    return amp1*(1/(sigma1*(np.sqrt(2*np.pi))))*(np.exp(-((x-cen1)**2)/((2*sigma1)**2)))

function_mean = sum(x * y) / sum(y)

sigma = np.sqrt(sum(y * (x - function_mean) ** 2) / sum(y))

p0 = [max(y), function_mean, sigma]

popt,pcov = curve_fit(_1gaussian, x, y, p0)

new_x = np.linspace(int(min(x)), int(max(x)), 100)

new_y = _1gaussian(new_x, *popt)

With lmfit:

from lmfit.models import PseudoVoigtModel

import matplotlib.pyplot as plt

mod = PseudoVoigtModel()

pars = mod.guess(y, x=x)

out = mod.fit(y, pars, x=x)

print(out.fit_report(min_correl=0.25))

out = mod.fit(y, pars, x=x)

plt.plot(x, y)

plt.plot(x, out.best_fit, 'r-')

plt.show()

1 Answer

0 votes
by (25.1k points)

You can just use out variable from your code. print(out.best_values) 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...