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

前端 未结 2 1471
伪装坚强ぢ
伪装坚强ぢ 2021-01-24 17:01

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 th

2条回答
  •  無奈伤痛
    2021-01-24 17:42

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

提交回复
热议问题