在scipy树形图中获取具有相同颜色的数据标签

时间:2014-11-07 05:55:53

标签: python scipy hierarchical-clustering dendrogram

我使用scipy进行了层次聚类,以下是我的树形图。现在我需要获得具有特定颜色的叶子标签。例如:我需要找到红色,蓝色,绿色等标签。有什么帮助?

B = dendrogram(linkage_matrix,
               color_threshold=250,
               labels=df.session.tolist(),
               distance_sort='ascending')

enter image description here

2 个答案:

答案 0 :(得分:2)

我只需要解决完全相同的问题,但上面的答案是不正确的。 color_list中的颜色与 leaves 的颜色不对应,而是与链接的颜色相对应 - 连接簇的pi形结构。 Color_list和ivl甚至不需要相同的长度。

在这里,您可以找到获取树叶标签的方法: http://nxn.se/post/90198924975/extract-cluster-elements-by-color-in-python

答案 1 :(得分:-2)

B或树形图函数返回的任何内容都是带有以下键的字典:

print B.keys()
['ivl', 'dcoord', 'leaves', 'color_list', 'icoord']

' color_list'会看起来像这样:

print B['color_list']
['g', 'g', 'b', 'r', 'r', 'r', 'r', 'b']

并且' ivl'会是这样的:

print B['ivl']
['4', '1', '5', '6', '3', '2', '7', '0']

只需要进行列表理解:

myInd = [i for i, c in zip(B['ivl'], B['color_list']) if c=='g']
print myInd
['4', '1']