What is the difference between Euclidean and Manhattan distance, and which should KNN use?
Euclidean is the straight-line distance, sqrt of the sum of squared differences. Manhattan is the sum of absolute differences, like walking along city blocks. Euclidean is the default; Manhattan can be better in high dimensions or with many outliers.
Explain it simply
Between two points in Mumbai, Euclidean distance is how far a crow flies. Manhattan distance is how far an auto-rickshaw drives, going along the grid of roads, never cutting across buildings.
Example
Points P = (1, 2) and Q = (4, 6).
Differences: dx = 3, dy = 4
Euclidean = sqrt(3^2 + 4^2) = sqrt(25) = 5
Manhattan = |3| + |4| = 7Both are special cases of Minkowski distance with power p: p = 2 gives Euclidean, p = 1 gives Manhattan.
from sklearn.neighbors import KNeighborsClassifier
KNeighborsClassifier(n_neighbors=5, p=1) # Manhattan
KNeighborsClassifier(n_neighbors=5, p=2) # Euclidean (default)Squaring makes Euclidean distance more sensitive to one large difference in a single feature. Manhattan treats each feature's gap in proportion. For binary or text-like features, other measures (Hamming, cosine) are also common.
What to say in the interview
"Euclidean is the L2 straight-line distance and Manhattan is the L1 sum of absolute differences; both are Minkowski distances. I start with Euclidean on scaled features and treat the metric as a hyperparameter to compare by cross-validation."
Common mistakes
- Using any distance on unscaled features.
- Using Euclidean on categorical data coded as integers.