Note
Click here to download the full example code
doc_model_with_iter_callback.pyΒΆ
Out:
ITER -1 ['3.00000', '6.00000', '2.00000', '0.00000', '0.00000', '4.70964', '0.59841']
ITER 0 ['3.00000', '6.00000', '2.00000', '0.00000', '0.00000', '4.70964', '0.59841']
ITER 1 ['3.00000', '6.00000', '2.00000', '0.00000', '0.00000', '4.70964', '0.59841']
ITER 2 ['3.00000', '6.00000', '2.00000', '0.00000', '0.00000', '4.70964', '0.59841']
ITER 3 ['3.00000', '6.00000', '2.00000', '0.00000', '0.00000', '4.70964', '0.59841']
ITER 4 ['3.00000', '6.00000', '2.00000', '0.00000', '0.00000', '4.70964', '0.59841']
ITER 5 ['3.00000', '6.00000', '2.00000', '0.00000', '0.00000', '4.70964', '0.59841']
ITER 6 ['3.00000', '6.00000', '2.00000', '0.00000', '0.00000', '4.70964', '0.59841']
ITER 7 ['27.41810', '24.48443', '6.86407', '-0.20617', '3.22509', '16.16365', '1.59355']
ITER 8 ['27.41810', '24.48443', '6.86407', '-0.20617', '3.22509', '16.16365', '1.59355']
ITER 9 ['27.41810', '24.48443', '6.86407', '-0.20617', '3.22509', '16.16365', '1.59355']
ITER 10 ['27.41810', '24.48443', '6.86407', '-0.20617', '3.22509', '16.16365', '1.59355']
ITER 11 ['27.41810', '24.48443', '6.86407', '-0.20617', '3.22509', '16.16365', '1.59355']
ITER 12 ['27.41810', '24.48443', '6.86407', '-0.20617', '3.22509', '16.16365', '1.59355']
ITER 13 ['6.96502', '104.53101', '17.24485', '0.39526', '3.20217', '40.60853', '0.16113']
ITER 14 ['25.52956', '29.38325', '12.08679', '-0.19932', '3.90951', '28.46221', '0.84264']
ITER 15 ['25.52956', '29.38325', '12.08679', '-0.19932', '3.90951', '28.46221', '0.84264']
ITER 16 ['25.52956', '29.38325', '12.08679', '-0.19932', '3.90951', '28.46221', '0.84264']
ITER 17 ['25.52956', '29.38325', '12.08679', '-0.19932', '3.90951', '28.46221', '0.84264']
ITER 18 ['25.52956', '29.38325', '12.08679', '-0.19932', '3.90951', '28.46221', '0.84264']
ITER 19 ['25.52956', '29.38325', '12.08679', '-0.19932', '3.90951', '28.46221', '0.84264']
ITER 20 ['20.91135', '29.80809', '28.98798', '-0.25803', '4.54769', '68.26148', '0.28779']
ITER 21 ['20.91135', '29.80809', '28.98798', '-0.25803', '4.54769', '68.26148', '0.28779']
ITER 22 ['20.91135', '29.80809', '28.98798', '-0.25803', '4.54769', '68.26148', '0.28779']
ITER 23 ['20.91135', '29.80809', '28.98798', '-0.25803', '4.54769', '68.26148', '0.28779']
ITER 24 ['20.91135', '29.80809', '28.98798', '-0.25803', '4.54769', '68.26148', '0.28779']
ITER 25 ['20.91135', '29.80809', '28.98798', '-0.25803', '4.54769', '68.26148', '0.28779']
ITER 26 ['29.69932', '8.72234', '28.44164', '-0.29097', '5.00931', '66.97495', '0.41658']
ITER 27 ['29.69932', '8.72234', '28.44164', '-0.29097', '5.00931', '66.97495', '0.41658']
ITER 28 ['29.69932', '8.72234', '28.44164', '-0.29097', '5.00931', '66.97495', '0.41658']
ITER 29 ['29.69932', '8.72234', '28.44165', '-0.29097', '5.00931', '66.97496', '0.41658']
ITER 30 ['29.69932', '8.72234', '28.44164', '-0.29097', '5.00931', '66.97495', '0.41658']
ITER 31 ['29.69932', '8.72234', '28.44164', '-0.29097', '5.00931', '66.97495', '0.41658']
ITER 32 ['46.89714', '8.78075', '7.31686', '-0.28389', '4.21023', '17.22988', '2.55701']
ITER 33 ['46.89714', '8.78075', '7.31686', '-0.28389', '4.21023', '17.22988', '2.55701']
ITER 34 ['46.89714', '8.78075', '7.31686', '-0.28389', '4.21023', '17.22988', '2.55701']
ITER 35 ['46.89714', '8.78075', '7.31686', '-0.28389', '4.21023', '17.22988', '2.55701']
ITER 36 ['46.89714', '8.78075', '7.31686', '-0.28389', '4.21023', '17.22988', '2.55701']
ITER 37 ['46.89714', '8.78075', '7.31686', '-0.28389', '4.21023', '17.22988', '2.55701']
ITER 38 ['-4.07966', '-5.77276', '1.83398', '0.19891', '-0.40733', '4.31869', '-0.88744']
ITER 39 ['42.58021', '9.47161', '3.59328', '-0.28184', '3.21567', '8.46154', '4.72744']
ITER 40 ['42.58021', '9.47161', '3.59328', '-0.28184', '3.21567', '8.46154', '4.72744']
ITER 41 ['42.58021', '9.47161', '3.59328', '-0.28184', '3.21567', '8.46154', '4.72744']
ITER 42 ['42.58021', '9.47161', '3.59328', '-0.28184', '3.21567', '8.46154', '4.72744']
ITER 43 ['42.58021', '9.47161', '3.59328', '-0.28184', '3.21567', '8.46154', '4.72744']
ITER 44 ['42.58021', '9.47161', '3.59328', '-0.28184', '3.21567', '8.46154', '4.72744']
ITER 45 ['30.01832', '6.80220', '2.99461', '-0.12851', '2.34682', '7.05177', '3.99904']
ITER 46 ['30.01832', '6.80220', '2.99461', '-0.12851', '2.34682', '7.05177', '3.99904']
ITER 47 ['30.01832', '6.80220', '2.99461', '-0.12851', '2.34682', '7.05177', '3.99904']
ITER 48 ['30.01832', '6.80220', '2.99461', '-0.12851', '2.34682', '7.05177', '3.99904']
ITER 49 ['30.01832', '6.80220', '2.99461', '-0.12851', '2.34682', '7.05177', '3.99904']
ITER 50 ['30.01832', '6.80220', '2.99461', '-0.12851', '2.34682', '7.05177', '3.99904']
ITER 51 ['17.69507', '8.46657', '1.01659', '-0.23045', '3.83669', '2.39389', '6.94409']
ITER 52 ['17.69507', '8.46657', '1.01659', '-0.23045', '3.83669', '2.39389', '6.94409']
ITER 53 ['17.69507', '8.46657', '1.01659', '-0.23045', '3.83669', '2.39389', '6.94409']
ITER 54 ['17.69507', '8.46657', '1.01659', '-0.23045', '3.83669', '2.39389', '6.94409']
ITER 55 ['17.69507', '8.46657', '1.01659', '-0.23045', '3.83669', '2.39389', '6.94409']
ITER 56 ['17.69507', '8.46657', '1.01659', '-0.23045', '3.83669', '2.39389', '6.94409']
ITER 57 ['22.61349', '7.75137', '1.45137', '-0.21630', '3.56563', '3.41771', '6.21585']
ITER 58 ['22.61349', '7.75137', '1.45137', '-0.21630', '3.56563', '3.41771', '6.21585']
ITER 59 ['22.61349', '7.75137', '1.45137', '-0.21630', '3.56563', '3.41771', '6.21585']
ITER 60 ['22.61349', '7.75137', '1.45137', '-0.21630', '3.56563', '3.41771', '6.21585']
ITER 61 ['22.61349', '7.75137', '1.45137', '-0.21630', '3.56563', '3.41771', '6.21585']
ITER 62 ['22.61349', '7.75137', '1.45137', '-0.21630', '3.56563', '3.41771', '6.21585']
ITER 63 ['24.13916', '7.62179', '1.18261', '-0.20160', '3.34246', '2.78482', '8.14315']
ITER 64 ['24.13916', '7.62179', '1.18261', '-0.20160', '3.34246', '2.78482', '8.14315']
ITER 65 ['24.13916', '7.62179', '1.18261', '-0.20160', '3.34246', '2.78482', '8.14315']
ITER 66 ['24.13916', '7.62179', '1.18260', '-0.20160', '3.34246', '2.78482', '8.14315']
ITER 67 ['24.13916', '7.62179', '1.18261', '-0.20160', '3.34246', '2.78482', '8.14315']
ITER 68 ['24.13916', '7.62179', '1.18261', '-0.20160', '3.34246', '2.78482', '8.14315']
ITER 69 ['24.40272', '7.65357', '1.22777', '-0.20151', '3.32847', '2.89118', '7.92923']
ITER 70 ['24.40272', '7.65357', '1.22777', '-0.20151', '3.32847', '2.89118', '7.92923']
ITER 71 ['24.40272', '7.65357', '1.22777', '-0.20151', '3.32847', '2.89118', '7.92923']
ITER 72 ['24.40272', '7.65357', '1.22777', '-0.20151', '3.32847', '2.89118', '7.92923']
ITER 73 ['24.40272', '7.65357', '1.22777', '-0.20151', '3.32847', '2.89118', '7.92923']
ITER 74 ['24.40272', '7.65357', '1.22777', '-0.20151', '3.32847', '2.89118', '7.92923']
ITER 75 ['24.43754', '7.65514', '1.22977', '-0.20146', '3.32621', '2.89589', '7.92763']
ITER 76 ['24.43754', '7.65514', '1.22977', '-0.20146', '3.32621', '2.89589', '7.92763']
ITER 77 ['24.43754', '7.65514', '1.22977', '-0.20146', '3.32621', '2.89589', '7.92763']
ITER 78 ['24.43754', '7.65514', '1.22977', '-0.20146', '3.32621', '2.89589', '7.92763']
ITER 79 ['24.43754', '7.65514', '1.22977', '-0.20146', '3.32621', '2.89589', '7.92763']
ITER 80 ['24.43754', '7.65514', '1.22977', '-0.20146', '3.32621', '2.89589', '7.92763']
ITER 81 ['24.43823', '7.65514', '1.22982', '-0.20146', '3.32615', '2.89600', '7.92754']
ITER 82 ['24.43823', '7.65514', '1.22982', '-0.20146', '3.32615', '2.89600', '7.92754']
Nfev = 81
[[Model]]
(Model(gaussian, prefix='peak_') + Model(linear, prefix='bkg_'))
[[Fit Statistics]]
# fitting method = leastsq
# function evals = 81
# data points = 401
# variables = 5
chi-square = 20.7309419
reduced chi-square = 0.05235086
Akaike info crit = -1177.89596
Bayesian info crit = -1157.92615
[[Variables]]
peak_amplitude: 24.4382287 +/- 0.16625391 (0.68%) (init = 3)
peak_center: 7.65513988 +/- 0.00768465 (0.10%) (init = 6)
peak_sigma: 1.22981974 +/- 0.00834506 (0.68%) (init = 2)
bkg_slope: -0.20145591 +/- 0.00207980 (1.03%) (init = 0)
bkg_intercept: 3.32614932 +/- 0.02701936 (0.81%) (init = 0)
peak_fwhm: 2.89600411 +/- 0.01965112 (0.68%) == '2.3548200*peak_sigma'
peak_height: 7.92753837 +/- 0.04386563 (0.55%) == '0.3989423*peak_amplitude/max(2.220446049250313e-16, peak_sigma)'
[[Correlations]] (unreported correlations are < 0.100)
C(bkg_slope, bkg_intercept) = -0.857
C(peak_amplitude, peak_sigma) = 0.668
C(peak_amplitude, bkg_intercept) = -0.526
C(peak_sigma, bkg_intercept) = -0.352
C(peak_amplitude, bkg_slope) = 0.285
C(peak_sigma, bkg_slope) = 0.190
C(peak_center, bkg_slope) = -0.146
C(peak_center, bkg_intercept) = 0.125
##
import warnings
warnings.filterwarnings("ignore")
##
# <examples/doc_with_itercb.py>
import matplotlib.pyplot as plt
from numpy import linspace, random
from lmfit.lineshapes import gaussian
from lmfit.models import GaussianModel, LinearModel
def per_iteration(pars, iter, resid, *args, **kws):
print(" ITER ", iter, ["%.5f" % p for p in pars.values()])
x = linspace(0., 20, 401)
y = gaussian(x, amplitude=24.56, center=7.6543, sigma=1.23)
y = y - .20*x + 3.333 + random.normal(scale=0.23, size=x.size)
mod = GaussianModel(prefix='peak_') + LinearModel(prefix='bkg_')
pars = mod.make_params()
pars['peak_amplitude'].value = 3.0
pars['peak_center'].value = 6.0
pars['peak_sigma'].value = 2.0
pars['bkg_intercept'].value = 0.0
pars['bkg_slope'].value = 0.0
out = mod.fit(y, pars, x=x, iter_cb=per_iteration)
plt.plot(x, y, 'b--')
print('Nfev = ', out.nfev)
print(out.fit_report())
plt.plot(x, out.best_fit, 'k-', label='best fit')
plt.legend(loc='best')
plt.show()
# <end examples/doc_with_itercb.py>
Total running time of the script: ( 0 minutes 0.277 seconds)