def test_dist(): X = rand(100,5) Y = rand(80,5) d1 = num.distsq(X,Y) d2 = ((X[:,None,...] - Y[None,...])**2.0).sum(axis=-1) d3 = cdist(X,Y, metric='euclidean')**2.0 print "d1 - d2" aaae(d1,d2) print "d1 - d3" aaae(d1,d3)
def test_dist(): X = rand(100, 5) Y = rand(80, 5) d1 = num.distsq(X, Y) d2 = ((X[:, None, ...] - Y[None, ...])**2.0).sum(axis=-1) d3 = cdist(X, Y, metric='euclidean')**2.0 print("d1 - d2") aaae(d1, d2) print("d1 - d3") aaae(d1, d3)
def get_dist_mat(self, X=None, C=None): """Matrix of distance values r_ij = ||x_i - c_j|| with:: x_i : X[i,:] c_i : C[i,:] Parameters ---------- X : None or array (M,N) with N-dim points Training data or interpolation points. If None then self.Rsq is returned. C : 2d array (K,N), optional If None then self.C is used. Returns ------- Rsq : (M,K), where K = M usually for training """ # pure numpy: # dist = X[:,None,...] - C[None,...] # Rsq = (dist**2.0).sum(axis=-1) # where # X: (M,N) # C: (K,N) # dist: (M,K,N) "matrix" of distance vectors (only for numpy case) # Rsq: (M,K) matrix of squared distance values # Creates *big* temorary arrays if X is big (~1e4 points). # # training: # If X == C, we could also use pdist(X), which would give us a 1d # array of all distances. But we need the redundant square matrix # form for get_rbf_mat() anyway, so there is no real point in # special-casing that. These two are the same: # >>> R = spatial.squareform(spatial.distances.pdist(X)) # >>> R = spatial.distances.cdist(X,X) # >>> Rsq = R**2 if X is not None: self.msg("get_dist_mat...") C = self.C if C is None else C if self.distmethod == 'spatial': Rsq = distance.cdist(X, C)**2.0 elif self.distmethod == 'fortran': Rsq = num.distsq(X,C) elif self.distmethod == 'numpy': dist = X[:,None,...] - C[None,...] Rsq = (dist**2.0).sum(axis=-1) else: raise StandardError("unknown value for distmethod: %s" %self.distmethod) return Rsq else: assert self.Rsq is not None, ("self.Rsq is None") return self.Rsq