Manhattan distance measures how far two points are by summing the absolute difference in each coordinate. Unlike Euclidean distance, it does not square coordinate differences, so large differences receive less emphasis. section 9.4.1 section 9.7.1
For mixed data, one-hot encoding makes categorical mismatches contribute to the distance. A mismatch across binary columns can outweigh a comparatively large numeric difference, creating an imbalance between categorical and numeric features. section 9.4.1
Use the metric with these comparisons in mind:
- Treat Minkowski distance with power 1 as Manhattan distance and power 2 as Euclidean distance. section 9.7.1
- Prefer Manhattan distance when large feature differences should not dominate as strongly as they do under Euclidean distance. section 9.7.1
- Inspect feature representation and scale before interpreting distances, especially after one-hot encoding categorical variables. section 9.4.1
Computing pairwise distances
Represent two rows as vectors and calculate their distance with scikit-learn's manhattan_distances function. In the demonstrated one-hot example, changing the categorical feature's cardinality does not change the resulting Manhattan distance. section 9.4.1
Table 9.15 One-hot encoding with one categorical and one numeric feature (view table figure)
| Row |
Red |
Blue |
Green |
White |
Yellow |
Weight |
|---|---|---|---|---|---|---|
| 1 |
1 |
0 |
0 |
0 |
0 |
0.1 |
| 2 |
0 |
1 |
0 |
0 |
0 |
0.1 |
| 3 |
1 |
0 |
0 |
0 |
0 |
0.3 |
| 4 |
1 |
0 |
0 |
0 |
0 |
0.9 |
Inspect the one-hot representation of a categorical feature alongside a numeric weight before evaluating distances. section 9.4.1



