我正在使用模块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只适用于压缩向量,还是我在这个过程中犯了错误。
谢谢你的帮忙!
发布于 2011-04-19 21:00:37
我做了一个和你的类似的快速随机例子,也遇到了同样的问题。在文档字符串中,它确实写着:
在压缩距离矩阵y上执行分层/凝聚聚类。Y必须是a :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.所以我认为这个接口不允许传递距离矩阵。相反,它认为您正在向它传递n维的m观测向量。那么结果有什么不同呢?
这看起来合理吗?
否则,只要看一下代码本身,我相信您将能够调试它,并找出为什么您的示例是不同的。
干杯马特
https://stackoverflow.com/questions/5714595
复制相似问题