显示scipy树状图的簇标签

问题描述 投票:6回答:1

我正在使用层次聚类来聚类单词向量,我希望用户能够显示显示聚类的树形图。但是,由于可能有数千个单词,我希望将此树形图截断为一些合理的有价值,每个叶子的标签是该群集中最重要单词的字符串。

我的问题是,according to the docs,“标签[i]值是仅当它对应于原始观察而不是非单一群集时才放在第i个叶子节点下的文本。”我认为这意味着我不能标记簇,只能标记奇点?

为了说明,这是一个简短的python脚本,它生成一个简单的标记树形图:

import numpy as np
from scipy.cluster.hierarchy import dendrogram, linkage
from matplotlib import pyplot as plt

randomMatrix = np.random.uniform(-10,10,size=(20,3))
linked = linkage(randomMatrix, 'ward')

labelList = ["foo" for i in range(0, 20)]

plt.figure(figsize=(15, 12))
dendrogram(
            linked,
            orientation='right',
            labels=labelList,
            distance_sort='descending',
            show_leaf_counts=False
          )
plt.show()

a dendrogram of randomly generated points

现在假设我要截断到只有5个叶子,并且对于每个叶子,将它标记为“foo,foo,foo ...”,即构成该群集的单词。 (注意:生成这些标签不是问题。)我将其截断,并提供一个匹配的标签列表:

labelList = ["foo, foo, foo..." for i in range(0, 5)]
dendrogram(
            linked,
            orientation='right',
            p=5,
            truncate_mode='lastp',
            labels=labelList,
            distance_sort='descending',
            show_leaf_counts=False
          )

这是问题,没有标签:

enter image description here

我想这里可能有一个参数'leaf_label_func',但我不知道如何使用它。

python matplotlib scipy dendrogram
1个回答
2
投票

您对使用leaf_label_func参数是正确的。

除了创建绘图之外,树形图函数还返回包含多个列表的字典(它们在文档中称为R)。您创建的leaf_label_func必须从R [“leaves”]中获取值并返回所需的标签。设置标签的最简单方法是运行树形图两次。一旦使用no_plot=True获取用于创建标签贴图的字典。然后再次创建情节。

randomMatrix = np.random.uniform(-10,10,size=(20,3))
linked = linkage(randomMatrix, 'ward')

labels = ["A", "B", "C", "D"]
p = len(labels)

plt.figure(figsize=(8,4))
plt.title('Hierarchical Clustering Dendrogram (truncated)', fontsize=20)
plt.xlabel('Look at my fancy labels!', fontsize=16)
plt.ylabel('distance', fontsize=16)

# call dendrogram to get the returned dictionary 
# (plotting parameters can be ignored at this point)
R = dendrogram(
                linked,
                truncate_mode='lastp',  # show only the last p merged clusters
                p=p,  # show only the last p merged clusters
                no_plot=True,
                )

print("values passed to leaf_label_func\nleaves : ", R["leaves"])

# create a label dictionary
temp = {R["leaves"][ii]: labels[ii] for ii in range(len(R["leaves"]))}
def llf(xx):
    return "{} - custom label!".format(temp[xx])

## This version gives you your label AND the count
# temp = {R["leaves"][ii]:(labels[ii], R["ivl"][ii]) for ii in range(len(R["leaves"]))}
# def llf(xx):
#     return "{} - {}".format(*temp[xx])


dendrogram(
            linked,
            truncate_mode='lastp',  # show only the last p merged clusters
            p=p,  # show only the last p merged clusters
            leaf_label_func=llf,
            leaf_rotation=60.,
            leaf_font_size=12.,
            show_contracted=True,  # to get a distribution impression in truncated branches
            )
plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.