Note
Click here to download the full example code
doc_model_two_components.pyΒΆ
Out:
[[Model]]
(Model(gaussian) + Model(line))
[[Fit Statistics]]
# fitting method = leastsq
# function evals = 44
# data points = 101
# variables = 5
chi-square = 2.57855517
reduced chi-square = 0.02685995
Akaike info crit = -360.457020
Bayesian info crit = -347.381417
[[Variables]]
amp: 8.45931062 +/- 0.12414515 (1.47%) (init = 5)
cen: 5.65547873 +/- 0.00917678 (0.16%) (init = 5)
wid: 0.67545524 +/- 0.00991686 (1.47%) (init = 1)
slope: 0.26484404 +/- 0.00574892 (2.17%) (init = 0)
intercept: -0.96860202 +/- 0.03352202 (3.46%) (init = 1)
[[Correlations]] (unreported correlations are < 0.100)
C(slope, intercept) = -0.795
C(amp, wid) = 0.666
C(amp, intercept) = -0.222
C(amp, slope) = -0.169
C(cen, slope) = -0.162
C(wid, intercept) = -0.148
C(cen, intercept) = 0.129
C(wid, slope) = -0.113
##
import warnings
warnings.filterwarnings("ignore")
##
# <examples/doc_model_two_components.py>
import matplotlib.pyplot as plt
from numpy import exp, loadtxt, pi, sqrt
from lmfit import Model
data = loadtxt('model1d_gauss.dat')
x = data[:, 0]
y = data[:, 1] + 0.25*x - 1.0
def gaussian(x, amp, cen, wid):
"""1-d gaussian: gaussian(x, amp, cen, wid)"""
return (amp / (sqrt(2*pi) * wid)) * exp(-(x-cen)**2 / (2*wid**2))
def line(x, slope, intercept):
"""a line"""
return slope*x + intercept
mod = Model(gaussian) + Model(line)
pars = mod.make_params(amp=5, cen=5, wid=1, slope=0, intercept=1)
result = mod.fit(y, pars, x=x)
print(result.fit_report())
plt.plot(x, y, 'bo')
plt.plot(x, result.init_fit, 'k--', label='initial fit')
plt.plot(x, result.best_fit, 'r-', label='best fit')
plt.legend(loc='best')
plt.show()
# <end examples/doc_model_two_components.py>
Total running time of the script: ( 0 minutes 0.142 seconds)