什么是 scipy 集群层次结构?如何将层次聚类切分为平面聚类?

scipy.cluster.hierarchy 模块提供了层次聚类及其类型的功能,例如凝聚聚类。它有各种我们可以使用的例程 -

  • 将层次聚类切分为平面聚类。

  • 实施凝聚聚类。

  • 计算层次结构的统计信息

  • 可视化平面聚类。

  • 检查两个平面集群分配的同构。

  • 绘制聚类。

例程scipy.cluster.hierarchy.fcluster用于将层次聚类切割为平面聚类,他们将原始数据点分配给单个聚类,从而获得平面聚类。让我们在下面给出的例子的帮助下理解这个概念 -

示例

#Importing the packages
from scipy.cluster.hierarchy import ward, fcluster
from scipy.spatial.distance import pdist

#The cluster linkage method i.e., scipy.cluster.hierarchy.ward will generate a linkage matrix as their output:
A = [
   [0, 0], [0, 1], [1, 0],
   [0, 3], [0, 2], [1, 4],
   [3, 0], [2, 0], [4, 1],
   [3, 3], [2, 3], [4, 3]
]
X = ward(pdist(A))
print(X)
输出结果
[[ 0.   1.   1.           2. ]
 [ 2.   7.   1.           2. ]
 [ 3.   4.   1.           2. ]
 [ 9.  10.   1.           2. ]
 [ 6.   8.   1.41421356   2. ]
 [11.  15.   1.73205081   3. ]
 [ 5.  14.   2.081666     3. ] 
 [12.  13.   2.23606798   4. ]
 [16.  17.   3.94968353   5. ]
 [18.  19.   5.15012714   7. ]
 [20.  21.   6.4968857   12. ]]

在上述输出中接收到的矩阵 X 表示一个树状图。在这个树状图中,第一个和第二个元素是在每一步合并的两个集群。这些簇之间的距离由上述树状图的第三个元素给出。新簇的大小由第四个元素提供。

#Flatting the dendrogram by using fcluster() where the assignation of the original
data points to single clusters mostly depend on the distance threshold t.
fcluster(X, t=1.5, criterion='distance') #when t= 1.5
输出结果
array([6, 6, 7, 4, 4, 5, 1, 7, 1, 2, 2, 3], dtype=int32)

示例

fcluster(X, t=0.9, criterion='distance') #when t= 0.9
输出结果
array([ 9, 10, 11, 6, 7, 8, 1, 12, 2, 3, 4, 5], dtype=int32)

示例

fcluster(X, t=9, criterion='distance') #when t= 9
输出结果
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=int32)