One dimensional Mahalanobis Distance in Python

2024/9/16 22:47:55

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 invreturn wrap(solve(a, identity(a.shape[0], dtype=a.dtype)))
IndexError: tuple index out of range
Answer

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
https://en.xdnf.cn/q/72234.html

Related Q&A

DeprecationWarning: please use dns.resolver.Resolver.resolve()

I am using resolver() as an alternative to socket() as I found that when multiple connections are made to different IPs it ends up stopping working. Anyway it returns a warning to me that I should use …

python cannot find module when using ssh

Im using python on servers. When I run a python command which needs numpy module, if I do ssh <server name> <python command>that server will complain no module named numpy found.However, if…

Python sklearn installation windows

When trying to install Pythons sklearn package on Windows 10 using pip I am given an EnvironmentError that tells me there is no such file or directory of a specific file: ERROR: Could not install packa…

Python PSD layers?

I need to write a Python program for loading a PSD photoshop image, which has multiple layers and spit out png files (one for each layer). Can you do that in Python? Ive tried PIL, but there doesnt se…

Multiple inheritance: overridden methods containing super()-calls

With the file super5.py:class A:def m(self):print("m of A called")class B(A):def m(self):print("m of B called")super().m()class C(A):def m(self):print("m of C called")supe…

Specifying the output file name in Apache Spark

I have a MapReduce job that Im trying to migrate to PySpark. Is there any way of defining the name of the output file, rather than getting part-xxxxx?In MR, I was using the org.apache.hadoop.mapred.li…

How to plot two DataFrame on same graph for comparison

I have two DataFrames (trail1 and trail2) with the following columns: Genre, City, and Number Sold. Now I want to create a bar graph of both data sets for a side by side comparison of Genre vs. total N…

Babel doesnt recognize jinja2 extraction method for language support

Im adding language translation support to my project. The code is on Python and has jinja2 in the html files, and Javascript.Im trying to use Babel to do the translation, but it doesnt recognize the ex…

Automatically simplifying/refactoring Python code (e.g. for loops - list comprehension)? [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…

Knowing the number of iterations needed for convergence in SVR scikit-learn

I am trying to optimize an SVR model and facing a problem because of overfitting, to overcome this I have tried to decrease the number of iterations instead of leaving it until convergence.To compare …