[SciPy-Dev] Unexpected behavior in cdist

Jacob VanderPlas vanderplas@astro.washington....
Mon Jan 2 11:02:30 CST 2012


Hello,
I found an unexpected result in scipy.spatial.cdist when specifying 
mahalanobis distance by string and by callable.  In the first case, the 
result seems to be
   (x - y)^T V^-1 (x - y)
while in the second case it is
   (x - y)^T V (x - y)
I'll submit a pull request with a fix, but I wanted to check here first 
to see if there is a reason for this behavior.  Thanks
   Jake


In [1]: from scipy.spatial.distance import cdist, mahalanobis

In [2]: import numpy as np

In [3]: np.random.seed(0)

In [4]: x = np.random.random((3, 5))  # 3 points in 5 dimensions

In [5]: y = np.random.random((2, 5))  # 2 points in 5 dimensions

In [6]: V = np.random.random((5, 5))

In [7]: V = np.dot(V, V.T)  # create a symmetric 5x5 covariance matrix

In [8]: cdist(x, y, "mahalanobis", V=V)
Out[8]:
array([[ 1.65110737,  3.53903147],
       [ 3.45891658,  2.84888938],
       [ 3.48806579,  2.75361187]])

In [9]: cdist(x, y, mahalanobis, V=V)
Out[9]:
array([[ 0.6078299 ,  0.51651104],
       [ 1.07543511,  0.44469686],
       [ 0.79203867,  0.27108674]])



More information about the SciPy-Dev mailing list