How to plot the pdf of a 1D Gaussian Mixture Model with matplotlib

浪尽此生 提交于 2019-12-02 07:23:18

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()

You have to form a convex combination of the densities.

curve = p*curve_0 + (1-p)*curve_1

where p is the probability that a sample comes from the first Gaussian.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!