如何在scikit-learn下绘制拟合高斯混合模型的概率密度函数? [英] How can I plot the probability density function for a fitted Gaussian mixture model under scikit-learn?

查看:44
本文介绍了如何在scikit-learn下绘制拟合高斯混合模型的概率密度函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力完成一个相当简单的任务.我有一个浮点向量,我想用两个高斯核拟合高斯混合模型:

from sklearn.mixture import GMMgmm = GMM(n_components=2)gmm.fit(values)#values是浮点数的numpy向量

我现在想为我创建的混合模型绘制概率密度函数,但是我似乎找不到任何有关如何执行此操作的文档.我应该如何最好地进行?

这个想法是生成meshgrid,从gmm 中获取它们的score,并绘制它.

示例显示

I'm struggling with a rather simple task. I have a vector of floats to which I would like to fit a Gaussian mixture model with two Gaussian kernels:

from sklearn.mixture import GMM

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

I would now like to plot the probability density function for the mixture model I've created, but I can't seem to find any documentation on how to do this. How should I best proceed?

Edit:

Here is the vector of data I'm fitting. And below is a more detailed example of how I'm doing things:

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

The fitted curve doesn't look anything like what I'd expect. It doesn't even seem Gaussian, which is a bit strange given it was produced by a Gaussian process. Am I crazy?

解决方案

Take a look at the one of scikit-learn examples on Github

https://github.com/scikit-learn/scikit-learn/blob/master/examples/mixture/plot_gmm_pdf.py

The idea is to generate meshgrid, get their score from the gmm, and plot it.

The example shows

这篇关于如何在scikit-learn下绘制拟合高斯混合模型的概率密度函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