Search

Search papers and lessons

Euclidean Distance

Level 1 · 11 min read

Euclidean distance is the straight-line distance between two points: subtract the coordinates, square each difference, add them up, and take the square root.

On this page

  • Colour = topic mix
  • Linear algebra
  • Discrete math
  • ReadReady to readNot yet

TL;DR

The Euclidean distance between two points is the length of the straight line joining them. You get it by subtracting the two sets of coordinates, squaring each difference, adding the squares, and taking the square root. The result is a single non-negative number that says how far apart the points are.

It is the everyday meaning of "how far", and it is the default way computers measure how close two data points are. Its main job is to be the distance that a vector norm gives to the difference of two vectors.

Builds on: Vector Norm · Leads to: metric spaces, K-Means Clustering, and nearest-neighbour search

Where it's used

Matching faces and photos. Face-recognition systems turn each face image into a list of numbers, called an embedding. The FaceNet system (2015) uses a 128-number embedding per face. To check whether two photos show the same person, the system computes the Euclidean distance between their embeddings. A small distance means the faces are probably the same person, and a large distance means they probably are not.

Grouping customers. The k-means algorithm sorts data points into groups. Each point joins the group whose centre is nearest to it, and "nearest" is measured with Euclidean distance. A shop that describes each customer by age, spending and visits per month uses this distance to find groups of similar shoppers.

Default settings in software. The nearest-neighbour estimators in the scikit-learn library, which predict a label from the closest training examples, measure closeness with the Minkowski distance. That is a family of distances controlled by a number pp, and its default is p=2p = 2. With p=2p = 2 the Minkowski distance is exactly the Euclidean distance, so a programmer who changes nothing is already using it.

Robots and games. A robot arm or a game character asks how far it is from a target point, or from an obstacle. The answer is the straight-line distance between two positions in space, which is a Euclidean distance in two or three dimensions.

How it works

Take two points on a flat map. Point A is at (1,1)(1, 1) and point B is at (4,5)(4, 5). We want the straight-line distance between them.

  1. Write the points as coordinates. A=(1,1)A = (1, 1) means 1 unit right and 1 unit up from the origin. B=(4,5)B = (4, 5) means 4 units right and 5 units up. Check: each point has one number per axis, so there are two numbers for each.
  2. Subtract the coordinates, entry by entry. Going from A to B moves 4 minus 1 = 3 units right and 5 minus 1 = 4 units up. The difference is the vector (3,4)(3, 4). Check: 1 + 3 = 4 and 1 + 4 = 5, so adding the difference to A lands on B.
  3. Square each entry of the difference. 32=93^2 = 9 and 42=164^2 = 16. Check: 3 times 3 is 9, and 4 times 4 is 16.
  4. Add the squares. 9+16=259 + 16 = 25. Check: 9 + 16 = 25.
  5. Take the square root. The number whose square is 25 is 5, so the distance is 5. Check: 5 times 5 is 25.

The steps are the Pythagorean theorem in disguise. The horizontal move 3 and the vertical move 4 are the two legs of a right triangle, and the straight line from A to B is its hypotenuse.

The plot shows the same idea for a point that slides sideways. Fix one point at the origin and move the other point along the horizontal line at height 3. Its distance from the origin is x2+9\sqrt{x^2 + 9}.

−4−20240123456horizontal position xdistance
  • distance from (0, 0) to (x, 3)
  • height 3
The distance is never below the height 3. At x = 0 it is 3, and at x = 4 it is 5, the 3-4-5 triangle again.

The math

Definition

Definition: Euclidean distance

For two vectors x=(x1,…,xd)\mathbf{x} = (x_1, \dots, x_d) and y=(y1,…,yd)\mathbf{y} = (y_1, \dots, y_d) in Rd\mathbb{R}^d, the Euclidean distance is

d(x,y)=∥x−y∥2=∑i=1d(xi−yi)2,d(\mathbf{x}, \mathbf{y}) = \|\mathbf{x} - \mathbf{y}\|_2 = \sqrt{\sum_{i=1}^{d} (x_i - y_i)^2},

where ∥⋅∥2\|\cdot\|_2 is the Euclidean norm and ∑i=1d\sum_{i=1}^{d} means "add up the terms for i=1i = 1 up to i=di = d". The symbol Rd\mathbb{R}^d means lists of dd real numbers.

The subscript 2 in ∥⋅∥2\|\cdot\|_2 is why this distance is also called the ℓ2\ell_2 distance.

Steps 1 and 2 in symbols: the difference vector

