使用 scikit 选择层次凝聚聚类中的簇数

Choosing the number of clusters in heirarchical agglomerative clustering with scikit

关于 determining the number of clusters in a dataset indicated that I do not need to worry about such a problem when using hierarchical clustering. However when I tried to use scikit-learn's agglomerative clustering 的维基百科文章我看到我必须将集群的数量作为参数提供给它 "n_clusters" - 没有它我得到两个集群的硬编码默认值。在这种情况下,我该如何为数据集选择正确数量的集群?维基文章有错吗?

维基百科简直是极度简化,与现实生活毫无关系。层次聚类 无法避免聚类数量 的问题。简单地说 - 它构建了跨越 所有样本 的树,它显示了哪些样本(稍后 - 聚类)合并在一起以创建更大的聚类。这递归地发生,直到你只有两个集群(这就是默认集群数为 2 的原因),它们被合并到整个数据集。你只剩下 "cutting" 通过树来获得实际的聚类。一旦适合 AgglomerativeClustering,您就可以遍历整棵树并分析要保留哪些集群

import numpy as np
from sklearn.cluster import AgglomerativeClustering
import itertools

X = np.concatenate([np.random.randn(3, 10), np.random.randn(2, 10) + 100])
clustering = AgglomerativeClustering()
clustering.fit(X)

[{'node_id': next(itertools.count(X.shape[0])), 'left': x[0], 'right':x[1]} for x in clustering.children_]

ELKI(不是 scikit-learn,而是 Java)有许多从层次聚类中提取聚类的高级方法。它们比仅在特定高度切割树更聪明,但它们可以产生最小尺寸的簇层次结构,例如。

您可以检查一下这些方法是否适合您。