liveBook · Manning

Manhattan distance

As explained in Outlier Detection in Python

Manhattan distance is the sum of the absolute coordinate-wise differences between two points.

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

How Outlier Detection in Python covers it

How deep it goes
Defines the metric, compares its weighting with Euclidean distance, and demonstrates implementation; it stops short of a full detector.
Examples
Uses one-hot encoded color and weight rows, pairwise distance tables, and Python calls to manhattan_distances.
What it assumes
Assumes vectorized features and teaches how one-hot encoding represents categorical values for distance calculation.
What you can do after
Apply it via scikit-learn to calculate distances between numeric or one-hot encoded records.
How it connects
Connects Manhattan distance to one-hot encoding, Euclidean distance, Minkowski distance, Canberra distance, and outlier detection.

Also explained in

Compare how 4 books explain Manhattan distance →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Outlier Detection in Python. The section links point to the parts of the book it draws on.