【问题标题】:Plot the estimated Gaussian components from GMM on histogram在直方图上绘制来自 GMM 的估计高斯分量
【发布时间】:2019-06-24 20:08:19
【问题描述】:

我有一些从两个正态分布中检索到的一维数据。我的目标是估计两个不同的高斯分量。

plt.hist(my_data, bins=100, edgecolor= 'white' normed=False)

我使用 GMM(高斯混合模型)。

clf = mixture.GaussianMixture(n_components=2)
clf.fit(my_data)

我检索我的两个高斯函数。

mean_1 = clf.means_[0][0]
mean_2 = clf.means_[1][0]
std_1 = np.sqrt(clf.covariances_[0][0])[0]
std_2 = np.sqrt(clf.covariances_[1][0])[0]
weight_1 = weights[0]
weight_2 = weights[1]

现在问题是,我想用上面的高斯参数覆盖直方图。我想我首先必须对直方图进行标准化,但是如何绘制它们以便每个高斯权重的面积正确且总面积等于 1,以及如何覆盖在非标准化直方图之上?

xmin, xmax = plt.xlim()
x = np.linspace(xmin, xmax, 500)
y = norm.pdf(x, mean_1, std_1)
plt.plot(x,y)

y = norm.pdf(x, mean_2, std_2)
plt.plot(x,y)

上面的代码块给了我两个标准化的高斯图,但它们都有相同的面积。

【问题讨论】:

  • 可以添加一些数据吗?
  • 您可能希望发布解决您的问题的更新作为对这个问题的回答。

标签: python scikit-learn scipy gaussian gmm


【解决方案1】:

更新:

我通过将每个组件缩放到其权重来解决我的问题,并将其覆盖在非规范直方图上,我用它的 bin 的总面积对其进行了缩放。

val, bins, _ = plt.hist(my_data, bins=100,  edgecolor = 'white', 
               normed=False)

area = sum(np.diff(bins)*val)  +  sum(np.diff(bins)*val)

y = norm.pdf(x, mean_1, std_1)*weight_1*area
plt.plot(x,y)

y = norm.pdf(x, mean_2, std_2)*weight_2*area
plt.plot(x,y)

【讨论】:

    猜你喜欢
    • 2017-09-16
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    相关资源
    最近更新 更多