是否存在scipy.spatial.distance.squareform的反函数?如果没有,那么用它来处理巨大距离矩阵的最佳方法是什么?
答案 0 :(得分:8)
根据文档,squareform
是它自己的反转:
http://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.squareform.html
答案 1 :(得分:1)
实际上,这个lambda非常有效:
In [1]: unsquareform = lambda a: a[numpy.nonzero(numpy.triu(a))]
例如:
In [2]: scipy.spatial.distance.pdist(numpy.arange(12).reshape((4,3)))
Out[2]:
array([ 5.19615242, 10.39230485, 15.58845727, 5.19615242,
10.39230485, 5.19615242])
和
In [3]: unsquareform(scipy.spatial.distance.squareform(scipy.spatial.distance.pdist(numpy.arange(12).reshape((4,3)))))
Out[3]:
array([ 5.19615242, 10.39230485, 15.58845727, 5.19615242,
10.39230485, 5.19615242])