【问题标题】:How to properly remove redundant components for Scikit-Learn's DPGMM?如何正确删除 Scikit-Learn 的 DPGMM 的冗余组件?
【发布时间】:2017-03-05 00:58:08
【问题描述】:

我正在使用 scikit-learn 来实现狄利克雷过程高斯混合模型:

https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/mixture/dpgmm.py http://scikit-learn.org/stable/modules/generated/sklearn.mixture.BayesianGaussianMixture.html

也就是说,它是sklearn.mixture.BayesianGaussianMixture(),默认设置为weight_concentration_prior_type = 'dirichlet_process'。与 k-means 不同,在 k-means 中,用户先验地设置簇数“k”,DPGMM 是一个无限混合模型,Dirichlet 过程作为簇数的先验分布。

我的 DPGMM 模型始终将确切的簇数输出为n_components。正如这里所讨论的,处理这个问题的正确方法是使用predict(X)“减少冗余组件”:

Scikit-Learn's DPGMM fitting: number of components?

但是,链接到的示例实际上并未删除冗余组件并显示数据中“正确”的集群数量。相反,它只是绘制了正确数量的集群。

http://scikit-learn.org/stable/auto_examples/mixture/plot_gmm.html

用户如何实际删除冗余组件,并输出一个数组,这些组件应该包含哪些?这是删除冗余集群的“官方”/唯一方法吗?

这是我的代码:

>>> import pandas as pd 
>>> import numpy as np 
>>> import random
>>> from sklearn import mixture  
>>> X = pd.read_csv(....)   # my matrix
>>> X.shape
(20000, 48) 
>>> dpgmm3 = mixture.BayesianGaussianMixture(n_components = 20, weight_concentration_prior_type='dirichlet_process', max_iter = 1000, verbose = 2) 
>>> dpgmm3.fit(X) # Fitting the DPGMM model
>>> labels = dpgmm3.predict(X) # Generating labels after model is fitted
>>> max(labels)
>>> np.unique(labels) #Number of lab els == n_components specified above
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19])

#Trying with a different n_components

>>> dpgmm3_1 = mixture.BayesianGaussianMixture( weight_concentration_prior_type='dirichlet_process', max_iter = 1000) #not specifying n_components
>>> dpgmm3_1.fit(X)
>>> labels_1 = dpgmm3_1.predict(X)  
>>> labels_1
array([0, 0, 0, ..., 0, 0, 0]) #All were classified under the same label

#Trying with n_components = 7

>>> dpgmm3_2 = mixture.BayesianGaussianMixture(n_components = 7, weight_concentration_prior_type='dirichlet_process', max_iter = 1000)
>>> dpgmm3_2.fit()

>>> labels_2 = dpgmm3_2.predict(X)
>>> np.unique(labels_2)
array([0, 1, 2, 3, 4, 5, 6]) #number of labels == n_components

【问题讨论】:

    标签: python python-3.x machine-learning statistics scikit-learn


    【解决方案1】:

    目前还没有自动化的方法可以做到这一点,但您可以查看估计的 weights_ 属性并修剪具有较小值(例如低于 0.01)的组件。

    编辑:你可以计算模型有效使用的组件数量:

    model = BayesianGaussianMixture(n_components=30).fit(X)
    print("active components: %d" % np.sum(model.weights_ > 0.01)
    

    这应该打印低于提供的上限(本例中为 30)的活动组件数。

    编辑 2n_components 参数指定模型可以使用的最大组件数。模型实际使用的组件的有效数量可以通过在拟合结束时自省weigths_ 属性来检索。它主要取决于数据的结构和weight_concentration_prior 的值(尤其是在样本数量很少的情况下)。

    【讨论】:

    • 谢谢,@ogrisel。向 API 添加一些 cmets 可能很有用。我使用 DPGMM 的唯一动机是“我想尝试一种不需要先验地指定集群数量的算法”。对于 DPGMM,我接受(不合理地)我们正在工作的假设来自高斯。有没有什么准自动化的方法浮现在脑海中?
    • 在实践中模型会忽略weights_ 非常接近于零的组件。所以在实践中你只需要为混合物中的成分数量指定一个上限,DP-GMM 算法就不会使用它不需要的成分(根据先验)。
    • 我现在有点困惑。 “DP-GMM 算法不会使用它不需要的组件(根据之前的说法)”这不是我上面所做的吗?我预计数据中有 3-6 个集群。我设置了n_components=7,我得到了 7 个集群。我设置了n_components=20,我得到了 20 个集群。我设置了n_components=30,我得到了 30 个集群。
    • 否,如果您没有为 n_components 指定值,它将使用默认值:n_components=1,如文档中所述:scikit-learn.org/stable/modules/generated/…n_components 参数是指定模型可以使用的最大组件数。正如我解释的那样,模型实际使用的组件的有效数量可以通过在调用fit 之后修剪weights_ 属性来找到。尝试将 n_components 的值设置为足够大的值(例如 30)并尝试为 weight_concentration_prior 设置不同的值。
    【解决方案2】:

    查看 [1] 中描述的排斥高斯混合。 它们试图拟合具有较少重叠的高斯混合,因此通常冗余较少。

    我还没有找到它的源代码。

    [1]https://papers.nips.cc/paper/4589-repulsive-mixtures.pdf

    【讨论】:

    • 上面的源码有实现过吗?
    猜你喜欢
    • 2016-11-26
    • 2023-03-29
    • 2018-01-19
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-17
    相关资源
    最近更新 更多