curve_fit 在 lmfit 中的 popt 是什么?
What is the equivalent of curve_fit's popt in lmfit?
我想使用 lmfit 的拟合来生成数据点。我可以用 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()
没有 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
我想使用 lmfit 的拟合来生成数据点。我可以用 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()
没有 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