【问题标题】:Scipy dendrogram leaf node orderingScipy 树状图叶节点排序
【发布时间】:2018-11-25 12:16:32
【问题描述】:

我正在尝试更改 Scipy dendrogram 绘图函数中叶节点的顺序。考虑以下 sn-p:

from scipy.cluster.hierarchy import linkage, dendrogram
import matplotlib.pyplot as plt
dists = [ 2., 10.,  3.]
lx = linkage(dists, 'complete')
dendrogram(lx)
plt.show()

得到的树状图是:

我想在这个图中将叶节点的顺序更改为0, 1, 2。有没有简单的方法来实现这一点?

我尝试了dendrogram 中参数count_sortdistance_sort 的所有可能设置,但顺序保持不变。

【问题讨论】:

    标签: python scipy


    【解决方案1】:

    source code of the dendrogram scipy method中出现以下评论:

    # This feature was thought about but never implemented (still useful?):
    #
    #         ... = dendrogram(..., leaves_order=None)
    #
    #         Plots the leaves in the order specified by a vector of
    #         original observation indices. If the vector contains duplicates
    #         or results in a crossing, an exception will be thrown. Passing
    #         None orders leaf nodes based on the order they appear in the
    #         pre-order traversal.
    

    所以这是一个相关的问题。您可以联系 Scipy 开发人员 (SciPy Project Mailing Lists) 表达您对该增强功能的兴趣,以便他们了解它的有用性并给予更高的优先级。

    【讨论】:

      【解决方案2】:

      我认为这是不可能的。 count_sortdistance_sort 选项仅适用于后代节点。

      想象一下您的示例中标签 1 和 2 已切换。在这种情况下,节点不可能按顺序排列,因为节点 1 需要强制在配对节点 0 和 1 之间。这表明任何对所有节点进行排序的过程都是不可扩展的。

      【讨论】:

      • 我只想让它适用于这个特定的例子(我知道某些排序不起作用,但事实并非如此)
      • 我不认为“这表明任何对所有节点进行排序的过程都是不可扩展的”。使用组合学很容易证明非平凡树的叶子的线性排序没有单一的解决方案。排序标准将确定解决方案的精确形式。迭代或递归算法可以完成这项工作。
      猜你喜欢
      • 2013-01-25
      • 2015-07-30
      • 2012-09-16
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      • 2020-06-06
      • 1970-01-01
      • 2020-07-24
      相关资源
      最近更新 更多