CAB212 · Unit 3: Classification
k-nearest neighbours
8 practice questions
Key points
- Lazy learner: stores data
- Distance + majority vote
- Small k -> high variance
- Scale features; tune k
k-NN is a lazy, instance-based method: there is no training phase beyond storing data. To classify a point, find its k nearest training points (usually by Euclidean distance) and take a majority vote (classification) or average (regression).
Euclidean distance: d = sqrt(sum((a_i - b_i)^2)). Other choices: Manhattan, Minkowski, cosine.
- Small k: flexible, low bias, high variance (noisy). k = 1 memorizes training data.
- Large k: smoother, higher bias.
- Choose odd k for binary problems to avoid ties; tune k with cross-validation.
Worked example: query (0,0); A(1,0) positive, B(0,2) negative, C(3,3) negative. Distances 1, 2, 4.24. k=1 gives positive; k=3 votes +,-,- giving negative.
Costs: prediction is O(n*d) per query, so it is slow on large data; features must be scaled; it suffers in high dimensions (curse of dimensionality).
Exam pitfall: k-NN needs no training time but has expensive prediction.
Try two questions
k-NN is called a lazy learner because it:
- Never makes any predictions
- Uses no training data at all
- Trains for many epochs before predicting
- Defers computation until prediction time
Show the answer
D. It simply stores training data.
Unscaled features hurt k-NN because:
- Large-range features dominate the distance
- k-NN relies on gradient descent to fit
- k-NN requires all features to be integers
- Scaling changes the class labels
Show the answer
A. Distance is not scale invariant.