【问题标题】:Agglomerative Clustering Hierarchy Visualization凝聚聚类层次可视化
【发布时间】:2021-10-04 14:46:10
【问题描述】:

如何将凝聚聚类形成的层次结构可视化为树状图。我有一个大小为 (400,400) 的预先计算的距离矩阵。

clusterer= AgglomerativeClustering(n_clusters=32,affinity="precomputed",linkage="average").fit(distance_matrix)

如何将形成 32 个聚类的结果清晰地可视化为树状图?我尝试将这些集群可视化,但由于它们是 32 个,因此颜色无法清楚地区分它们。

colors_clusters = clusterer.labels_
fig = plt.figure(figsize=(10,7))
plt.xlabel('median_score', family='Arial', fontsize=9)
plt.ylabel('count_intersections', family='Arial', fontsize=9)
plt.title('Heliopolis', family='Arial', fontsize=12)
plt.scatter(clusters_df['median_score'], clusters_df['count_intersections'], c=colors_clusters, edgecolors='black', s=50)
plt.show()

【问题讨论】:

    标签: python scikit-learn scipy hierarchical-clustering


    【解决方案1】:

    一种著名的层次聚类可视化方法是使用dendrogram。您可以在sklearn library 中找到绘图示例。您也可以在scipy library 中找到示例。

    您可以在此处从以前的链接中找到一个示例:

    import numpy as np
    
    from matplotlib import pyplot as plt
    from scipy.cluster.hierarchy import dendrogram
    from sklearn.datasets import load_iris
    from sklearn.cluster import AgglomerativeClustering
    
    
    def plot_dendrogram(model, **kwargs):
        # Create linkage matrix and then plot the dendrogram
    
        # create the counts of samples under each node
        counts = np.zeros(model.children_.shape[0])
        n_samples = len(model.labels_)
        for i, merge in enumerate(model.children_):
            current_count = 0
            for child_idx in merge:
                if child_idx < n_samples:
                    current_count += 1  # leaf node
                else:
                    current_count += counts[child_idx - n_samples]
            counts[i] = current_count
    
        linkage_matrix = np.column_stack([model.children_, model.distances_,
                                          counts]).astype(float)
    
        # Plot the corresponding dendrogram
        dendrogram(linkage_matrix, **kwargs)
    
    
    iris = load_iris()
    X = iris.data
    
    # setting distance_threshold=0 ensures we compute the full tree.
    model = AgglomerativeClustering(distance_threshold=0, n_clusters=None)
    
    model = model.fit(X)
    plt.title('Hierarchical Clustering Dendrogram')
    # plot the top three levels of the dendrogram
    plot_dendrogram(model, truncate_mode='level', p=3)
    plt.xlabel("Number of points in node (or index of point if no parenthesis).")
    plt.show()
    
    

    【讨论】:

    • 但是如何使用预先计算的矩阵绘制树状图?我在 scipy.cluster.hierarchy.linkage 文档中读到该指标不能“预先计算”
    • 当我尝试将 n_clusters 属性设置为 32,而不是 none 时,我得到一个错误 'AgglomerativeClustering' object has no attribute 'distances_'
    • 我什至尝试添加“compute_distances=True”,但得到了另一个错误“TypeError: __init__() got an unexpected keyword argument 'compute_distances'。我不明白我错过了什么。
    • 原来我必须更新到更新的版本。非常感谢
    猜你喜欢
    • 2014-06-28
    • 2016-09-06
    • 2021-07-25
    • 2021-07-21
    • 2020-11-30
    • 2015-11-20
    • 2017-10-24
    • 2015-12-01
    • 1970-01-01
    相关资源
    最近更新 更多