Subtracting the coordinates gives x−y=(x1−y1,…,xd−yd)\mathbf{x} - \mathbf{y} = (x_1 - y_1, \dots, x_d - y_d). In the example, x=(1,1)\mathbf{x} = (1, 1) and y=(4,5)\mathbf{y} = (4, 5), so x−y\mathbf{x} - \mathbf{y} is (1−4,1−5)=(−3,−4)(1 - 4, 1 - 5) = (-3, -4). The sign does not matter for a length, so we may use (3,4)(3, 4) instead.

Steps 3 to 5 in symbols: square, sum, root

Each entry ii of the difference is squared, giving (xi−yi)2(x_i - y_i)^2. Adding over all dd entries gives the sum of squares ∑i=1d(xi−yi)2\sum_{i=1}^{d} (x_i - y_i)^2. In the example this is 32+42=253^2 + 4^2 = 25. Taking the square root gives 25=5\sqrt{25} = 5.

Why it is a distance: three properties

A distance function, also called a metric, must satisfy three properties. The Euclidean distance satisfies all of them.

  1. Zero only for equal points. d(x,y)≥0d(\mathbf{x}, \mathbf{y}) \ge 0, and d(x,y)=0d(\mathbf{x}, \mathbf{y}) = 0 exactly when x=y\mathbf{x} = \mathbf{y}. A sum of squares is never negative, and it is zero only when every difference is zero.
  2. Symmetry. d(x,y)=d(y,x)d(\mathbf{x}, \mathbf{y}) = d(\mathbf{y}, \mathbf{x}). Squaring removes the sign of each difference, so (xi−yi)2=(yi−xi)2(x_i - y_i)^2 = (y_i - x_i)^2.
  3. Triangle inequality. d(x,z)≤d(x,y)+d(y,z)d(\mathbf{x}, \mathbf{z}) \le d(\mathbf{x}, \mathbf{y}) + d(\mathbf{y}, \mathbf{z}). Write x−z=(x−y)+(y−z)\mathbf{x} - \mathbf{z} = (\mathbf{x} - \mathbf{y}) + (\mathbf{y} - \mathbf{z}). The vector norm obeys the triangle inequality, so the length of the sum is at most the sum of the lengths. Going from x\mathbf{x} to z\mathbf{z} through y\mathbf{y} is never shorter than going directly.

Any function with these three properties is a metric, and the Euclidean distance is the most familiar one.

Squared distance

Taking the square root is slow for a computer when it is done millions of times, and it is not needed to compare distances. Since the square root is increasing, d(x,y)<d(x,z)d(\mathbf{x}, \mathbf{y}) < d(\mathbf{x}, \mathbf{z}) exactly when d(x,y)2<d(x,z)2d(\mathbf{x}, \mathbf{y})^2 < d(\mathbf{x}, \mathbf{z})^2. So nearest-neighbour search often ranks points by the sum of squares alone.

Worked examples

Example
1. One dimension: the distance on a number line

Find the distance between the points 2 and 7 on a number line.

  1. Write the points as one-entry vectors: x=(2)x = (2) and y=(7)y = (7).
  2. Subtract: x−y=(2−7)=(−5)x - y = (2 - 7) = (-5).
  3. Square: (−5)2=25(-5)^2 = 25.
  4. Take the square root: 25=5\sqrt{25} = 5.

Interpretation: with one coordinate, the Euclidean distance is just the size of the gap, ∣7−2∣=5|7 - 2| = 5. The square and the square root undo each other for a positive number, so the formula reduces to the plain gap between the two numbers.

Example
2. The standard case: three dimensions with an irrational answer

Find the distance between x=(2,−1,3)\mathbf{x} = (2, -1, 3) and y=(0,3,1)\mathbf{y} = (0, 3, 1).

  1. Subtract entry by entry: x−y=(2−0,−1−3,3−1)=(2,−4,2)\mathbf{x} - \mathbf{y} = (2 - 0, -1 - 3, 3 - 1) = (2, -4, 2).
  2. Square each entry: 22=42^2 = 4, (−4)2=16(-4)^2 = 16, 22=42^2 = 4.
  3. Add the squares: 4+16+4=244 + 16 + 4 = 24.
  4. Take the square root: 24=26≈4.90\sqrt{24} = 2\sqrt{6} \approx 4.90.

Interpretation: the two points are about 4.90 units apart. The answer is not a whole number, which is normal once the points are not on a simple right triangle. Note that the entry −4-4 contributes the most, since it has the largest square.

Example
3. A realistic case: two customers and one dominant feature

