简体   繁体   English

使用 Lmfit 对 Python 中的数据进行高斯拟合

[英]Gaussian Fit to Data in Python using Lmfit

I'm trying to write a code that performs a Gaussian fit to a gamma ray calibration spectrum, ie multiple peaks.我正在尝试编写一个代码来执行对伽马射线校准光谱的高斯拟合,即多个峰值。 Here is my current code:这是我当前的代码:

from numpy import loadtxt
import numpy as np


from lmfit.models import GaussianModel
import matplotlib.pyplot as plt



#Centre of each of the peaks we want to fit Gaussian to
peakCentroids = np.array([251, 398, 803, 908, 996, 1133, 1178, 2581, 3194, 3698, 4671])


#Import total data set
data = loadtxt('Phase_1_02.dat')
x = data[:, 0] #channel number/gamma energy
y = data[:, 1] #Count number

mod = GaussianModel()

pars = mod.guess(y, x=x)
out = mod.fit(y, pars, x=x)

print(out.fit_report(min_correl=0.25))



for currentPeakNumber in range(len(peakCentroids)):
     fig = plt.figure(figsize = (8, 8))
    
plt.plot(x, y, 'b')
plt.plot(x, out.init_fit, 'k--', label='initial fit')
plt.plot(x, out.best_fit, 'r-', label='best fit')
plt.legend(loc='best')
plt.show()


It's outputting the spectra for my data and printing the relevant parameters (eg center, sigma, fwhm etc.), but I'm having a bit of brain freeze in terms of fitting the Gaussian peak to each of the centroids specified.它正在为我的数据输出光谱并打印相关参数(例如中心、西格玛、fwhm 等),但是在将高斯峰拟合到每个指定的质心方面,我有点大脑冻结。 Currently the output spectra is only fitting to the first peak at a value of about 248?目前 output 光谱仅适合第一个峰值,值约为 248? Is there anyone much better at coding in Python than me that can shed some light on how to go about this and if it's possible using Lmfit please: Thanks in advance!!有没有人比我更擅长在 Python 中编码,可以阐明如何 go 以及是否可以使用 Lmfit 请:提前致谢! :) :)

If I understand the question correctly, you are looking to model the data you have with a series of Gaussian line shapes, centered at the many (10 or more) values you have.如果我正确理解了这个问题,您正在寻找 model 数据,这些数据具有一系列高斯线形,以您拥有的许多(10 个或更多)值为中心。

If that is the case, the model should be constructed from the 10 or more Gaussians, but your model only builds one Gaussian.如果是这种情况,model 应该由 10 个或更多高斯构成,但您的 model 只构建一个高斯。 You'll want to build a model with something like你会想用类似的东西构建一个 model

import numpy as np
from lmfit.models import GaussianModel

peakCentroids = np.array([251.0, 398, 803, 908, 996, 1133, 1178, 2581, 3194,
                          3698, 4671.0])

mod = None 

for i, cen in enumerate(peakCentroids):
     thispeak = GaussianModel(prefix='p%d_' %(i+1)) 
     if mod is None:  
          mod = thispeak
     else:
          mod = mod + thispeak

pars = mod.make_params()

for i, cen in enumerate(peakCentroids):
    pars['p%d_center' % (i+1)].value = cen
    pars['p%d_amplitude' % (i+1)].value = 1.0 # is that a sensible initial value?
    pars['p%d_sigma' % (i+1)].value = 1.0 # is that a sensible initial value?

out = mod.fit(y, pars, x=x)
print(out.fit_report(min_correl=0.25))

or at least that might be a good place to start...或者至少这可能是一个很好的起点......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM