2014-05-12 113 views
10

我正在努力完成一項相當簡單的任務。我有花車,而我想,以適應高斯混合模型有兩個高斯核的載體:如何在scikit-learn下繪製擬合高斯混合模型的概率密度函數?

from sklearn.mixture import GMM 

gmm = GMM(n_components=2) 
gmm.fit(values) # values is numpy vector of floats 

我現在想爲陰謀我創建的混合模型的概率密度函數,但我似乎無法找到有關如何執行此操作的任何文檔。我應該如何最好地繼續?

編輯:

Here是我擬合數據的載體。以下是我如何做事更詳細的例子:

from sklearn.mixture import GMM 
from matplotlib.pyplot import * 
import numpy as np 

try: 
    import cPickle as pickle 
except: 
    import pickle 

with open('/path/to/kde.pickle') as f: # open the data file provided above 
    kde = pickle.load(f) 

gmm = GMM(n_components=2) 
gmm.fit(kde) 

x = np.linspace(np.min(kde), np.max(kde), len(kde)) 

# Plot the data to which the GMM is being fitted 
figure() 
plot(x, kde, color='blue') 

enter image description here

# My half-baked attempt at replicating the scipy example 
fit = gmm.score_samples(x)[0] 
plot(x, fit, color='red') 

擬合曲線看起來並不像我期望什麼東西。它看起來不像高斯,考慮到它是由高斯過程產生的,這有點奇怪。我瘋了嗎?

+1

使用'積(X,np.exp(FIT),顏色= '紅色')'代替。因爲'gmm.score_samples'給出了'log'概率。 – emeth

+0

@blz數據向量的鏈接已過期。 – shahensha

回答

1

我跟着這個線程和其他人提到的一些例子,並設法接近解決方案,但最終的概率密度函數沒有整合到一個。我想,我會在另一個線程中發佈這個問題。

import ntumpy as np 
import matplotlib.pyplot as plt 
from sklearn.mixture import GaussianMixture 

np.random.seed(1) 

mus = np.array([[0.2], [0.8]]) 
sigmas = np.array([[0.1], [0.1]]) ** 2 
gmm = GaussianMixture(2) 
gmm.means_ = mus 
gmm.covars_ = sigmas 
gmm.weights_ = np.array([0.5, 0.5]) 

#Fit the GMM with random data from the correspondent gaussians 
gaus_samples_1 = np.random.normal(mus[0], sigmas[0], 10).reshape(10,1) 
gaus_samples_2 = np.random.normal(mus[1], sigmas[1], 10).reshape(10,1) 
fit_samples = np.concatenate((gaus_samples_1, gaus_samples_2)) 
gmm.fit(fit_samples) 

fig = plt.figure() 
ax = fig.add_subplot(111) 
x = np.linspace(0, 1, 1000).reshape(1000,1) 
logprob = gmm.score_samples(x) 
pdf = np.exp(logprob) 
#print np.max(pdf) -> 19.8409464401 !? 
ax.plot(x, pdf, '-k') 
plt.show() 

Here is the resulting plot

+0

這回答了我有關在概率密度函數中大於1的值的問題:https://math.stackexchange.com/questions/105455/how-can-a- probbability -density待大於-一和整合到一個 – rauldg