[SciPy-User] scipy.spatial.distance.mahalanobis & inverse covariance matrix
Skipper Seabold
jsseabold at gmail.com
Wed Aug 4 14:28:59 EDT 2010
On Wed, Jul 21, 2010 at 8:22 AM, bearce meanu <dfeuzs at googlemail.com> wrote:
> 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
>
Have a look at what concatenate actually does here. The problem is
that inv is getting a scalar array (which I think would be nice to
have linalg.inv and linalg.det handle...), though I think it could use
a more useful error message.
In [59]: np.linalg.inv(np.array(1.5))
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
/home/skipper/<ipython console> in <module>()
/usr/local/lib/python2.6/dist-packages/numpy/linalg/linalg.pyc in inv(a)
443 """
444 a, wrap = _makearray(a)
--> 445 return wrap(solve(a, identity(a.shape[0], dtype=a.dtype)))
446
447
IndexError: tuple index out of range
You probably want something like this. Note that you don't have to
stack x and y, but you could do
np.linalg.cov(np.column_stack((x,y)), rowvar=0)
or
np.linalg.cov(x,y, rowvar=0)
Note that the rowvar=0 tells the cov function that you have your
variables in columns, so you don't have to take the transpose.
hth,
Skipper
More information about the SciPy-User
mailing list