Python HCluster、距离矩阵和压缩距离矩阵

3

我正在使用hcluster模块从距离矩阵计算一个树形图。我的距离矩阵是通过以下方式生成的数组:

import hcluster
import numpy as np

mols = (..a list of molecules)
distMatrix = np.zeros((10, 10))
  for i in range(0,10):       
    for j in range(0,10):
      sim = OETanimoto(mols[i],mols[j]) # a function to calculate similarity between molecules
      distMatrix[i][j] = 1 - sim

我接着使用命令distVec = hcluster.squareform(distMatrix)将矩阵转换为压缩向量,并使用vecLink = hcluster.linkage(distVec)计算关联矩阵。所有这些都很好,但如果我使用距离矩阵而不是压缩向量来计算链接矩阵matLink = hcluster.linkage(distMatrix),我会得到一个不同的链接矩阵(节点之间的距离更大,拓扑结构略有不同)。现在我不确定这是因为hcluster只适用于压缩向量,还是我在操作过程中出了错。谢谢您的帮助!
1个回答

2
我模仿了你的示例并遇到了同样的问题。在文档字符串中确实写着:

对压缩距离矩阵 y 执行分层/凝聚聚类。y 必须是一个大小为 :math:{n \choose 2} 的向量,其中 n 是距离矩阵中配对的原始观测值的数量。

然而,快速查看代码后,似乎意图是让它能够同时处理向量形状和矩阵形状的代码: 在 hierarchy.py 中,基于矩阵形状进行了开关选择。 但是,关键信息似乎在函数 linkage 的文档字符串中:

   - Q : ndarray
       A condensed or redundant distance matrix. A condensed
       distance matrix is a flat array containing the upper
       triangular of the distance matrix. This is the form that
       ``pdist`` returns. Alternatively, a collection of
       :math:`m` observation vectors in n dimensions may be passed as
       a :math:`m` by :math:`n` array.

因此,我认为该接口不允许传递距离矩阵。 相反,它认为您正在传递m个n维观测向量。 因此,结果会有所不同?

这听起来合理吗?

否则,请查看代码本身,我相信您能够调试并找出为什么您的示例不同。

干杯 Matt


嗨马特,非常感谢你的回复。听到传递向量是正确的方法让我感到放心。 - Mo Sander

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接