【发布时间】:2014-06-29 20:36:03
【问题描述】:
我正在为一项相当简单的任务而苦苦挣扎。我有一个浮点向量,我想用两个高斯核拟合一个高斯混合模型:
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')
# My half-baked attempt at replicating the scipy example
fit = gmm.score_samples(x)[0]
plot(x, fit, color='red')
拟合曲线与我预期的完全不同。它甚至看起来都不是高斯的,这有点奇怪,因为它是由高斯过程产生的。我疯了吗?
【问题讨论】:
-
改用
plot(x, np.exp(fit), color='red')。因为gmm.score_samples给出了log的概率。 -
@blz 指向数据向量的链接已过期。
标签: python matplotlib scikit-learn