From memorizing to neighbours
The topic Validation and overfitting built a protocol that measures any model honestly: hold-out and -fold cross-validation, a test set touched once, nested cross-validation when a model is chosen among many. So far it had little to measure. The only models were thought experiments, such as the memorizer from What machine learning is: it returns for a training object and a default answer for everything else.
The smallest change that makes the memorizer useful is to answer a new question with the answers to the most similar questions seen before. That is the nearest-neighbour method (Fix and Hodges, 1951), the first real model of the course.
It needs a distance between objects. For a query , let be the indices of the training objects nearest to ; in this topic always counts neighbours. For classification, predict the most frequent class among them:
For regression, predict the mean of their targets:
The topic Conditional expectation showed that the best predictor under squared loss is . The regression rule estimates it locally: it averages over the training points whose lies close to . In the same way, the vote shares estimate the class probabilities . "Training" is just storing the sample; all the work happens at prediction time.
A worked example. The query is . Two training points have class : and . Three have class : , and . The Euclidean distances from the query to are , , , and .
- : the only neighbour is , so the answer is .
- : , and vote two to one for .
- : all five points vote three to two for .
The answer depends on , and so does its quality.