【问题标题】:How to fit a double Gaussian distribution in Python?如何在 Python 中拟合双高斯分布?
【发布时间】:2016-01-15 16:43:57
【问题描述】:

我正在尝试使用 Python 获得数据 (link) 的双高斯分布。原始数据格式为:

对于给定的数据,我想获得图中所示峰值的两个高斯轮廓。我用下面的代码(source)试了一下:

from sklearn import mixture
import matplotlib.pyplot
import matplotlib.mlab
import numpy as np
from pylab import *
data = np.genfromtxt('gaussian_fit.dat', skiprows = 1)
x = data[:, 0]
y = data[:, 1]
clf = mixture.GMM(n_components=2, covariance_type='full')
clf.fit((y, x))
m1, m2 = clf.means_
w1, w2 = clf.weights_
c1, c2 = clf.covars_
fig = plt.figure(figsize = (5, 5))
plt.subplot(111)
plotgauss1 = lambda x: plot(x,w1*matplotlib.mlab.normpdf(x,m1,np.sqrt(c1))[0], linewidth=3)
plotgauss2 = lambda x: plot(x,w2*matplotlib.mlab.normpdf(x,m2,np.sqrt(c2))[0], linewidth=3)
fig.savefig('gaussian_fit.pdf')

但我无法获得所需的输出。那么,如何在 Python 中获得双高斯分布呢?

更新

我能够使用以下代码拟合单个高斯分布:

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

data = np.genfromtxt('gaussian_fit.dat', skiprows = 1)
x = data[:, 0]
y = data[:, 1]
n = len(x)
mean = sum(x*y)/n
sigma = sum(y*(x-mean)**2)/n


def gaus(x,a,x0,sigma):
    return a*exp(-(x-x0)**2/(2*sigma**2))


popt,pcov = curve_fit(gaus, x, y ,p0 = [1, mean, sigma])


fig = plt.figure(figsize = (5, 5))
plt.subplot(111)
plt.plot(x, y, label='Raw')
plt.plot(x, gaus(x, *popt), 'o', markersize = 4, label='Gaussian fit')
plt.xlabel('X')
plt.ylabel('Y')
plt.legend()
fig.savefig('gaussian_fit.pdf')

【问题讨论】:

  • 正如 spfrnd 的回答所暗示的那样,您应该首先问自己为什么要将高斯拟合到数据中,因为 PDF 几乎总是被定义为在其范围内具有 0 的下限(即在这里您'正在考虑拟合'负'概率)。您可以通过例如转换数据减去最小值,然后 GMM 可能会更好。如果您向我们展示您尝试的输出和相关数据,您更有可能获得拟合 GMM 的帮助。

标签: python numpy scipy scikit-learn gaussian


【解决方案1】:

您不能为此使用 scikit-learn,因为您没有处理要估计其分布的一组样本。您当然可以将曲线转换为 PDF,对其进行采样,然后尝试使用高斯混合模型对其进行拟合,但这对我来说似乎有点矫枉过正。

这是一个使用简单最小二乘曲线拟合的解决方案。为了让它工作,我必须移除背景,即忽略所有带有y < 5 的数据点,并为leastsq 提供一个良好的起始向量,可以通过数据图来估计。

寻找起始向量

最小二乘法找到的参数向量就是向量

params = [c1, mu1, sigma1, c2, mu2, sigma2]

这里,c1c2是两个高斯的比例因子,即它们的高度,mu1mu2是平均值,即峰值的水平位置,sigma1sigma2确定高斯宽度的标准差。为了找到一个起始向量,我只查看了数据图并估计了两个峰的高度(分别为 = c1c2)及其水平位置(分别为 = mu1mu1) . sigma1sigma2 被简单地设置为 1.0

代码

from sklearn import mixture
import matplotlib.pyplot
import matplotlib.mlab
import numpy as np
from pylab import *
from scipy.optimize import leastsq

data = np.genfromtxt('gaussian_fit.dat', skiprows = 1)
x = data[:, 0]
y = data[:, 1]

def double_gaussian( x, params ):
    (c1, mu1, sigma1, c2, mu2, sigma2) = params
    res =   c1 * np.exp( - (x - mu1)**2.0 / (2.0 * sigma1**2.0) ) \
          + c2 * np.exp( - (x - mu2)**2.0 / (2.0 * sigma2**2.0) )
    return res

def double_gaussian_fit( params ):
    fit = double_gaussian( x, params )
    return (fit - y_proc)

# Remove background.
y_proc = np.copy(y)
y_proc[y_proc < 5] = 0.0

# Least squares fit. Starting values found by inspection.
fit = leastsq( double_gaussian_fit, [13.0,-13.0,1.0,60.0,3.0,1.0] )
plot( x, y, c='b' )
plot( x, double_gaussian( x, fit[0] ), c='r' )

【讨论】:

  • 您能否解释一下您是如何得出初始值的。
  • 效果很好! from sklearn import mixture 实际上并不是代码运行所必需的。
猜你喜欢
  • 2021-07-28
  • 2015-04-19
  • 2017-11-12
  • 2022-01-19
  • 1970-01-01
  • 2015-10-10
  • 2019-07-15
  • 2019-06-17
  • 1970-01-01
相关资源
最近更新 更多