2017-02-20 48 views
0

我想繪製高斯混合模型。下面的代碼允許我繪製兩個獨立的高斯,但是它們相交的地方,線條非常銳利而且不夠光滑。有沒有辦法繪製一維GMM的pdf?如何使用matplotlib繪製一維高斯混合模型的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') 
+0

你首先可能要找出如何高斯混合的概率密度函數模型看起來像:) – cel

回答

0

你必須形成密度的凸組合

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

其中p的概率是一個樣品來自第一高斯。

1

你可以從字面上高斯混合模型得出樣品並繪製經驗密度/直方圖太:

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

相關問題