简体   繁体   English

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

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

I have some 1-D data that is retrieved from two normal distributions.我有一些从两个正态分布中检索的一维数据。 My goal is to estimate the two different gaussian components.我的目标是估计两个不同的高斯分量。

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

我的资料

I use a GMM (Gaussian Mixture model).我使用 GMM(高斯混合模型)。

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

I retrive my two gaussians.我检索我的两个高斯。

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]

Now to the question, I would like to overlay the histogram with gaussian parameters that i have above.现在的问题,我想用我上面的高斯参数覆盖直方图。 I guess that I first have to norm the histogram but how do I plot them so that the area of each gaussian weights correctly and that the total area equals to 1, and how do i overlay on top of the non-normed histogram?我想我首先必须对直方图进行规范,但是如何绘制它们以便每个高斯权重的面积正确且总面积等于 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)

The above code block gives me two normed gaussians plots but they both have the same area.上面的代码块给了我两个规范的高斯图,但它们都有相同的面积。

UPDATE:更新:

I solved my issue by scaling each component to its weight, and to overlay it on the non-normed histogram I scaled it with the total area of its bins.我通过将每个组件缩放到其权重来解决我的问题,并将其覆盖在非规范直方图上,我用其 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