Euclidean Distance (KNN)
Straight-line distance between two points; the default metric for k-nearest neighbours.
Formula
Variables
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
More in Machine Learning & AI
View allLinear Regression Model
BasicPredicts a continuous value as a weighted sum of input features plus a bias.
Gradient Descent Update
BasicIteratively moves parameters in the direction that most reduces the loss.
ReLU Activation
BasicRectified Linear Unit: outputs the input if positive, else zero.
Leaky ReLU
BasicA ReLU variant that lets a small gradient flow for negative inputs.
Tanh Activation
BasicSquashes input to the range (-1, 1); zero-centred activation.
Softmax
BasicTurns a vector of scores into a probability distribution that sums to 1.