[PYTHON] How to find Mahalanobis distance

Mahalanobis distance

d=(x-\mu)^T\Sigma^{-1}(x-\mu)

$ x $ is the vector for which you want to find the distance to the data group. $ \ Mu $ is the average value of the data group. $ \ Sigma ^ {-1} $ is the inverse of the covariance matrix of the data group. Using the Cholesky factorization, the equation can be transformed as follows.

\begin{eqnarray}
d &=& (x-\mu)^T\Sigma^{-1}(x-\mu) \\
  &=& (x-\mu)^T(LL^T)^{-1}(x-\mu) \\
  &=& (L^{-1}(x-\mu))^T(L^{-1}(x-\mu)) \\
  &=& z^Tz
\end{eqnarray}

$ L $ is the lower triangular matrix obtained by the Cholesky decomposition. If you set $ z = (L ^ {-1} (x- \ mu)) $ and find this, all you have to do is calculate the inner product.

Implementation

Implement the above in python.

import numpy as np
from scipy.linalg import solve_triangular

def mahalanobis(x, mu, sigma):
    L = np.linalg.cholesky(sigma)
    d = x - mu
    z = solve_triangular(
        L, d.T, lower=True, check_finite=False,
        overwrite_b=True)
    squared_maha = np.sum(z * z, axis=0)
    return squared_maha

$ L $ can be found in numpy's linalg.cholesky. $ z $ can be found in scipy's linalg.solve_triangular.

reference

Recommended Posts

How to find Mahalanobis distance
How to find large files on Linux
How to find the area of the Voronoi diagram
How to find the correlation for categorical variables
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
Scraping 2 How to scrape
How to use Seaboan
How to use image-match
How to use shogun
How to install Python
How to use Pandas 2
How to read PyPI
How to install pip
How to use Virtualenv
How to use numpy.vectorize
How to update easy_install
How to install archlinux
How to use pytest_report_header
How to restart gunicorn
How to install python
How to virtual host
How to debug selenium
How to use partial
How to use Bio.Phylo
How to read JSON
How to use x-means
How to use WikiExtractor.py
How to update Spyder
How to use IPython
How to install BayesOpt
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to grow dotfiles
How to use list []
How to use python-kabusapi
"How to count Fukashigi"
How to install Nbextensions
How to use OptParse
How to use return
How to install Prover9
How to use dotenv
How to operate NumPy
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
[2020.8 latest] How to install Python
How to find the optimal number of clusters in k-means
How to estimate kernel density
How to use Qt Designer
[IPython] How to Share IPython Notebook
How to install Python [Windows]
How to use search sorted
[gensim] How to use Doc2Vec