# Mahalanobis Distance¶

The Mahalanobis distance for real valued features computes the distance between a feature vector and a distribution of features characterized by its mean and covariance.

$\sqrt{ ( x_{i} - \mu )^\top S^{-1} ( x_{i} - \mu )}$

## Example¶

Imagine we have files with data. We create CDenseFeatures (here 64 bit floats aka RealFeatures) as

features_a = RealFeatures(f_feats_a)
features_b = RealFeatures(f_feats_b)

features_a = RealFeatures(f_feats_a);
features_b = RealFeatures(f_feats_b);

RealFeatures features_a = new RealFeatures(f_feats_a);
RealFeatures features_b = new RealFeatures(f_feats_b);

features_a = Shogun::RealFeatures.new f_feats_a
features_b = Shogun::RealFeatures.new f_feats_b

features_a <- RealFeatures(f_feats_a)
features_b <- RealFeatures(f_feats_b)

features_a = shogun.RealFeatures(f_feats_a)
features_b = shogun.RealFeatures(f_feats_b)

RealFeatures features_a = new RealFeatures(f_feats_a);
RealFeatures features_b = new RealFeatures(f_feats_b);

auto features_a = some<CDenseFeatures<float64_t>>(f_feats_a);
auto features_b = some<CDenseFeatures<float64_t>>(f_feats_b);


We create an instance of CMahalanobisDistance by passing it CDenseFeatures.

distance = MahalanobisDistance(features_a, features_a)

distance = MahalanobisDistance(features_a, features_a);

MahalanobisDistance distance = new MahalanobisDistance(features_a, features_a);

distance = Shogun::MahalanobisDistance.new features_a, features_a

distance <- MahalanobisDistance(features_a, features_a)

distance = shogun.MahalanobisDistance(features_a, features_a)

MahalanobisDistance distance = new MahalanobisDistance(features_a, features_a);

auto distance = some<CMahalanobisDistance>(features_a, features_a);


The distance matrix can be extracted as follows:

distance_matrix_aa = distance.get_distance_matrix()

distance_matrix_aa = distance.get_distance_matrix();

DoubleMatrix distance_matrix_aa = distance.get_distance_matrix();

distance_matrix_aa = distance.get_distance_matrix

distance_matrix_aa <- distance$get_distance_matrix()  distance_matrix_aa = distance:get_distance_matrix()  double[,] distance_matrix_aa = distance.get_distance_matrix();  auto distance_matrix_aa = distance->get_distance_matrix();  We can use the same instance with new CDenseFeatures to compute asymmetrical distance as follows: distance.init(features_a, features_b) distance_matrix_ab = distance.get_distance_matrix()  distance.init(features_a, features_b); distance_matrix_ab = distance.get_distance_matrix();  distance.init(features_a, features_b); DoubleMatrix distance_matrix_ab = distance.get_distance_matrix();  distance.init features_a, features_b distance_matrix_ab = distance.get_distance_matrix  distance$init(features_a, features_b)
distance_matrix_ab <- distance\$get_distance_matrix()

distance:init(features_a, features_b)
distance_matrix_ab = distance:get_distance_matrix()

distance.init(features_a, features_b);
double[,] distance_matrix_ab = distance.get_distance_matrix();

distance->init(features_a, features_b);
auto distance_matrix_ab = distance->get_distance_matrix();


## References¶

Wikipedia: Mahalanobis_distance