Notes

Manhattan Distance

Imagine navigating a city laid out in a perfect grid: you can’t cut diagonally through buildings, so you move block by block. Manhattan Distance captures that same idea for data points—how far apart two feature vectors are when you add up the “straight-line” steps along each feature.

What it is (and the math behind it)

Manhattan Distance (also called L1 distance) between two vectors x and y is the sum of absolute differences across features:

d(x, y) = Σ |x_i - y_i|

So if you have two customers described by features like “number of purchases,” “days since last login,” and “support tickets,” Manhattan distance adds up how different they are on each of those dimensions, without squaring anything or taking square roots.

Why it matters in supervised learning (especially k-NN)

In k-Nearest Neighbors (k-NN), the distance metric decides who counts as a “neighbor,” which directly changes predictions. Manhattan distance tends to be more robust than Euclidean distance when:

  • You have outliers: large deviations don’t get amplified by squaring.
  • Your data is high-dimensional: L1 can preserve meaningful differences when distances start to look similar.
  • You expect effects to be more “additive per feature” than “geometric.”

Practical examples and how you’ll see it in code

  • Spam detection: comparing email feature counts (word frequencies) where a few extreme words shouldn’t dominate similarity.
  • Credit risk: finding similar borrowers based on multiple numeric attributes with occasional unusual values.
  • Churn prediction: matching customers by behavior counts (logins, purchases, complaints).

In scikit-learn, you’ll encounter it via k-NN’s metric="manhattan" (or equivalently p=1 for Minkowski distance).

Manhattan Distance (L1 distance) measures dissimilarity between two feature vectors as the sum of absolute per-feature differences: d(x,y)=∑|xᵢ−yᵢ|. It corresponds to moving along axis-aligned steps in feature space rather than straight-line distance. It matters in supervised learning because algorithms like k-NN use it to define “nearest” examples, directly shaping neighborhoods, decision boundaries, and sensitivity to outliers compared with Euclidean distance.

Imagine you’re in a city laid out like a grid, like Manhattan. To get from one corner to another, you can’t cut diagonally through buildings—you have to walk along streets and avenues. Manhattan Distance measures “how far apart” two things are using that same idea: add up how much you’d move in each direction, step by step.

In supervised learning, this is one way to decide which past examples are most similar to a new case. For instance, a spam filter or a fraud detector might compare a new message or transaction to known ones, and Manhattan Distance helps pick the closest matches.