Contents
02The rule: let the neighbors vote
A new wine arrives, and we want its cultivar. We have no formula for cultivars, only 124 wines whose cultivar we know. The idea is the one we use every day: things that look alike tend to be alike. A doctor compares a patient with similar past patients; a shop recommends what customers like you bought. k-nearest neighbors turns this into a rule.
2.1Distance, the k closest, a vote
Definition 1 (The k-nearest neighbors classifier, Murphy, 2022, eq. 16.1). Given training points (x_i,y_i) and a new input x:
measure the distance from x to every training input, usually the Euclidean (straight-line) distance
\operatorname{dist}(x,x_i)=\sqrt{\sum_{j=1}^{d}\;(x_j-x_{ij})^2}\;; \tag{1}- take the k training points with the smallest distances, N_k(x);
let them vote: the share of the neighbors in class c,
\hat p(c\mid x)=\frac1k\sum_{i\in N_k(x)}\mathbb I(y_i=c), \tag{2}estimates the probability of class c near x, and the prediction is the majority class,
\hat y=\operatorname*{arg\,max}_c\;\hat p(c\mid x).
The Euclidean distance (1) is Pythagoras in d dimensions: take the difference in every measurement, square it, add the squares, take the square root. The vote (2) is the class makeup of a small neighborhood around x, read as a probability. To predict a number instead, such as a price, average the neighbors’ values,
Worked example 1 (One new wine). Take two measurements, alcohol and flavanoids (Figure 1). The new wine has alcohol 13.77 and flavanoids 2.79. The closest training wine has 13.76 and 2.74, so by (1) its distance is
We compute this distance for all 124 training wines and sort them. With k=5, the five closest are four wines of cultivar one and one of cultivar two, so (2) gives \hat p=(0.8,\,0.2,\,0) for the three cultivars, and we predict cultivar one. The new wine is indeed cultivar one.
2.2No training: the model is the data
Most models are fitted once and then the training data can be thrown away: logistic regression keeps only its weights. k-nearest neighbors keeps everything and does all the work at prediction time. That is why it is called instance-based, memory-based or lazy learning (Murphy, 2022, §16). It is also nonparametric: it has no fixed set of parameters, and its flexibility grows with the data. The price is paid later, at every prediction (Chapter 5).
Two practical details. When two classes tie in the vote, a common rule is to choose an odd k for two classes, which makes ties impossible; with more classes ties can still happen, and they are broken by the nearest neighbor or by weighting each vote by the inverse of its distance (Dudani, 1976). And the predicted probability (2) is coarse: with k=5 it can only be 0, 0.2, 0.4, 0.6, 0.8 or 1.
Pause and think. The method does not train. So what, exactly, did we choose when we set up the classifier of Worked example 1?
The distance (1) adds up raw differences. With two measurements of similar size that is fine. But with all thirteen measurements, does every one count fairly?