MoonSay /Tutorials/Machine Learning Interview #17
Contents
  1. 01Introduction
  2. 02The rule
  3. 03Scale first
  4. 04Choosing k
  5. 05Interview follow-ups
  6. 06Common misconceptions
  7. 07Interview questions and model answers
  8. 08Summary
  9. References
k-Nearest Neighbors/Chapter 02 of 08

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:

  1. 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}
  2. take the k training points with the smallest distances, N_k(x);
  3. 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,

\hat y=\frac1k\sum_{i\in N_k(x)}y_i . \tag{3}
The rule on two of the 13 measurements. (a) The 124 training wines, colored by cultivar, and a new wine from the test set (star); the circle encloses its five nearest training wines. (b) The dashed box of (a), enlarged: the five nearest wines, numbered by distance, and the vote. Both axes use the same scale, so the neighborhood is a circle. Data of Forina et al. (1988); computed for this tutorial, with the same data as the companion video.
Figure 1. The rule on two of the 13 measurements. (a) The 124 training wines, colored by cultivar, and a new wine from the test set (star); the circle encloses its five nearest training wines. (b) The dashed box of (a), enlarged: the five nearest wines, numbered by distance, and the vote. Both axes use the same scale, so the neighborhood is a circle. Data of Forina et al. (1988); computed for this tutorial, with the same data as the companion video.
Try itLet the neighbors vote

Click anywhere in the plot to place a new wine, and choose k. The page measures the Euclidean distance from the new wine to all 124 training wines of Figure 1, draws the k nearest and the circle that holds them, and counts their votes. Both axes use the same scale, so the circle is round.

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

\sqrt{(13.77-13.76)^2+(2.79-2.74)^2}=\sqrt{0.0001+0.0025}=0.051 . \tag{4}

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?

Show the answer

Three things: which measurements to use, the distance (1), and k. These choices are the whole model, and the next two chapters are about making them well: the distance in Chapter 3, k in Chapter 4.

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?