AI Glossary / Mathematics and optimization / Beginner
Euclidean distance
Definition
Euclidean distance is the straight-line distance between two points in a Euclidean space. For real coordinate vectors x and y of the same length, it is the square root of the sum of squared coordinate differences: d(x, y) = √Σ(xj − yj)². In machine learning, it is often used to compare numeric feature vectors, provided their representation and scales make that comparison meaningful.
What it means
In two dimensions, the formula is the Pythagorean theorem applied to the horizontal and vertical differences. The same construction extends to three dimensions and to feature spaces with many coordinates. Each coordinate contributes through its squared difference. The result describes closeness in the chosen coordinate system. It does not automatically describe travel time, semantic similarity or business relevance. Those interpretations require a suitable representation and a reason why straight-line geometry matches the question.
How it works
Subtract corresponding coordinates, square each difference, add the squares and take the square root. Equivalently, calculate the Euclidean norm of x − y. The distance is nonnegative, symmetric, zero exactly when the vectors agree and satisfies the triangle inequality. Translating both points by the same vector or applying the same orthogonal transformation preserves the distance. Rescaling a single coordinate generally changes it. A weighted version changes how much coordinates contribute and must be specified separately rather than assumed to be the ordinary unweighted formula.
Three practical examples
- A nearest-neighbor classifier can compare a new feature vector with training examples after a documented scaling procedure.
- A geometric design tool can calculate the direct distance between two points in a flat coordinate system.
- A clustering analysis can use Euclidean geometry to group observations, while checking whether the numeric features and their units support that notion of similarity.
Think of it this way
Think of stretching a taut string directly between two pins on a flat board. Its length represents the shortest straight connection. This picture helps for spatial coordinates, but a feature vector may combine quantities such as age and spending that are not physical directions. The string analogy does not choose sensible feature scales, account for roads or obstacles, or prove that nearby records should receive the same prediction.
Worked example
Let A = (2, 1), B = (5, 5) and C = (2, 7), with both coordinates measured in meters. The differences from A to B are 3 and 4, so d(A, B) = √(9 + 16) = 5 meters. The distance from A to C is 6 meters. B is closer under this geometry. Now consider a separate feature-space example with the same numerical coordinates but an intentional rescaling of the first feature by 10. The transformed points are A′ = (20, 1), B′ = (50, 5) and C′ = (20, 7). The distance A′ to B′ is √(900 + 16) = √916, approximately 30.27, while A′ to C′ remains 6. The nearest point changes to C. This is a change in the metric representation, not a contradiction in the distance formula. Also distinguish distance from squared distance: the original A-to-B squared distance is 25 square meters. Squaring preserves pairwise ordering for nonnegative distances, but it does not preserve every property of a distance metric.
Illustrative case study
In a fictional customer-analysis project, Harbor Retail compares records using purchase count and spending. Analysts notice that raw spending values dominate the squared differences because of their numeric scale. They define and validate a scaling policy, then review how the resulting neighborhoods change. They keep the transformation fitted on the training data and apply it consistently later. The case illustrates representation choices, not a claim that scaling necessarily improves every model or reveals true customer similarity.
Common misconceptions and mistakes
Euclidean distance is not the same as Manhattan distance, which adds absolute coordinate differences. It is not a road-network distance, and geographic coordinates in degrees require appropriate treatment rather than casual use as flat distances. Squared Euclidean distance is not a metric: on the line, the squared distance from 0 to 2 is 4, exceeding 1 + 1 through the intermediate point 1. Missing values cannot simply be ignored without defining how comparisons remain comparable. Large feature counts can also make distance behavior unintuitive, so inspect actual neighborhoods rather than trusting a familiar formula alone.
Business use
Select Euclidean distance when straight-line differences in the chosen features express the intended comparison. Document units, transformations and any coordinate weights. Use the same preprocessing for reference and new observations, and avoid fitting scaling choices on held-out evaluation data. Inspect examples that the metric calls near and far. For heterogeneous or categorical features, consider whether another representation or distance definition is more appropriate. Check whether downstream software expects distance or squared distance, because thresholds and aggregation can change even when nearest-neighbor ordering does not.
People also ask
Can Euclidean distance be used in more than three dimensions?
Yes. The formula extends to any finite number of real coordinates.
Does standardizing features always solve the scaling problem?
No. It supplies one convention, but the appropriate geometry depends on the task and data.
Is the distance between two unit vectors the same as cosine similarity?
No, although they are related: squared Euclidean distance equals 2 minus twice their dot product when both have unit norm.
Check your understanding
Why did rescaling one coordinate change the closest point in the worked example?
Answer: It increased that coordinate's contribution to the sum of squared differences. Euclidean closeness depends on the representation, including feature scales.
Related terms
Learn next
recompute the geometric example using Manhattan distance and compare the rankings. Then verify the squared-distance triangle-inequality counterexample at 0, 1 and 2. For a real feature table, examine how a documented scaling choice changes a few nearest neighbors.
More in Mathematics and optimization
- Absolute error loss - Absolute error loss measures a numerical prediction's error as the absolute difference between its predicted and reference values: L(y,y-hat)=|y-y-hat|.
- Active-set method - An active-set method is an optimization approach that works with a changing subset of constraints believed to be tight at the solution.
- Adjoint state method - The adjoint state method computes how a chosen objective changes when the parameters of a mathematical model change.
- Σ-algebra
- Algebraic connectivity - Algebraic connectivity is the second-smallest eigenvalue of a graph's Laplacian matrix.
- Algorithm IMED - Algorithm IMED, short for Indexed Minimum Empirical Divergence, is a method for choosing actions in a stochastic multi-armed bandit problem.
Sources
Sources: Jung and colleagues, The Aalto Dictionary of Machine Learning, Euclidean distance, https://aaltodictionaryofml.github.io/ADictML_Main.pdf ; SciPy, euclidean distance documentation, https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.euclidean.html . The coordinate calculations, rescaling example and fictional retail study are original.
Previous: ETL Next: Euclidean norm