簡體   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