【问题标题】:python hcluster, distance matrix and condensed distance matrixpython hcluster,距离矩阵和压缩距离矩阵
【发布时间】:2011-08-08 13:07:22
【问题描述】:

我正在使用模块 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 仅适用于压缩向量,还是我在此过程中犯了错误。

感谢您的帮助!

【问题讨论】:

    标签: python distance hcluster


    【解决方案1】:

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

    在 压缩距离矩阵 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.
    

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

    这看起来合理吗?

    否则,只需查看代码本身,我相信您将能够对其进行调试并找出您的示例不同的原因。

    干杯 马特

    【讨论】:

    • 嗨,马特,非常感谢您的回复。听到传递向量是要走的路,这令人欣慰。
    猜你喜欢
    • 2016-08-15
    • 1970-01-01
    • 2020-07-04
    • 2016-12-06
    • 2019-09-04
    • 2012-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多