首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >凝聚聚类层次可视化

凝聚聚类层次可视化
EN

Stack Overflow用户
提问于 2021-07-29 05:23:36
回答 1查看 167关注 0票数 2

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

代码语言:javascript
复制
clusterer= AgglomerativeClustering(n_clusters=32,affinity="precomputed",linkage="average").fit(distance_matrix)

我如何清晰地将形成这32个集群的结果可视化为树状图?我试着可视化集群,但由于它们是32,颜色不能清楚地区分它们。

代码语言:javascript
复制
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()

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-29 05:36:34

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

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

代码语言:javascript
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68567522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档