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

2024-04-25 06:46:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些从两个正态分布中提取的一维数据。我的目标是估计两个不同的高斯分量。你知道吗

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

My data

我使用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)

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


Tags: 规范datamynppltsqrt直方图mean
1条回答
网友
1楼 · 发布于 2024-04-25 06:46:36

更新:

我解决了我的问题,通过缩放每个组件的重量,并将其覆盖在非标准化的直方图上,我用它的箱子的总面积来缩放它。你知道吗

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)

相关问题 更多 >