A shop describes each customer by three numbers: age in years, yearly spending in thousands of dollars, and visits per month. Customer P is (25,40,3)(25, 40, 3) and customer Q is (35,52,1)(35, 52, 1). Find how far apart they are.

  1. Subtract entry by entry: (25−35,40−52,3−1)=(−10,−12,2)(25 - 35, 40 - 52, 3 - 1) = (-10, -12, 2).
  2. Square each entry: 100100, 144144 and 44.
  3. Add the squares: 100+144+4=248100 + 144 + 4 = 248.
  4. Take the square root of 248 and round it to one decimal place. Then say which feature contributes the largest share of the 248.

Try it: The answer to step 4 is 248≈15.7\sqrt{248} \approx 15.7. The feature that contributes the largest share is spending, with 144144 of the 248248, which is about 58 percent. Age contributes 100100 and visits contribute only 44.

Interpretation: spending dominates this distance only because its numbers are large. A difference of 12 thousand dollars counts far more than a difference of 2 visits, even though both may matter equally to the shop. In practice the features are rescaled to a common range before the distance is computed.

Common mistakes and tradeoffs

It is tempting to think that the squared distance is a distance; in fact it is not, because it breaks the triangle inequality, and because it is not measured in the same units. It is only a convenient quantity for ranking. The square root is needed when you want an actual length, such as a threshold of 5 meters.

It is tempting to think that Euclidean distance treats all features equally; in fact it does so only when they share the same units and scale. Adding age in years to spending in thousands lets whichever feature has the biggest numbers decide the answer. Rescale the features first, for example so that each has mean 0 and spread 1.

It is tempting to think that the nearest point is always the most similar one in a high-dimensional space; in fact in very many dimensions the distances from one point to all the others become almost equal, which is the curse of dimensionality. This is one reason that sometimes Cosine Similarity is used instead.

When to use it: continuous measurements in comparable units, where "how far apart" is the question, and where clusters are roughly round.

When not to use it: when the distance should count movement along a grid, such as streets in a city. There the Manhattan distance, the sum of the absolute differences, is the right measure. When only the direction of a vector matters and its length does not, cosine similarity is better. Categories such as colours should not be coded as the numbers 1, 2, 3, because the code then implies an order that does not exist.

History

For about two thousand years, geometry measured distance with the Pythagorean theorem, which Euclid proved in Book I of the Elements around 300 BCE. Distances were lengths of lines in drawings, and nobody wrote them as coordinates.

In 1637 René Descartes published his Geometry, an appendix to his Discourse on Method. It showed how to describe points by pairs of numbers and turn geometry into algebra. Once points had coordinates, the distance formula became the Pythagorean theorem applied to the coordinate differences.

The idea of a distance as a general mathematical object came later. In 1906 Maurice Fréchet introduced the abstract notion of a metric space, which takes the three properties above as its definition. Euclidean distance became the standard example.

It caught on in data science in the 1960s, when the nearest-neighbour rule was studied as a method of pattern classification. Cover and Hart analysed it in 1967, and the method has been a baseline for classification ever since.

Recap

  • Euclidean distance subtracts the coordinates, squares each difference, adds the squares and takes the square root.
  • It is the norm of the difference of two vectors.
  • It satisfies the three metric properties, and the triangle inequality follows from the triangle inequality for norms.
  • Squared distance gives the same ranking and is cheaper to compute.
  • Features must share a scale, or the largest-number feature decides the answer.

Read next: K-Means Clustering uses this distance to assign points to centres. Hierarchical Clustering builds groups from it, step by step. Curse of Dimensionality explains what changes when there are many features.

Check yourself

QuestionWhat is the formula for the Euclidean distance between two vectors x and y in d dimensions?Show answer

∑i=1d(xi−yi)2\sqrt{\sum_{i=1}^{d} (x_i - y_i)^2}. Subtract the corresponding entries, square each difference, add the squares over all dd entries, and take the square root of the sum.

QuestionFind the Euclidean distance between the points (1, -2) and (4, 2).Show answer

The difference is (4−1,2−(−2))=(3,4)(4 - 1, 2 - (-2)) = (3, 4). The squares are 99 and 1616, their sum is 2525, and the square root is 55. The distance is 5.

QuestionWhy can nearest-neighbour search rank points by squared Euclidean distance instead of the distance itself?Show answer

The square root is an increasing function of a non-negative number. So if one squared distance is smaller than another, the corresponding distance is also smaller, and the nearest point is the same either way. Skipping the square root only saves computation; it changes the numerical values but not the ranking.

QuestionA dataset has a feature measured in metres, with values near 2, and a feature measured in millimetres, with values near 2000. Why can the Euclidean distance mislead, and what should you do first?Show answer

The millimetre feature's differences are about 1000 times larger, so its squares dominate the sum and the metre feature barely affects the distance. The distance then mostly measures the millimetre feature. Rescale the features to a common unit or a common spread before computing the distance.

Sources