Minkowski Distance FamilyOpen in Colab

Minkowski Distance Family

Every algorithm that relies on "nearness" (KNN, K-Means, hierarchical clustering, kernel methods) needs a formula for distance. The Minkowski family unifies the most common metrics under a single parameter pp, letting you interpolate between Manhattan and Euclidean and beyond.

The four standard metrics

MetricFormulaTypical use
Euclidean∑i(xi−yi)2\sqrt{\sum_i (x_i - y_i)^2}Default for continuous features
Manhattan∑i∣xi−yi∣\sum_i \lvert x_i - y_i\rvertHigh-dimensional, sparse, or grid-like data
Minkowski (pp)(∑i∣xi−yi∣p)1/p\left(\sum_i \lvert x_i - y_i\rvert^p\right)^{1/p}Generalizes both; choose pp via CV
Chebyshevmax⁡i∣xi−yi∣\max_i \lvert x_i - y_i\rvertWhen the largest gap dominates

The master Minkowski formula and its special cases

dp(x,y)=(∑i=1d∣xi−yi∣p)1/pd_p(\mathbf{x}, \mathbf{y}) = \left(\sum_{i=1}^{d} \lvert x_i - y_i\rvert^p\right)^{1/p}

Special cases:

  • p=1p = 1: Manhattan — sum the absolute gaps; like walking a city grid, no diagonals.
  • p=2p = 2: Euclidean — square the gaps, sum, square-root; the ruler distance.
  • p→∞p \to \infty: Chebyshev — only the single largest coordinate gap survives, since (∑aip)1/p→max⁡iai(\sum a_i^p)^{1/p} \to \max_i a_i.

All the metrics on a worked example

Take (1,2,3)(1, 2, 3) to (4,6,3)(4, 6, 3).

Per-coordinate absolute gaps:

∣x1−y1∣=∣1−4∣=3,∣x2−y2∣=∣2−6∣=4,∣x3−y3∣=∣3−3∣=0\lvert x_1 - y_1\rvert = \lvert 1 - 4\rvert = 3, \quad \lvert x_2 - y_2\rvert = \lvert 2 - 6\rvert = 4, \quad \lvert x_3 - y_3\rvert = \lvert 3 - 3\rvert = 0

Feed gaps (3,4,0)(3, 4, 0) into each formula:

Manhattan (p=1p = 1): add the gaps.

d1=3+4+0=7d_1 = 3 + 4 + 0 = 7

Euclidean (p=2p = 2): square, sum, square-root.

d2=32+42+02=9+16=25=5d_2 = \sqrt{3^2 + 4^2 + 0^2} = \sqrt{9 + 16} = \sqrt{25} = 5

Minkowski (p=3p = 3): cube the gaps, sum, take the cube root.

d3=(33+43+03)1/3=(27+64)1/3=911/3≈4.50d_3 = \left(3^3 + 4^3 + 0^3\right)^{1/3} = (27 + 64)^{1/3} = 91^{1/3} \approx 4.50

Chebyshev (p→∞p \to \infty): the maximum gap wins.

d∞=max⁡(3,4,0)=4d_\infty = \max(3, 4, 0) = 4

Ordering: d1=7≥d2=5≥d3≈4.50≥d∞=4d_1 = 7 \ge d_2 = 5 \ge d_3 \approx 4.50 \ge d_\infty = 4. Larger pp lets the single biggest gap dominate while smaller gaps fade, so the distance decreases toward max⁡(3,4,0)=4\max(3, 4, 0) = 4 as p→∞p \to \infty.

How the parameter p changes the geometry

The unit ball (the set of all points at distance 1 from the origin) changes shape with pp:

ppUnit ball shapeKNN neighborhood
1Diamond (square rotated 45°)Axis-aligned diamond; gaps in each coordinate count separately
2Circle / sphereIsotropic; most natural for continuous data without strong axes
∞\inftySquare / hypercubeSquare neighborhood; dominated by the single largest gap

As pp increases from 1 to ∞\infty, the ball transitions from diamond to circle to square. Features with the largest individual gap gain more influence relative to the rest.

Feature scaling is critical

All Minkowski metrics treat coordinate magnitudes as meaningful. A feature measured in millimeters (range 0–10 000) completely overwhelms one in meters (range 0–10). Always normalize or standardize before computing distances.

Computing and comparing every metric in Python

Python
import numpy as np

x = np.array([1, 2, 3], dtype=float)
y = np.array([4, 6, 3], dtype=float)
gaps = np.abs(x - y)  # [3, 4, 0]

d1   = gaps.sum()                          # Manhattan:   7.0
d2   = np.sqrt((gaps**2).sum())            # Euclidean:   5.0
d3   = (gaps**3).sum() ** (1/3)            # Minkowski p=3: ~4.497
dinf = gaps.max()                          # Chebyshev:   4.0

print(f"Manhattan  (p=1):   {d1:.4f}")
print(f"Euclidean  (p=2):   {d2:.4f}")
print(f"Minkowski  (p=3):   {d3:.4f}")
print(f"Chebyshev  (p→∞):   {dinf:.4f}")

# Verify limit: as p grows, Minkowski → Chebyshev
for p in [1, 2, 3, 5, 10, 50, 100]:
    dp = (gaps**p).sum() ** (1/p)
    print(f"  p={p:3d}: d_p = {dp:.6f}  (Chebyshev = {dinf:.6f})")

Categorical features: the Hamming distance

For features that are categories rather than numbers, Euclidean distance is meaningless. The Hamming distance counts the fraction of positions where the two vectors disagree:

dH(x,y)=1d∑i=1d1[xi≠yi]d_H(\mathbf{x}, \mathbf{y}) = \frac{1}{d}\sum_{i=1}^d \mathbf{1}[x_i \neq y_i]

A Hamming distance of 0 means the vectors are identical; a distance of 1 means they differ in every coordinate.

Related concepts

Concept neighbourhood

How this concept connects — click any node to jump there, or explore the whole concept map.

RelatedReferenced by
Minkowski Distance FamilyK-Means: Full AlgorithmHierarchical Clustering L…K-Nearest Neighbors