【问题标题】:Label Ordering in Scipy DendrogramScipy Dendrogram 中的标签排序
【发布时间】:2018-06-28 03:07:27
【问题描述】:

在 python 中,我有一个 N × N 距离矩阵 dmat,其中 dmat[i,j] 编码从实体 i 到实体 j 的距离。我想查看树状图。我做到了:

from scipy.cluster.hierarchy import dendrogram, linkage
import matplotlib.pylab as plt

labels=[name of entity 1,2,3,...]

Z=linkage(dmat)
dn=dendrogram(Z,labels=labels)
plt.show()

但是标签排序看起来不对。有些实体与 dmat 非常接近,但这并未反映在树状图中。怎么回事?

【问题讨论】:

    标签: python scipy hierarchical-clustering


    【解决方案1】:

    linkage 的第一个参数必须是 condensed 格式的距离,或者是被聚类的点数组。如果传递平方 (N x N) 距离矩阵,linkage 会将其解释为 N 维空间中的 N 个点。

    您可以使用scipy.spatial.distance.squareform 将方阵转换为精简形式。

    将此添加到文件的开头

    from scipy.spatial.distance import squareform
    

    替换这个

    Z=linkage(dmat)
    

    Z = linkage(squareform(dmat))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-09
      • 2018-12-12
      • 1970-01-01
      • 1970-01-01
      • 2017-10-26
      • 2018-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多