def sq_dist(a, b=None):
#mean-center for numerical stability
D, n = a.shape[0], a.shape[1]
if (b is None):
mu = a.mean(axis=1)
a -= mu[:, np.newaxis]
b = a
m = n
aSq = np.sum(a**2, axis=0)
bSq = aSq
else:
d, m = b.shape[0], b.shape[1]
if (d != D): raise Exception('column lengths must agree')
mu = (float(m)/float(m+n))*b.mean(axis=1) + (float(n)/float(m+n))*a.mean(axis=1)
a -= mu[:, np.newaxis]
b -= mu[:, np.newaxis]
aSq = np.sum(a**2, axis=0)
bSq = np.sum(b**2, axis=0)
C = np.tile(np.column_stack(aSq).T, (1, m)) + np.tile(bSq, (n, 1)) - 2*a.T.dot(b)
C = np.maximum(C, 0) #remove numerical noise
return C
#evaluate 'power sums' of the individual terms in Z
评论列表
文章目录