如何使用matplotlib绘制一维高斯混合模型的pdf [英] How to plot the pdf of a 1D Gaussian Mixture Model with matplotlib
本文介绍了如何使用matplotlib绘制一维高斯混合模型的pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想绘制一个高斯混合模型.以下代码允许我绘制2个独立的高斯曲线,但是在它们相交的地方,该线非常尖锐并且不够平滑.有没有办法绘制一维GMM的pdf?
I want to plot a Gaussian Mixture Model. The following code allows me to plot 2 separate Gaussians, but where they intersect, the line is very sharp and not smooth enough. Is there a way to plot the pdf of a 1D GMM?
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')
推荐答案
您也可以从高斯混合模型中抽取样本并绘制经验密度/直方图:
You can literally draw samples from a Gaussian mixture model and plot the empirical density / histogram too:
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()
这篇关于如何使用matplotlib绘制一维高斯混合模型的pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文