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
a=[1,2,3], b=[4,6,3]. Per dimension: (4−1)²=9, (6−2)²=16, (3−3)²=0. sq_sum=25, dist=√25=5.0.
naive.py
Replay: real traced execution (multi-file project)
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))
import math
1import math2a = [1, 2, 3]a ← [1, 2, 3]
1import math2a = [1, 2, 3]3b = [4, 6, 3]values this step[1, 2, 3]ab ← [4, 6, 3]
2a = [1, 2, 3]3b = [4, 6, 3]4n = len(a)values this step[4, 6, 3]bn ← 3
3b = [4, 6, 3]4n = len(a)5sq_sum = 0.0values this step3nsq_sum ← 0.0
4n = len(a)5sq_sum = 0.06for i in range(n):values this step0.0sq_sumi ← 0
5sq_sum = 0.06for i in range(n):7 diff = a[i] - b[i]values this step0idiff ← -3
6for i in range(n):7 diff = a[i] - b[i]8 sq_sum = sq_sum + diff * diffvalues this step-3diffsq_sum ← 9.0
7 diff = a[i] - b[i]8 sq_sum = sq_sum + diff * diff9dist = math.sqrt(sq_sum)values this step0.0 → 9.0sq_sumi ← 1
5sq_sum = 0.06for i in range(n):7 diff = a[i] - b[i]values this step0 → 1idiff ← -4
6for i in range(n):7 diff = a[i] - b[i]8 sq_sum = sq_sum + diff * diffvalues this step-3 → -4diffsq_sum ← 25.0
7 diff = a[i] - b[i]8 sq_sum = sq_sum + diff * diff9dist = math.sqrt(sq_sum)values this step9.0 → 25.0sq_sumi ← 2
5sq_sum = 0.06for i in range(n):7 diff = a[i] - b[i]values this step1 → 2idiff ← 0
6for i in range(n):7 diff = a[i] - b[i]8 sq_sum = sq_sum + diff * diffvalues this step-4 → 0diffsq_sum = sq_sum + diff * diff
7 diff = a[i] - b[i]8 sq_sum = sq_sum + diff * diff9dist = math.sqrt(sq_sum)for i in range(n):
5sq_sum = 0.06for i in range(n):7 diff = a[i] - b[i]dist ← 5.0
8 sq_sum = sq_sum + diff * diff9dist = math.sqrt(sq_sum)10print('RESULT:', round(dist, 4))values this step5.0diststdout ← RESULT: 5.0
9dist = math.sqrt(sq_sum)10print('RESULT:', round(dist, 4))values this stepRESULT: 5.0stdout
With NumPy
np.linalg.norm computes the L2 norm of the difference vector — identical
to the loop formula for Euclidean distance.
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 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.