我正在使用 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-means 不同,DPGMM 是一个无限混合模型,使用狄利克雷过程作为簇数的先验分布。
我的 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
请您参考如下方法:
目前还没有自动方法可以这样做,但您可以查看估计的 weights_ 属性并修剪具有较小值(例如低于 0.01)的组件。
编辑:你可以计算模型有效使用的组件数量:
model = BayesianGaussianMixture(n_components=30).fit(X)
print("active components: %d" % np.sum(model.weights_ > 0.01)
这应该打印出一些低于提供的上限(在本例中为 30)的事件组件。
编辑 2:n_components 参数指定模型可以使用的最大组件数。模型实际使用的组件的有效数量可以通过在拟合结束时内省(introspection) weigths_ 属性来检索。这主要取决于数据的结构和 weight_concentration_prior 的值(尤其是在样本数量较少的情况下)。
