scipy.spatial.distance.squareform的反函数

时间:2013-02-01 10:01:59

标签: python scipy

是否存在scipy.spatial.distance.squareform的反函数?如果没有,那么用它来处理巨大距离矩阵的最佳方法是什么?

2 个答案:

答案 0 :(得分:8)

答案 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])