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 仅适用于压缩向量,或者我是否在途中犯了错误。

谢谢你的帮助!

4

1 回答 1

2

我敲了一个与您类似的快速随机示例,并遇到了同样的问题。在文档字符串中它确实说:

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

然而,快速浏览一下代码,似乎它的意图是让它同时使用矢量形状和矩阵形状的代码:在 hierachy.py 中有一个基于矩阵形状的开关。然而,信息的关键位似乎在函数链接的文档字符串中:

   - 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.

所以我认为接口不允许传递距离矩阵。相反,它认为您正在传递mn 维的观察向量。因此结果不同?

这看起来合理吗?

否则只需看一下代码本身,我相信您将能够调试它并弄清楚为什么您的示例不同。

干杯马特

于 2011-04-19T13:00:36.580 回答