Machine Learning & AI
Basic

Euclidean Distance (KNN)

Straight-line distance between two points; the default metric for k-nearest neighbours.

Formula

d(p,q)=∑i(pi−qi)2d(\mathbf{p},\mathbf{q}) = \sqrt{\sum_i (p_i-q_i)^2}

Variables

\mathbf{p},\mathbf{q}Two vectors
p_i,q_iComponents

Example

p=(0,0), q=(3,4): d=5

How it works

This formula measures the ordinary straight-line distance between two points in a feature space by taking the difference in each coordinate, squaring those differences, adding them up, and then square-rooting the result. The squaring makes every coordinate difference positive and gives larger mismatches more influence, while the square root returns the total to the original scale. In k-nearest neighbours, this is useful because points with smaller Euclidean distance are usually more similar under equal-weighted numeric features.

When to use it

Use it when your data points are numeric and each feature is measured on a comparable scale, such as height/weight data, coordinates, sensor readings, or simple embedding vectors. It is a common default choice in k-nearest neighbours for classification or regression when you want “closest in ordinary geometry” to mean “most similar.”

Worked example, step by step

Let p = (2, 1, 5) and q = (7, 4, 1). Step 1: Subtract coordinates: (2-7, 1-4, 5-1) = (-5, -3, 4) Step 2: Square each difference: (-5)^2 = 25, (-3)^2 = 9, 4^2 = 16 Step 3: Add the squares: 25 + 9 + 16 = 50 Step 4: Take the square root: d(p, q) = √50 Step 5: Simplify: √50 = 5√2 ≈ 7.07

Common mistakes to avoid

  • Forgetting to square the coordinate differences before adding them
  • Mixing up the order of subtraction and thinking it changes the final distance
  • Using Euclidean distance on features with very different scales without normalizing first

Did You Know?

KNN is a "lazy" learner — it does no training and simply memorises the data.

Share this formula

Reviewed & maintained by the FormulaWon editorial teamLast reviewed September 2026