【问题标题】:scipy.cluster.hierarchy.dendrogram(): exactly what does truncate_mode='level' do?scipy.cluster.hierarchy.dendrogram():truncate_mode='level' 到底是做什么的?
【发布时间】:2021-05-16 16:26:58
【问题描述】:

documentation 表示:“树状图树的显示级别不超过 p。“级别”包括从上次合并中合并 p 的所有节点。” (p 是另一个参数)我无法弄清楚“p 从上次合并中合并”是什么意思。谁能解释一下?

(我用truncate_mode='level'truncate_mode='lastp' 创建了相同数据的树状图;我没有发现比较有启发性。)

【问题讨论】:

    标签: python scipy hierarchical-clustering dendrogram


    【解决方案1】:

    以下代码演示了truncate_mode='level'truncate_mode='lastp'

    from scipy.cluster import hierarchy
    import matplotlib.pyplot as plt
    import numpy as np
    
    np.random.seed(1729)
    ytdist = np.random.randint(1, 1000, 36)
    
    Z = hierarchy.linkage(ytdist, 'single')
    fig, ax_rows = plt.subplots(ncols=6, nrows=2, sharey=True, figsize=(16, 5))
    
    for ax_row, truncate_mode in zip(ax_rows, ['level', 'lastp']):
        hierarchy.dendrogram(Z, ax=ax_row[0])
        ax_row[0].set_title('default, no truncation')
        for ind, ax in enumerate(ax_row[1:]):
            if truncate_mode == 'level':
                p = len(ax_row) - ind - 1
            else:
                p = len(ax_row) - ind
            hierarchy.dendrogram(Z, truncate_mode=truncate_mode, p=p, ax=ax)
            ax.set_title(f"truncate_mode='{truncate_mode}', p={p}")
    plt.tight_layout()
    plt.show()
    

    【讨论】:

    • 谢谢,这很有帮助! (如果子图的截断顺序相反,这样会更有帮助,因此最不严重的截断紧邻未截断的结果。)从示例转到我的问题的答案:“最后”合并提到在文档中是最后的合并,之后只有一个包含所有数据的集群。我把“last”读作“previous”。
    • 仅供参考,我创建了一个文档更改拉取请求,已合并到 scipy:master - github.com/scipy/scipy/commit/…
    猜你喜欢
    • 2015-08-06
    • 2013-09-02
    • 2014-01-02
    • 2013-10-10
    • 2017-05-08
    • 2022-01-20
    • 2012-10-17
    • 2017-06-15
    • 2011-05-20
    相关资源
    最近更新 更多