Nearest Neighbors
Euclidean Distance
Compute the Euclidean distance between two points as √Σ(aᵢ−bᵢ)². A single
loop accumulates the squared difference per dimension; math.sqrt at the
end. Library: np.linalg.norm(np.array(a) - np.array(b)) — single call,
same formula. RESULT: distance (rounded).
By hand
With NumPy
np.linalg.norm computes the L2 norm of the difference vector — identical
to the loop formula for Euclidean distance.
naive.py
import math
a = [1, 2, 3]
b = [4, 6, 3]
n = len(a)
sq_sum = 0.0
for i in range(n):
diff = a[i] - b[i]
sq_sum = sq_sum + diff * diff
dist = math.sqrt(sq_sum)
print('RESULT:', round(dist, 4))
library.py
import numpy as np
from dalib.display import set_display
set_display()
a = [1, 2, 3]
b = [4, 6, 3]
dist = float(np.linalg.norm(np.array(a) - np.array(b)))
print('a:', a)
print('b:', b)
print('RESULT:', round(dist, 4))
a: [1, 2, 3]
b: [4, 6, 3]
RESULT: 5.0
Implementation notes
- Euclidean distance generalizes the Pythagorean theorem to n dimensions. For 2D points it reduces to √((x₂−x₁)²+(y₂−y₁)²).
- The loop accumulates
diff*diff(explicit multiply) so each squared term appears as a distinct step in the replay trace, keeping the squaring operation visible rather than folded into a single expression. - Scale sensitivity: dimensions with large magnitudes dominate. Standardize
features before computing distances — see
standardize-features(ch01). - Cross-reference:
knn-classify-majority(this chapter) applies this distance to find nearest neighbors.