gpt4 book ai didi

python - 如何使用 matplotlib 绘制一维高斯混合模型的 pdf

转载 作者:行者123 更新时间:2023-12-01 03:17:25 26 4
gpt4 key购买 nike

我想绘制高斯混合模型。下面的代码允许我绘制两个独立的高斯曲线,但是在它们相交的地方,线条非常尖锐并且不够平滑。有没有办法绘制一维 GMM 的 pdf?

def plot_data():
mu = [-6, 5]
var = [2, 3]
sigma = [np.sqrt(var[0]), np.sqrt(var[1])]
x = np.linspace(-10, 10, 100)
curve_0 = mlab.normpdf(x, mu[0], sigma[0])
curve_1 = mlab.normpdf(x, mu[1], sigma[1])
import ipdb; ipdb.set_trace()
plt.plot(x, curve_0, color='grey')
plt.plot(x, curve_1, color='grey')
plt.fill_between(x,curve_0 , color='grey')
plt.fill_between(x,curve_1, color='grey')
plt.show()
plt.savefig('data_t0.jpg')

最佳答案

您实际上可以从高斯混合模型中提取样本并绘制经验密度/直方图:

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
n = 10000 # number of sample to be drawn
mu = [-6, 5]
sigma = [2, 3]
samples = []
for i in range(n): # iteratively draw samples
Z = np.random.choice([0,1]) # latent variable
samples.append(np.random.normal(mu[Z], sigma[Z], 1))
sns.distplot(samples, hist=False)
plt.show()
sns.distplot(samples)
plt.show()

enter image description here

关于python - 如何使用 matplotlib 绘制一维高斯混合模型的 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42339786/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com