scipy.spatial.distance.squareform的反函数
scipy.spatial.distance.squareform这个函数有没有反向的功能?如果没有,处理超大距离矩阵的最佳方法是什么?
2 个回答
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])
8
根据文档,squareform
是它自己的反函数:
http://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.squareform.html