问题描述
我正在尝试拟合高斯分布。我尝试使用 OriginPro 和 Python 来适应。在 OriginPro 中的拟合比通过 Python 获得的要好,我想用 Python 来做。
我使用的代码是:
import numpy as np
import matplotlib.pyplot as plt
import lmfit
data=data
mod=lmfit.models.GaussianModel()
x=np.arange(140,510,1)
y= np.array(np.log(data[140:510]),dtype=np.float32)
idx= (np.isfinite(y))
pars = mod.guess(y[idx],x=x[idx])
out = mod.fit(y[idx],pars,x=x[idx])
plt.plot(x[idx],out.best_fit,'r',label='Gaussian Model')
plt.plot(x,np.log(data[140:510]))
plt.show()
数据:
[ 0.,0.,34.,33.,30.,25.,47.,36.,37.,41.,42.,40.,49.,44.,35.,32.,50.,29.,39.,43.,27.,48.,38.,20.,53.,45.,62.,31.,46.,56.,57.,51.,55.,67.,68.,66.,60.,63.,58.,72.,64.,61.,71.,74.,69.,59.,70.,73.,65.,52.,81.,80.,84.,54.,23.,24.,28.,21.,1.]
fit_report 的输出:
In [37]:out.fit_report()
Out[37]: "[[Model]]\n Model(gaussian)\n[[Fit Statistics]]\n # fitting method = leastsq\n # function evals = 17\n # data points = 333\n # variables = 3\n chi-square = 12.5923742\n reduced chi-square = 0.03815871\n Akaike info crit = -1084.59201\n Bayesian info crit = -1073.16758\n[[Variables]]\n amplitude: 3590.97370 +/- 125.195985 (3.49%) (init = 781.4034)\n center: 317.738344 +/- 3.57625327 (1.13%) (init = 322.4771)\n sigma: 361.440601 +/- 13.6760525 (3.78%) (init = 181.5)\n fwhm: 851.127556 +/- 32.2046420 (3.78%) == '2.3548200*sigma'\n height: 3.96355944 +/- 0.01657428 (0.42%) == '0.3989423*amplitude/max(1e-15,sigma)'\n[[Correlations]] (unreported correlations are < 0.100)\n C(amplitude,sigma) = 0.997"
跟我使用的猜测函数有关系吗?
解决方法
评论中给出的提示似乎不够。您需要在模型中包含偏移量。高斯函数在远离峰值强度的地方趋于 0 - 我不知道 OriginPro 在做什么,但很明显,它的建模不仅仅是高斯函数。
尝试制作一个高斯 + 常数的模型,如下所示:
import numpy as np
import matplotlib.pyplot as plt
import lmfit
data = np.array([ 0.,0.,34.,33.,30.,25.,47.,36.,37.,41.,42.,40.,49.,44.,35.,32.,50.,29.,39.,43.,27.,48.,38.,20.,53.,45.,62.,31.,46.,56.,57.,51.,55.,67.,68.,66.,60.,63.,58.,72.,64.,61.,71.,74.,69.,59.,70.,73.,65.,52.,81.,80.,84.,54.,23.,24.,28.,21.,1.])
data = data[140:510]
ipos = np.where(data>0)[0]
data = np.log(data[ipos])
x = np.arange(140,510,1.0)[ipos]
mod = lmfit.models.GaussianModel() + lmfit.models.ConstantModel()
pars = mod.make_params(c=data.mean(),center=x.mean(),sigma=x.std(),amplitude=x.std()*data.ptp())
out = mod.fit(data,pars,x=x)
print(out.fit_report())
plt.plot(x,out.best_fit,'r',label='Gaussian Model')
plt.plot(x,data)
plt.show()
这将打印一份报告
[Model]]
(Model(gaussian) + Model(constant))
[[Fit Statistics]]
# fitting method = leastsq
# function evals = 48
# data points = 333
# variables = 4
chi-square = 9.50683177
reduced chi-square = 0.02889615
Akaike info crit = -1176.19189
Bayesian info crit = -1160.95932
[[Variables]]
amplitude: 58.7086539 +/- 5.27950257 (8.99%) (init = 153.376)
center: 302.462920 +/- 2.79518589 (0.92%) (init = 322.6336)
sigma: 48.1997878 +/- 3.66967747 (7.61%) (init = 106.8759)
c: 3.64131210 +/- 0.01679060 (0.46%) (init = 3.796494)
fwhm: 113.501824 +/- 8.64142989 (7.61%) == '2.3548200*sigma'
height: 0.48592258 +/- 0.02641186 (5.44%) == '0.3989423*amplitude/max(1e-15,sigma)'
[[Correlations]] (unreported correlations are < 0.100)
C(amplitude,c) = -0.832
C(amplitude,sigma) = 0.798
C(sigma,c) = -0.664
和一个情节