【问题标题】:Least Square fit for Gaussian in PythonPython中的最小二乘拟合高斯
【发布时间】:2021-09-15 08:20:13
【问题描述】:

我尝试使用给定数据在 Python 中实现高斯拟合。但是,我无法获得所需的配合。任何建议都会有所帮助。

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from scipy.optimize import curve_fit
from scipy import asarray as ar, exp

xData=ar([-7.66E-06,-7.60E-06,-7.53E-06,-7.46E-06,-7.40E-06,-7.33E-06,-7.26E-06,-7.19E-06,-7.13E-06,-7.06E-06,-6.99E-06,
-6.93E-06,-6.86E-06,-6.79E-06,-6.73E-06,-6.66E-06,-6.59E-06,-6.52E-06,-6.46E-06,-6.39E-06,-6.32E-06,-6.26E-06,-6.19E-06,
-6.12E-06,-6.06E-06,-5.99E-06,-5.92E-06,-5.85E-06,-5.79E-06,-5.72E-06])
yData=ar([17763,2853,3694,4203,4614,4984,5080,7038,6905,8729,11687,13339,14667,16175,15953,15342,14340,15707,13001,10982,8867,6827,5262,4760,3869,3232,2835,2746,2552,2576])
#plot the data points
plt.plot(xData,yData,'bo',label='experimental_data')
plt.show()
#define the function we want to fit the plot into
# Define the Gaussian function
n = len(xData)
mean = sum(xData*yData)/n
sigma = np.sqrt(sum(yData*(xData-mean)**2)/n)
def Gauss(x,I0,x0,sigma,Background):
    return I0*exp(-(x-x0)**2/(2*sigma**2))+Background

popt,pcov = curve_fit(Gauss,xData,yData,p0=[1,mean,sigma, 0.0])
print(popt)
plt.plot(xData,yData,'b+:',label='data')
plt.plot(xData,Gauss(xData,*popt),'ro:',label='fit')
plt.legend()
plt.title('Gaussian_Fit')
plt.xlabel('x-axis')
plt.ylabel('PL Intensity')
plt.show()

【问题讨论】:

    标签: python scipy gaussian least-squares scipy-optimize


    【解决方案1】:

    计算meansigma 时,除以sum(yData),而不是n

    mean = sum(xData*yData)/sum(yData)
    sigma = np.sqrt(sum(yData*(xData-mean)**2)/sum(yData))
    

    原因是,比如说mean,您需要计算xData 的平均值,以yData 加权。为此,您需要规范化 yData 使总和为 1,即,您需要将 xDatayData / sum(yData) 相乘并取和。

    【讨论】:

      【解决方案2】:

      使用correction by j1-lee 并删除明显不符合高斯模型的第一个点,拟合如下所示:

      移除明显不属于拟合的 bin 会将拟合宽度降低约 20%,并将(拟合)噪声与背景的比率降低约 30%。均值仅受到轻微影响。

      【讨论】:

        猜你喜欢
        • 2012-04-25
        • 1970-01-01
        • 2020-10-02
        • 1970-01-01
        • 2013-01-21
        • 1970-01-01
        • 1970-01-01
        • 2017-02-24
        • 1970-01-01
        相关资源
        最近更新 更多