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 , and its default is . With 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 and point B is at . We want the straight-line distance between them.
- Write the points as coordinates. means 1 unit right and 1 unit up from the origin. means 4 units right and 5 units up. Check: each point has one number per axis, so there are two numbers for each.
- 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 . Check: 1 + 3 = 4 and 1 + 4 = 5, so adding the difference to A lands on B.
- Square each entry of the difference. and . Check: 3 times 3 is 9, and 4 times 4 is 16.
- Add the squares. . Check: 9 + 16 = 25.
- 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 .
- distance from (0, 0) to (x, 3)
- height 3
The math
Definition
For two vectors and in , the Euclidean distance is
where is the Euclidean norm and means "add up the terms for up to ". The symbol means lists of real numbers.
The subscript 2 in is why this distance is also called the distance.
Steps 1 and 2 in symbols: the difference vector
Subtracting the coordinates gives . In the example, and , so is . The sign does not matter for a length, so we may use instead.
Steps 3 to 5 in symbols: square, sum, root
Each entry of the difference is squared, giving . Adding over all entries gives the sum of squares . In the example this is . Taking the square root gives .
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.
- Zero only for equal points. , and exactly when . A sum of squares is never negative, and it is zero only when every difference is zero.
- Symmetry. . Squaring removes the sign of each difference, so .
- Triangle inequality. . Write . The vector norm obeys the triangle inequality, so the length of the sum is at most the sum of the lengths. Going from to through 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, exactly when . So nearest-neighbour search often ranks points by the sum of squares alone.
Worked examples
Find the distance between the points 2 and 7 on a number line.
- Write the points as one-entry vectors: and .
- Subtract: .
- Square: .
- Take the square root: .
Interpretation: with one coordinate, the Euclidean distance is just the size of the gap, . 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.
Find the distance between and .
- Subtract entry by entry: .
- Square each entry: , , .
- Add the squares: .
- Take the square root: .
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 contributes the most, since it has the largest square.
A shop describes each customer by three numbers: age in years, yearly spending in thousands of dollars, and visits per month. Customer P is and customer Q is . Find how far apart they are.
- Subtract entry by entry: .
- Square each entry: , and .
- Add the squares: .
- 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 . The feature that contributes the largest share is spending, with of the , which is about 58 percent. Age contributes and visits contribute only .
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
. Subtract the corresponding entries, square each difference, add the squares over all 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 . The squares are and , their sum is , and the square root is . 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.