【问题标题】:Get labels of data with same color in scipy dendrogram在 scipy dendrogram 中获取具有相同颜色的数据标签
【发布时间】:2014-11-07 05:55:52
【问题描述】:

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

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

【问题讨论】:

  • 你能添加最少添加一些导入语句吗?

标签: python scipy hierarchical-clustering dendrogram


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    B,或者由 dendrogram 函数返回的任何内容,是具有以下键的字典:

    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']
    

    【讨论】:

      猜你喜欢
      • 2018-06-28
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 2021-07-10
      • 2013-01-25
      • 1970-01-01
      相关资源
      最近更新 更多