python - 从层次聚类的不同级别获取标签

标签 python machine-learning scipy hierarchical-clustering

我正在致力于实现集群自适应学习,如本文paper中所提议的那样。为了实现层次聚类,我使用了以下内容:

X = sp.hstack((title, abstract), format='csr')
Z = ward(X.todense())

如果这是为了创建像这样的树状图:

dendogram (摘自上述论文)

然后我可以使用以下行:

clusters = fcluster(Z, k=2, criterion='maxclust')

获取每个X所属的类(即图中的2或3),表示为0或1。

如何拆分组,以便为​​每个 X 获取多个级别的标签。例如,如何从 2、4 和 9 或 3 获取每个 X 所属的类的列表, 5和6。

最佳答案

使用

fcluster(Z, height, criterion='distance')

其中 level 是一个整数 (0, 1, 2, ... ),表示 cophenetic distance允许在扁平簇内。

The cophenetic distance between two objects is the height of the dendrogram where the two branches that include the two objects merge into a single branch

这意味着:

  • height = 0 不执行任何操作,您会得到单点簇,每个原始值对应一个。不太有用。
  • height = 1 执行第一级合并,即原始条目之间的合并
  • height = 2 执行二级合并,即一级集群之间的合并。

我在 10 个元素上生成了一些链接 Z 并运行

[fcluster(Z, height, criterion='distance') for height in range(5)]

为了说明这一点:

  [10,5, 3, 1, 6, 4, 7, 8, 9, 2]   <- level 0, everyone separate
  [6, 3, 2, 1, 3, 2, 4, 4, 5, 1]   <- 6 clusters
  [4, 3, 2, 1, 3, 2, 3, 3, 3, 1]   <- 4 clusters
  [3, 3, 2, 1, 3, 2, 3, 3, 3, 1]   <- 3 clusters
  [2, 2, 2, 1, 2, 2, 2, 2, 2, 1]   <- 2 clusters

关于python - 从层次聚类的不同级别获取标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47934170/

相关文章:

python - 如何按不同日期对 pandas DataFrame 进行分组?

matlab - 为 MATLAB 追加

artificial-intelligence - 当输入数量可变时如何使用神经网络?

python - 不近似求解不正确积分

python - 添加#noqa : F841后F841仍然存在

Python OOP问题,一个类的值没有被另一个类的函数更新

python - 在 Python 中将索引列表传递到另一个列表。语法正确吗?

machine-learning - 机器学习中如何处理超过二维的输入数据

多变量的 Python 最小二乘法

python - matplotlib 直方图中的分箱是否存在错误?或者 scipy.stats 中 rvs 方法的非随机性