(数月后)
scipy.spatial.distance.cdist( X, Y )给出了X和Y的所有距离对,2个暗角,3个暗角……
它也有22个不同的范数,在此处详细介绍
。
# cdist example: (nx,dim) (ny,dim) -> (nx,ny)from __future__ import divisionimport sysimport numpy as npfrom scipy.spatial.distance import cdist#...............................................................................dim = 10nx = 1000ny = 100metric = "euclidean"seed = 1 # change these params in sh or ipython: run this.py dim=3 ...for arg in sys.argv[1:]: exec( arg )np.random.seed(seed)np.set_printoptions( 2, threshold=100, edgeitems=10, suppress=True )title = "%s dim %d nx %d ny %d metric %s" % ( __file__, dim, nx, ny, metric )print "n", title#...............................................................................X = np.random.uniform( 0, 1, size=(nx,dim) )Y = np.random.uniform( 0, 1, size=(ny,dim) )dist = cdist( X, Y, metric=metric ) # -> (nx, ny) distances#...............................................................................print "scipy.spatial.distance.cdist: X %s Y %s -> %s" % ( X.shape, Y.shape, dist.shape )print "dist average %.3g +- %.2g" % (dist.mean(), dist.std())print "check: dist[0,3] %.3g == cdist( [X[0]], [Y[3]] ) %.3g" % ( dist[0,3], cdist( [X[0]], [Y[3]] ))# (trivia: how do pairwise distances between uniform-random points in the unit cube# depend on the metric ? With the right scaling, not much at all:# L1 / dim ~ .33 +- .2/sqrt dim# L2 / sqrt dim ~ .4 +- .2/sqrt dim# Lmax / 2 ~ .4 +- .2/sqrt dim



