python-hcluster,距离矩阵和凝聚距离矩阵

2024-06-26 01:34:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我用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)计算连接矩阵。在

所有这些都很好,但是如果我使用距离矩阵而不是压缩向量来计算链接矩阵,我会得到一个不同的链接矩阵(节点之间的距离要大得多,拓扑结构略有不同)

现在我不确定这是因为hcluster只适用于压缩向量,还是我在去那里的路上犯了错误。在

谢谢你的帮助!在


Tags: inimport距离for链接nprange矩阵
1条回答
网友
1楼 · 发布于 2024-06-26 01:34:12

我想出了一个与你相似的随机例子,也遇到了同样的问题。 在docstring中它确实说:

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

然而,在快速浏览代码之后,它的目的似乎是让它同时处理向量和矩阵形状的代码: 在层次结构.py有一个基于矩阵形状的开关。 不过,关键信息似乎在函数链接的docstring中:

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

所以我认为接口不允许传递距离矩阵。 相反,它认为你传递的是n维的m观测向量。 结果有什么不同?在

这看起来合理吗?在

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

干杯 马特

相关问题 更多 >