Euclidean Distance
In one sentence: Euclidean distance is the straight-line distance between two points: square each gap, add the squares, then take the square root.
Read it out loud: “d of p and q equals the square root of the sum, for i from 1 to n, of p sub i minus q sub i, all squared. Here p and q are two points, each a list of n numbers. The big sigma sign means add up, and i is a counter that goes through the features one at a time. Each p sub i is the i-th number of the point p, and each q sub i is the i-th number of the point q.”
Before you start
The ideas and signs this page uses, explained from zero. For powers of ten, Greek letters and other notation in general, see Math basics.
- Points as lists of numbers
A point here is a list of numbers, one for each feature. A house described by its size in square meters and its number of bedrooms is one point with two numbers, such as . A point with two numbers sits on a flat plane, and a point with three numbers sits in ordinary three-dimensional space. A list can be longer than three, even though no picture can show it.
- The sigma sign, , and add up
The capital Greek letter sigma, , means add up. It is said SIG-muh. The small label under it, , says where to start counting, and the label above it, , says where to stop. So means , the three values added together.
- Squares and square roots
A square is a number multiplied by itself: . A negative number squared is still positive, so . The square root, written , undoes squaring: , because . Most square roots of whole numbers are decimals that do not end, so they are rounded, such as .
- The Pythagorean theorem
The Pythagorean theorem says that in a right triangle, the square of the longest side, called the hypotenuse, equals the sum of the squares of the two shorter sides. For sides 3 and 4, the longest side is 5, because . Euclidean distance is this same rule applied to the gaps between two points.
- Scaling a feature
Scaling a feature means changing its units or its range, for example converting centimeters to meters, or rescaling values so they fall in a similar range. Features with very large numbers dominate the sum of squared gaps, so scaling is often needed before a distance is measured.
- Nearest neighbor
A nearest neighbor of a new point is the known point with the smallest distance to it. Finding nearest neighbors means measuring the distance from the new point to every known point, then choosing the smallest one. The distance used is often this Euclidean distance.
What each symbol means
| Symbol | Meaning |
|---|---|
| The distance between points and . It has the same unit as the features: if the features are in kilometers, the distance is in kilometers, and if they are in square meters, the distance is in meters. | |
| The first point, a list of numbers, one for each feature. For a house described as , the first number is the size in square meters. Each number has the unit of its own feature. | |
| The second point, with the same features in the same order. Its numbers must be in the same units as the matching numbers in , so that the gap between them has a meaning. | |
| The number of features, which is also the number of dimensions. It is a count with no unit: two features give a flat plane, three give ordinary three-dimensional space, and a house described by size and bedrooms has . | |
| The counter that picks one feature at a time, counting from 1 to . It has no unit. For a house described by (size, bedrooms), means the size and means the number of bedrooms. | |
| The value of point for feature number . It has the unit of that feature. For the house , the value is in square meters. | |
| The value of point for feature number . It has the same unit as , so that the subtraction means something. For , the value is also in square meters. | |
| Add up for every from 1 to , so the sum runs over all the features. It has no unit of its own. The units come from the terms being added, which here are squared gaps between the two points. | |
| The square root of the total of the squared gaps. It undoes the squaring, so the answer is in the original unit instead of square units. For example, a total of 25 square meters gives a distance of 5 meters. |
The formula piece by piece
Start with one feature. The gap between the two points in feature says how far apart they are along that one measurement. The gap can be positive or negative, depending on which point is larger, and the next step removes that sign.
Square the gap. Squaring makes every gap positive, so gaps in opposite directions do not cancel out, and big gaps count much more than small ones.
Add up the squared gaps for all the features. This total is the squared straight-line distance between the two points, with every feature counted once and none left out.
The distance between two points is the square root of the sum, over all their features, of the squared differences between their values.
What the formula is doing, and why
A point is a list of numbers. A house could be described by its size and its number of bedrooms, and each house is then one point. Euclidean distance tells you how far apart two points are in the straight-line sense you would measure with a ruler. It is the shortest possible distance between two points, the direct line rather than a route that bends.
Why squares and a root? With two features, the formula is the Pythagorean theorem. The gap along the first feature and the gap along the second are the two short sides of a right triangle, and the distance is the long side. The formula extends the same idea to any number of features, even though no ruler can draw that many directions.
Why square each gap? Squaring removes the sign, so a gap of and a gap of count the same. It also makes big gaps count much more than small ones: a gap of 6 adds 36 to the total, while a gap of 3 adds only 9. The squared gaps are added, and the square root at the end brings the answer back to the original unit, so the distance is in the same unit as the features.
Why scale the features? Every feature enters the sum, so the size of each featureβs numbers matters. A feature measured in dollars, with values in the thousands, will swamp a feature counting bedrooms, whose values are near 3. Rescale the features first so that no single one dominates. In k-nearest neighbors, the closest known points decide the label of a new point, so this scaling can change the answer.
When does it not fit? Euclidean distance treats equal gaps as equally important, which is reasonable for measurements such as size or weight. It is a poor fit for categories such as colors, where the number codes have no real distance between them. For those, a different encoding of the categories, or a different distance, is needed.
An everyday picture
Imagine a bird flying straight from one city to another. It ignores roads and turns. Its path is the shortest line between the two cities, and Euclidean distance measures exactly that line. Each extra feature is like one more direction the bird could fly, even if no one can picture it.
Worked example: Which bike station is closest?
You stand at a point 1 km east and 2 km north of the town center, so your position is km. Three bike stations are at km, km and km, the town center itself. The first number of each point is east of the center and the second is north. The positions are invented for the example, and every number is in kilometers.
- Check the units and the points. All the coordinates are in kilometers and use the same two features, east and north, so the gaps between them have a clear meaning. Each station is a point with two numbers, and we can subtract its coordinates from ours straight away.
- Distance to station 1. The gaps are km east and km north. Squared and added, they give , so the distance is km. The squares remove the minus signs, so the direction of each gap does not change the answer.
- Distance to station 2. The gaps are km and km. Squared and added, they give , so the distance is km. This is the straight-line distance, the distance a bird would fly, not the distance you would walk along the streets.
- Distance to station 3. The gaps are km and km. Squared and added, they give , so the distance is km. The town center is farther than station 2 but closer than station 1.
- Pick the nearest station. The three distances are 5 km, 1.414 km and 2.236 km. The smallest is 1.414 km, to station 2. The nearest neighbor rule says to choose the known point with the smallest distance, so station 2 is the nearest.
- Skip the roots when only ranking. The squared distances are 25, 2 and 5, and they rank the stations in exactly the same order as the true distances. The square root is still needed to report the actual distance in kilometers, but when you only need the winner, comparing the squares is a quick shortcut.
Answer: Station 2 is the nearest, about 1.41 km away in a straight line. The town center is about 2.24 km away, and station 1 is 5 km away. If you want the closest station, head toward station 2.
The numbers in this example are recomputed by script every time the site is built.
Common mistakes
- Leaving out the square root. The squared distance ranks neighbors in the same order, but it is not the distance, so do not report it as one.
- Mixing units. Subtracting a height in centimeters from a weight in kilograms gives a number with no meaning. Put every feature on a comparable footing first.
- Skipping feature scaling. A feature with large numbers dominates the distance, so the other features barely matter.
- Using category names as numbers. Coding red as 1 and blue as 2 creates distances that have no real meaning. Encode categories in a different way.
Where you will meet it
- k-nearest neighbors, which finds the closest known points to a new point and lets them vote on its label.
- k-means clustering, which assigns each point to the nearest center, using the squared form of this distance.
- Nearest-neighbor search in image retrieval, where the feature vectors of images are compared to find the most similar ones.
- Measuring the error between a predicted position and the true position in robotics and mapping.
Questions people ask
Why square the gaps and then take a square root?
Squaring turns every gap into a positive number, so gaps in opposite directions do not cancel out. The square root undoes the squaring, so the answer is in the same units as the original numbers.
Is this the same as the Pythagorean theorem?
Yes for two points in two dimensions, where the formula gives the long side of a right triangle. The formula extends the same idea to more features.
Why should I scale the features before using it?
Because a feature with large values dominates the sum. Rescaling, for example by subtracting the mean and dividing by the standard deviation, gives each feature a fair say in the distance.
How is it different from Manhattan distance?
Manhattan distance adds the absolute gaps instead. It does not square the gaps or take a square root, and it measures the distance you would walk along a grid of city blocks.
Related formulas
- Cosine Similarity: Cosine similarity is a score from -1 to 1 that says how closely two lists of numbers point the same way, ignoring how big each list is.
- k-Means Clustering: k-means sorts points into a chosen number of groups by repeatedly moving each groupβs center to the middle of its members and giving every point to its nearest center.
- Mean Squared Error (MSE): Mean squared error measures how wrong a modelβs guesses are by squaring each miss, so big misses count much more, and then averaging all of the squared misses.