Skip to content

Distance measures

Formulas for the distance measures used in data science: Manhattan, Euclidean, Minkowski, Chebyshev, cosine, Hamming and Jaccard.

2 min readUpdated

Many machine learning methods, such as k-nearest neighbours, k-means clustering and similarity search over embeddings, decide how alike two data points are by computing a distance between them. The choice of distance changes the result, so it should fit the data: real-valued coordinates, high-dimensional vectors, binary strings or sets.

The overview below shows thirteen common measures. This note gives the formulas for the ones used most often. In all of them, x=(x1,x2,…,xn)x=(x_1,x_2,\dots,x_n) and y=(y1,y2,…,yn)y=(y_1,y_2,\dots,y_n) are vectors in Rn\mathbb{R}^n.

Thirteen distance measures illustrated: Euclidean, cosine, Hamming, Mahalanobis, Manhattan, Minkowski, Chebyshev, Wasserstein, Jaccard, Haversine, Sørensen-Dice, Hausdorff and Levenshtein

Manhattan distance

The sum of the absolute differences of the coordinates: the distance travelled along a grid, like a taxi in a city with rectangular blocks. It is less sensitive to a single large difference than the Euclidean distance.

DManhattan(x,y)=∑i=1n∣xi−yi∣D_{Manhattan}(x,y) = \sum_{i=1}^{n} |x_i - y_i|

Euclidean distance

The length of the straight line between the two points. It is the default for low-dimensional, real-valued data, and works best when all features are on the same scale, so normalise them first.

DEuclidean(x,y)=∑i=1n(xi−yi)2D_{Euclidean}(x,y) = \sqrt{\sum_{i=1}^{n} (x_i - y_i)^2}

Minkowski distance

A generalisation of the Manhattan and Euclidean distances. It adds a parameter pp, called the order, that selects the measure:

DMinkowski(x,y)=(∑i=1n∣xi−yi∣p)1/pD_{Minkowski}(x,y) = \Big( \sum_{i=1}^{n} | x_i - y_i | ^p \Big) ^{1/p}

pp is a real number with p≥1p \ge 1; below 1 the result is no longer a metric. Special cases:

  • p=1⇒p=1 \Rightarrow Manhattan distance
  • p=2⇒p=2 \Rightarrow Euclidean distance
  • p→∞⇒p\to\infty \Rightarrow Chebyshev distance

Chebyshev distance

The largest difference along any single coordinate, the limit of the Minkowski distance as pp grows. It counts the moves of a king on a chessboard.

DChebyshev(x,y)=max⁡i∣xi−yi∣D_{Chebyshev}(x,y) = \max_{i} |x_i - y_i|

Cosine distance

Compares the direction of two vectors and ignores their length. It suits high-dimensional data such as text embeddings or word counts, where the length mostly reflects document size. Cosine similarity is the cosine of the angle between the vectors, and the distance is one minus the similarity:

Dcosine(x,y)=1−∑i=1nxiyi∑i=1nxi2 ∑i=1nyi2D_{cosine}(x,y) = 1 - \frac{\sum_{i=1}^{n} x_i y_i}{\sqrt{\sum_{i=1}^{n} x_i^2}\,\sqrt{\sum_{i=1}^{n} y_i^2}}

Hamming distance

The number of positions at which two strings or vectors of equal length differ. It is used for binary codes, categorical features and error detection.

DHamming(x,y)=∑i=1n[xi≠yi]D_{Hamming}(x,y) = \sum_{i=1}^{n} [x_i \ne y_i]

[xi≠yi][x_i \ne y_i] is 1 when the values differ and 0 when they are equal.

Jaccard distance

Compares two sets AA and BB: the size of the intersection divided by the size of the union is the Jaccard index, and the distance is one minus the index. It suits sets of tags, words or items bought.

DJaccard(A,B)=1−∣A∩B∣∣A∪B∣D_{Jaccard}(A,B) = 1 - \frac{|A \cap B|}{|A \cup B|}

References

Related notes

4 min readTürkçe

Ollama

GPU'suz, yalnız CPU ile çalışan makinelerde Ollama ile yerel LLM çalıştırma ve hızı belirleyen ayarlar.

  • AI & Data
  • DevOps & Containers
4 min read

Hadoop

Install Hadoop on macOS with Homebrew, configure a single-node HDFS and YARN setup, enable SSH to localhost and run basic HDFS commands.

  • AI & Data
5 min read

Algorithms for machine learning

How supervised learning fits a model: hypothesis function, cost function, gradient descent, feature scaling, overfitting and cross-validation.

  • AI & Data

Type to search the whole site.