Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One dimensional Mahalanobis Distance in Python

I've been trying to validate my code to calculate Mahalanobis distance written in Python (and double check to compare the result in OpenCV) My data points are of 1 dimension each (5 rows x 1 column).

In OpenCV (C++), I was successful in calculating the Mahalanobis distance when the dimension of a data point was with above dimensions.

The following code was unsuccessful in calculating Mahalanobis distance when dimension of the matrix was 5 rows x 1 column. But it works when the number of columns in the matrix are more than 1:

import numpy;
import scipy.spatial.distance;
s = numpy.array([[20],[123],[113],[103],[123]]);
covar = numpy.cov(s, rowvar=0);
invcovar = numpy.linalg.inv(covar)
print scipy.spatial.distance.mahalanobis(s[0],s[1],invcovar);

I get the following error:

Traceback (most recent call last):
  File "/home/abc/Desktop/Return.py", line 6, in <module>
    invcovar = numpy.linalg.inv(covar)
  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
like image 457
garak Avatar asked Oct 08 '22 06:10

garak


1 Answers

One-dimensional Mahalanobis distance is really easy to calculate manually:

import numpy as np
s = np.array([[20], [123], [113], [103], [123]])
std = s.std()
print np.abs(s[0] - s[1]) / std

(reducing the formula to the one-dimensional case).

But the problem with scipy.spatial.distance is that for some reason np.cov returns a scalar, i.e. a zero-dimensional array, when given a set of 1d variables. You want to pass in a 2d array:

>>> covar = np.cov(s, rowvar=0)

>>> covar.shape
()

>>> invcovar = np.linalg.inv(covar.reshape((1,1)))

>>> invcovar.shape
(1, 1)

>>> mahalanobis(s[0], s[1], invcovar)
2.3674720531046645
like image 156
Danica Avatar answered Oct 12 '22 22:10

Danica