[SciPy-User] scipy.spatial.distance.mahalanobis & inverse covariance matrix

bearce meanu dfeuzs at googlemail.com
Wed Jul 21 08:22:03 EDT 2010


Dear experts,

i just switched from matlab to scipy/numpy and i am sorry for this
very basic question.

my goal is to calculate the mahalanobis distance btw to vectors x & y.
Here is my code:

from scipy.spatial.distance import mahalanobis
import numpy as np
x=np.random.normal(size=25)
y=np.random.normal(size=25)
V = np.linalg.inv(np.cov(np.concatenate((x, y)).T)) # inverse covariance matrix

which gives the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/dist-packages/numpy/linalg/linalg.py", line
355, in inv
    return wrap(solve(a, identity(a.shape[0], dtype=a.dtype)))
IndexError: tuple index out of range

what is the appropriate way of calculating the inv cov matrix for this case.

I really appreciate your help,
BM



More information about the SciPy-User mailing list