Back to the 2024 paper

Module 3: Classification and Prediction

20247m

Define lazy learners and discuss how K-nearest neighbors (KNN) is used for classification.

Worked SolutionAI Assisted

Lazy Learners and K-Nearest Neighbour Classification

Definition. A lazy learner (instance-based learner) performs no, or almost no, generalisation during training: training only stores the labelled instances, and all computation is deferred until a prediction is requested. KNN is the standard example.

1. Lazy vs eager learners

Aspect Lazy learner (KNN) Eager learner (decision tree, neural net)
Training phase Stores the data only; very fast Builds an explicit model; slower
Prediction phase Expensive: queries the whole training set Cheap: one traversal of the model
Model built None, the data is the model Explicit rules, nodes or weights
Incremental data Added instantly, no retraining Usually requires rebuilding
Interpretability Low unless KK is tiny Generally higher
Main weakness Irrelevant features and feature scale Rigid model assumptions

Lazy learners adapt naturally to the local shape of the decision boundary but pay for it at query time; eager learners summarise the data, which is faster to use but may miss local detail.

2. KNN algorithm for classification

  1. Choose the number of neighbours KK (an odd value avoids ties in two-class problems) and a distance metric, Euclidean by default.
  2. Standardise or normalise the features so that one attribute does not dominate the distance.
  3. For a new instance xx, compute the distance d(x,xi)d(x, x_i) to every training instance.
  4. Select the KK instances with the smallest distances.
  5. Vote: assign the majority class; in weighted voting each neighbour contributes 1/d1/d or 1/d21/d^2.

y^=arg⁡max⁡c∑i∈NK(x)wi 1[yi=c],wi=1d(x,xi)\hat{y} = \arg\max_c \sum_{i \in N_K(x)} w_i \, \mathbb{1}[y_i = c], \qquad w_i = \frac{1}{d(x, x_i)}

3. Worked mini-example

Training point x1x_1 x2x_2 Class
A 1 2 +
B 2 3 +
C 3 1 -
D 6 5 -
E 7 6 -

Classify T=(5,4)T = (5, 4) with K=3K = 3:

Point Distance to T
D 12+12=1.41\sqrt{1^2+1^2} = 1.41
E 22+22=2.83\sqrt{2^2+2^2} = 2.83
B 32+12=3.16\sqrt{3^2+1^2} = 3.16
C 22+32=3.61\sqrt{2^2+3^2} = 3.61
A 42+22=4.47\sqrt{4^2+2^2} = 4.47

The nearest three are D (-), E (-) and B (+); the majority class is -, so TT is classified as -.

4. Practical remarks

  • Small KK gives a jagged, high-variance boundary (with K=1K = 1 there is zero training error but overfitting); large KK smooths the boundary and can wash out small clusters.
  • KNN is non-parametric, handles multiple classes naturally and needs essentially no training time.
  • Its weaknesses: prediction costs O(nd)O(nd), the whole data set must be kept in memory, and accuracy degrades as dimensionality grows (curse of dimensionality).
  • Acceleration structures include KD-trees, ball trees and approximate nearest-neighbour search; distance-weighted voting limits the damage done by distant neighbours.

Similar questions

Official AppAndroid App

PYQDeck for Android

Solve BEU previous-year exam papers anywhere, anytime. Fast search, offline paper viewing, and step-by-step solutions designed specifically for your phone.