One dimensional Mahalanobis Distance in Python

2019-03-21 09:31发布

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

2条回答
疯言疯语
2楼-- · 2019-03-21 09:41

Covariance needs 2 arrays to compare. In both np.cov() and Opencv CalcCovarMatrix, it expects the two arrays to be stacked on top of each other (Use vstack). You can also have the 2 arrays to be side by side if you change the Rowvar to false in numpy or use COVAR_COL in opencv. If your arrays are multidimentional, just flatten() them first.

So if I want to compare two 24x24 images, I flatten them both into 2 1x1024 images, then stack the two to get a 2x1024, and that is the first argument of np.cov().

You should then get a large square matrix, where it shows the results of comparing each element in array1 with each element in array2. In my example it will be 1024x1024. THAT is what you pass into your invert function.

查看更多
等我变得足够好
3楼-- · 2019-03-21 10:00

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
查看更多
登录 后发表回答