我正在努力完成一项相当简单的任务。我有一个浮点数向量,我想用两个高斯核来拟合高斯混合模型:
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')
拟合曲线看起来不像我期望的那样。它看起来甚至不是高斯的,这有点奇怪,因为它是由高斯过程产生的。我疯了吗?
看看这个链接:
http://www.astroml.org/book_figures/chapter4/fig_GMM_1D.html
它们展示了如何以3种不同的方式绘制1D GMM:
我遵循了这个线程和其他线程中提到的一些示例,并设法接近解决方案,但最终的概率密度函数没有集成到一个。我想,我会在另一个帖子里提出这个问题。
看看Github上的scikit学习示例之一
https://github.com/scikit-learn/scikit-learn/blob/master/examples/mixture/plot_gmm_pdf.py
其思想是生成
meshgrid
,从gmm
中获取它们的score
,并绘制出来。示例显示
相关问题 更多 >
编程相关推荐