Distance Metrics In Machine Learning
Distance Metrics In Machine Learning
3 def mahalanobis (
• Grid-like patterns (e.g., city blocks) 4 x : ndarray ,
5 y : ndarray ,
• When diagonal movement costs more 6 cov : ndarray = None
7 ) -> float :
• Robust to outliers 8 """ Calculate Mahalanobis distance using
sklearn . """
9 X = x . reshape (1 , -1) if x . ndim == 1 else x When to use:
10 Y = y . reshape (1 , -1) if y . ndim == 1 else y
11 • Binary or set-based data
12 if cov is None :
13 # Estimate covariance from data • Comparing discrete features
14 cov_estimator = EmpiricalCovariance ()
15 cov_estimator . fit ( np . vstack ([ X , Y ]) ) • Document similarity with word sets
16 cov = cov_estimator . covariance_
17