def mahalanobis_distsq(X, mu, C):
#print 'X', X.shape
#print 'mu', mu.shape
#print 'C', C.shape
assert(C.shape == (2,2))
det = C[0,0]*C[1,1] - C[0,1]*C[1,0]
Cinv = np.array([[ C[1,1]/det, -C[0,1]/det],
[-C[1,0]/det, C[0,0]/det]])
#print 'Cinv', Cinv.shape
#print 'Cinv * C:', np.dot(Cinv, C)
#print 'C * Cinv:', np.dot(C, Cinv)
d = X - mu
#print 'd', d.shape
Cinvd = np.dot(Cinv, d.T)
#print 'Cinvd', Cinvd.shape
M = np.sum(d * Cinvd.T, axis=1)
#print 'M', M.shape
return M
评论列表
文章目录