scipy.spatial.distance.squareform有反函数吗?如果不是,那么处理巨大距离矩阵的最佳方式是什么?
发布于 2013-02-01 13:44:09
根据文档,squareform
是它自己的反面:
http://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.squareform.html
发布于 2013-02-01 13:42:53
实际上,这个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])
https://stackoverflow.com/questions/14643756
复制相似问题