lmfit中curve_fit的popt等效于什么?

时间:2019-07-11 09:44:11

标签: python lmfit

我想使用lmfit中的fit生成数据点。我可以使用curve_fit的popt输出来做到这一点。 lmfit的等效功能是什么?

我用curve_fit尝试了,效果很好。但是,lmfit的拟合更好,我想使用它。但是我不知道怎么办?

使用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)

使用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 个答案:

答案 0 :(得分:0)

popt没有一个直接的类似物,但是有一些选项可以获取反映参数已命名和排序的变量参数的值。您可以尝试:

>>> print(out.best_values)

{'fraction': 0.2553102387885937, 'sigma': 0.7979269636290158, 'center': 5.659382353179054, 'amplitude': 9.539383879170684}

>>> for name, par in out.params.items():
...     print("{:s} = {:.4f} +/- {:.4f}".format(name, par.value, par.stderr))

fraction = 0.2553 +/- 0.0683
sigma = 0.7979 +/- 0.0143
center = 5.6594 +/- 0.0097
amplitude = 9.5394 +/- 0.2142
fwhm = 1.5959 +/- 0.0285
height = 5.1534 +/- 0.0679