Overview

Distance metrics and similarity measures are fundamental concepts in machine learning, used extensively in:

Distance Metrics

Euclidean Distance

The most common distance metric, measuring the straight-line distance between two points in Euclidean space.

Formula (2D):

General Formula (n-dimensional):

When to use:

  • When the magnitude of differences matters
  • For continuous numerical data
  • Default choice for many ML algorithms
  • Works well when features are on similar scales

Properties:

  • Sensitive to scale (requires normalization)
  • Affected by outliers
  • Assumes isotropic (circular/spherical) distributions

Manhattan Distance

Also known as Taxicab distance or L1 distance. Measures the sum of absolute differences between coordinates.

Formula:

When to use:

  • When movement is restricted to grid-like paths
  • For high-dimensional data
  • Less sensitive to outliers than Euclidean
  • When you want a more robust metric

Example:
Think of navigating city blocks - you can’t go diagonally through buildings, you must follow the streets.

Reference: What is Manhattan Distance? A Deep Dive | DataCamp

Minkowski Distance

A generalization of both Euclidean and Manhattan distances.

Formula:

Where:

  • p = 1: Manhattan distance
  • p = 2: Euclidean distance
  • p → ∞: Chebyshev distance (maximum coordinate difference)

When to use:

  • When you want flexibility in choosing distance behavior
  • Useful for experimenting with different distance metrics
  • Can be tuned as a hyperparameter

Similarity Measures

See Distance Metrics And Similarity Measures - Similarity Measures.

Choosing the Right Metric

MetricBest ForProsCons
EuclideanContinuous data, low dimensionsIntuitive, commonly usedSensitive to scale and outliers
ManhattanGrid-like data, high dimensionsRobust to outliersLess intuitive
MinkowskiExperimental tuningFlexible, generalizableRequires parameter tuning
CosineText, sparse high-dim dataMagnitude-independentIgnores vector length
JaccardSets, binary featuresSimple for categorical dataOnly for set-like data

Practical Applications

K-Means Clustering

Typically uses Euclidean Distance, but can use others like Manhattan Distance depending on the data and task.

DBSCAN

Uses distance metrics (typically Euclidean) with the parameter to define neighborhoods.

Hierarchical Clustering

Calculates distances (Euclidean Distance) and uses different linkage methods (Single, Complete, Average).

K-Nearest Neighbors (KNN)

Relies heavily on distance metrics to find the k closest neighbors.

Implementation Tips

  1. Normalize/Standardize features when using Euclidean or Minkowski distance
  2. Consider sparsity - Cosine similarity works well for sparse data
  3. Think about semantics - Does magnitude matter in your application?
  4. Experiment - Try multiple metrics and compare results
  5. Computational cost - Manhattan is faster to compute than Euclidean

References