sklearn笔记:层次聚类画出分类树状图

使用 sklearn笔记:AgglomerativeClustering_UQI-LIUWJ的博客-CSDN博客中的凝聚层次分类

from sklearn.cluster import AgglomerativeClustering
import numpy as np

X = np.array([[1, 2], [1, 4], [1, 0],
               [4, 2], [4, 4], [4, 0]])

model = AgglomerativeClustering(distance_threshold=0, n_clusters=None)
model=model.fit(X)


counts = np.zeros(model.children_.shape[0])
'''
model.children_是一个n_sample-1*2的数组
从每个样本单独为一个簇,到所有样本是一个簇,一共需要n_sample-1轮
model.children_每一行([a,b])表示第i轮迭代,会将簇a和簇b合并

如果簇a或簇b的编号比n_sample小,那么簇a或簇b表示单个样本组成的叶子节点
如果簇a或簇b的编号比n_sample大,那么簇a或簇b表示第a-n_sample或第b-n_sample次合并后形成的新簇(非叶节点)


count[i]需要计算的是,第i轮合并后,形成的新簇的大小
'''
n_samples = len(model.labels_)
for i, merge in enumerate(model.children_):
        #merge是当前轮需要merge的两个节点的index组成的list
        #i的范围是0~n_samples-1
        current_count = 0
        for child_idx in merge:
                if child_idx < n_samples:
                    current_count += 1  
                    # 其中一个合并的簇是编号为child_idx的叶子节点,所以当前合并的样本数量仅仅+1
                else:
                    current_count += counts[child_idx - n_samples]
                    #其中一个合并的簇是第child_idx - n_samples轮合并后生成的新簇,所以当前合并的样本数量+第child_idx - n_samples轮合并后生成的新簇的大小
        counts[i] = current_count
        #第i轮合并后的新簇的大小
counts
#array([2., 2., 3., 3., 6.])





model.distances_
#array([2.        , 2.        , 3.46410162, 3.46410162, 5.19615242])
#第i轮合并的两个簇之间的距离

linkage_matrix = np.column_stack(
        [model.children_, model.distances_, counts]
    ).astype(float)
linkage_matrix
'''
array([[0.        , 1.        , 2.        , 2.        ],
       [3.        , 5.        , 2.        , 2.        ],
       [2.        , 6.        , 3.46410162, 3.        ],
       [4.        , 7.        , 3.46410162, 3.        ],
       [8.        , 9.        , 5.19615242, 6.        ]])
'''
'''
第i轮合并哪两个簇,这两个簇之间的距离是多少,合并后的簇多大
'''

dendrogram(linkage_matrix)

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

UQI-LIUWJ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值