Permalink
Browse files

Add Cookbook for Mahalanobis

  • Loading branch information...
1 parent 2250549 commit 53bf1218ff10481366cb2107ff1efddabee11856 @abhinavrai44 abhinavrai44 committed Dec 30, 2016
@@ -0,0 +1,37 @@
+======================
+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.
+
+.. math::
+
+ \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
+
+.. sgexample:: mahalanobis.sg:create_features
+
+We create an instance of :sgclass:`CMahalanobisDistance` by passing it :sgclass:`CDenseFeatures`.
+
+.. sgexample:: mahalanobis.sg:create_instance
+
+The distance matrix can be extracted as follows:
+
+.. sgexample:: mahalanobis.sg:extract_distance
+
+We can use the same instance with new :sgclass:`CDenseFeatures` to compute asymmetrical distance as follows:
+
+.. sgexample:: mahalanobis.sg:refresh_distance
+
+----------
+References
+----------
+:wiki:`Mahalanobis_distance`
+
+.. bibliography:: ../../references.bib
+ :filter: docname in docnames
@@ -0,0 +1,20 @@
+CSVFile f_feats_a("../../data/fm_train_real.dat")
+CSVFile f_feats_b("../../data/fm_test_real.dat")
+
+#![create_features]
+RealFeatures features_a(f_feats_a)
+RealFeatures features_b(f_feats_b)
+#![create_features]
+
+#![create_instance]
+MahalanobisDistance distance(features_a, features_a)
+#![create_instance]
+
+#![extract_distance]
+RealMatrix distance_matrix_aa = distance.get_distance_matrix()
+#![extract_distance]
+
+#![refresh_distance]
+distance.init(features_a, features_b)
+RealMatrix distance_matrix_ab = distance.get_distance_matrix()
+#![refresh_distance]
@@ -1,24 +0,0 @@
-#!/usr/bin/env python
-traindat = '../data/fm_train_real.dat'
-testdat = '../data/fm_test_real.dat'
-
-parameter_list = [[traindat, testdat]]
-
-def distance_mahalanobis_modular (train_fname = traindat, test_fname = testdat):
-
- from modshogun import RealFeatures, CSVFile
- from modshogun import MahalanobisDistance
-
- feats_train=RealFeatures(CSVFile(train_fname))
- feats_test=RealFeatures(CSVFile(test_fname))
-
- distance = MahalanobisDistance(feats_train, feats_train)
- dm_train=distance.get_distance_matrix()
- distance.init(feats_train, feats_test)
- dm_test=distance.get_distance_matrix()
-
- return distance,dm_train,dm_test
-
-if __name__=='__main__':
- print('MahalanobisDistance')
- distance_mahalanobis_modular(*parameter_list[0])

0 comments on commit 53bf121

Please sign in to comment.