繁体   English   中英

在直方图上绘制来自 GMM 的估计高斯分量

[英]Plot the estimated Gaussian components from GMM on histogram

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

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)

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

更新:

我通过将每个组件缩放到其权重来解决我的问题,并将其覆盖在非规范直方图上,我用其 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)

暂无
暂无

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

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