📜  毫升 |层次聚类(凝聚和分裂聚类)

📅  最后修改于: 2022-05-13 01:55:17.250000             🧑  作者: Mango

毫升 |层次聚类(凝聚和分裂聚类)

在数据挖掘和统计中,层次聚类分析是一种聚类分析方法,旨在建立聚类的层次结构,即基于层次结构的树型结构。

基本上,有两种类型的层次聚类分析策略——

1.凝聚聚类:也称为自下而上的方法或层次凝聚聚类(HAC)。一种结构,它比平面聚类返回的非结构化聚类集提供更多信息。这种聚类算法不需要我们预先指定聚类的数量。自下而上的算法在开始时将每个数据视为一个单独的集群,然后依次聚集成对的集群,直到所有集群都合并为一个包含所有数据的集群。

算法 :

given a dataset (d1, d2, d3, ....dN) of size N
# compute the distance matrix
for i=1 to N:
   # as the distance matrix is symmetric about 
   # the primary diagonal so we compute only lower 
   # part of the primary diagonal 
   for j=1 to i:
      dis_mat[i][j] = distance[di, dj] 
each data point is a singleton cluster
repeat
   merge the two cluster having minimum distance
   update the distance matrix
until only a single cluster remains

使用 scikit-learn 库对上述算法的Python实现:

Python3
from sklearn.cluster import AgglomerativeClustering
import numpy as np
 
# randomly chosen dataset
X = np.array([[1, 2], [1, 4], [1, 0],
              [4, 2], [4, 4], [4, 0]])
 
# here we need to mention the number of clusters
# otherwise the result will be a single cluster
# containing all the data
clustering = AgglomerativeClustering(n_clusters = 2).fit(X)
 
# print the class labels
print(clustering.labels_)


输出 :

[1, 1, 1, 0, 0, 0]

2. 分裂聚类:也称为自上而下的方法。该算法也不需要预先指定集群的数量。自上而下的聚类需要一种方法来拆分包含整个数据的集群,然后递归地拆分集群,直到将单个数据拆分为单个集群。

算法 :

given a dataset (d1, d2, d3, ....dN) of size N
at the top we have all data in one cluster
the cluster is split using a flat clustering method eg. K-Means etc
repeat
choose the best cluster among all the clusters to split
split that cluster by the flat clustering algorithm
until each data is in its own singleton cluster

分层凝聚分裂聚类——

  • 与凝聚聚类相比,分裂聚类更复杂,因为在分裂聚类的情况下,我们需要一种平面聚类方法作为“子程序”来分割每个聚类,直到我们让每个数据都有自己的单例聚类。
  • 如果我们不生成完整的层次结构,一直到单个数据叶,分裂聚类会更有效。朴素凝聚聚类的时间复杂度为O(n 3 ) ,因为我们在每次 N-1 次迭代中详尽地扫描 N x N 矩阵 dist_mat 以获得最短距离。使用优先队列数据结构,我们可以将这种复杂性降低到O(n 2 logn) 。通过使用更多优化,它可以降低到O(n 2 ) 。而对于给定固定数量的顶层的分裂聚类,使用有效的平面算法(如 K-Means),分裂算法在模式和聚类的数量上是线性的。
  • 除法算法也更准确。凝聚聚类通过考虑局部模式或相邻点来做出决策,而不是最初考虑数据的全局分布。这些早期决定无法撤销。而分裂聚类在做出顶级分区决策时会考虑数据的全局分布。