如何正确删除Scikit-Learn的DPGMM的冗余组件?

EB2*_*127 4 python statistics machine-learning python-3.x scikit-learn

我正在使用 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 均值(用户先验设置聚类数量“k”)相反,DPGMM 是一种无限混合模型,使用狄利克雷过程作为聚类数量的先验分布。

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

Scikit-Learn 的 DPGMM 拟合:组件数量?

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

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
Run Code Online (Sandbox Code Playgroud)

ogr*_*sel 6

目前还没有自动方法可以执行此操作,但您可以查看估计weights_属性并修剪具有较小值(例如低于 0.01)的组件。

编辑:计算模型有效使用的组件数量,您可以执行以下操作:

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

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

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