3

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

4

2 回答 2

8

根据文档,squareform是它自己的倒数:

http://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.squareform.html

于 2013-02-01T13:44:09.243 回答
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])
于 2013-02-01T13:42:52.900 回答