scipy.spatial.distance.squareform的反函数

3

scipy.spatial.distance.squareform有反函数吗?如果没有,那么处理大型距离矩阵的最佳方法是什么?

2个回答

8

2

实际上,这个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])

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接