reox
reox

Reputation: 5217

Working Example for Mahalanobis Distance Measure

I need to measure the distance between two n-diensional vectors. It seems that Mahalanobis Distance is a good choise here so i want to give it a try.

My Code looks like this:

import numpy as np
import scipy.spatial.distance.mahalanobis

x = [19, 8, 0, 0, 2, 1, 0, 0, 18, 0, 1673, 9, 218]
y = [17, 6, 0, 0, 1, 2, 0, 0, 8, 0, 984, 9, 30]
scipy.spatial.distance.mahalanobis(x,y,np.linalg.inv(np.cov(x,y)))

But I get this error message:

/usr/lib/python2.7/dist-packages/scipy/spatial/distance.pyc in mahalanobis(u, v, VI)
    498     v = np.asarray(v, order='c')
    499     VI = np.asarray(VI, order='c')
--> 500     return np.sqrt(np.dot(np.dot((u-v),VI),(u-v).T).sum())
    501 
    502 def chebyshev(u, v):

ValueError: matrices are not aligned

The Scipy Doc says, that VI is the inverse of the covariance matrix, and i think np.cov is the covariance matrix and np.linalg.inv is the inverse of a matrix...

But I see what is the problem here (matrices are not aligned): The Matrix VI has the wrong dimension (2x2 and not 13x13). So possible solution is to do this:

VI = np.linalg.inv(np.cov(np.vstack((x,y)).T))

but unfortuanly the det of np.cov(np.vstack((x,y)).T) is 0, which means that a inverse matrix does not exsists.

so how can i use mahalanobis distance measure when i even cant compute the covariance matrix?

Upvotes: 3

Views: 9461

Answers (2)

YXD
YXD

Reputation: 32511

You don't have a sample set with which to calculate a covariance. You probably just want the Euclidean distance here (np.linalg.norm(x-y)). What is the bigger picture in what you are trying to achieve?

Upvotes: 1

Ber
Ber

Reputation: 41813

Are you sure that Mahalanobis Distance is right for you application? According to Wikipedia you need a set of points to generate the covariance matrix, not just two vectors. Then you can compute distances of vectors from the set's center.

Upvotes: 3

Related Questions