How do you predict a new case from similar cases? (k-nearest neighbours, distance, standardising)
In brief
k-nearest neighbours (kNN) is one of the simplest learning methods: for a new case it looks for the k training cases that are most similar to it in the features, and predicts the class that is most common among these k. Similar means: small distance, measured as with a ruler in the scatter plot (Euclidean distance). Nothing is learnt in advance; the method only remembers the training cases.
An example by hand
A penguin from the test data has a bill depth of 19.3 mm and weighs 3450 g; it is female, but the method does not know that. Its five nearest neighbours in the training data (distance in standardised units):
| Neighbour | Bill depth | Body mass | Distance | Sex |
|---|---|---|---|---|
| 1 | 19.2 mm | 3500 g | 0.081 | male |
| 2 | 19.4 mm | 3525 g | 0.108 | female |
| 3 | 19.0 mm | 3450 g | 0.149 | female |
| 4 | 19.0 mm | 3425 g | 0.152 | male |
| 5 | 19.1 mm | 3550 g | 0.161 | male |
- k = 1: only the nearest neighbour counts, and it is male. Prediction male, wrong.
- k = 3: male, female, female. Two to one for female, right.
- k = 5: plus male and male. Three to two for male, wrong again.
The same case, three answers: k is the most important setting of the method. How to choose k without using the test data is shown in the unit on overfitting.
Why standardise?
Distance counts every unit equally. In the training data body mass has a standard deviation of 786.4 g, bill depth one of 2.01 mm. If two animals are 2 mm and 100 g apart, raw bill depth contributes 4 and mass 10000 to the squared distance: bill depth plays almost no role. Standardised (each variable divided by its standard deviation) the contributions are 0.99 and 0.016; now the difference in bill depth counts, because it is large relative to the usual spread. Without standardising, the three nearest neighbours of the penguin above all weigh exactly 3450 g; their bill depth (19.0, 18.8 and 18.6 mm) only counts when the mass is the same. On the 100 test animals the error at k = 15 thus rises from 15% to 32%.
The mean and standard deviation come only from the training data (means 17.15 mm and 4207.8 g); the same numbers are applied to the test data.
When do I use it?
- To understand training and test error, overfitting and cross-validation: with kNN everything can be seen in one picture.
- With few features and boundaries between the classes that are not straight lines.
Requirements
- Numbers as features, in comparable units or standardised.
- Enough training cases in every class; with many features all cases become similarly far apart, and kNN loses sharpness.
How it works in Statable
- Open Understand > Learning lab > tab "Predicting (k-nearest neighbours)" (Ctrl+K: "predicting").
- Choose "Example: sex from bill depth and body mass". With seed 1 and 70% training data Statable splits the animals differently from R; the numbers in this article come from R and therefore differ a little. The picture stays the same.
- Move the slider k, switch "standardise measures" on and off and compare the sentences, the scatter plot, the confusion matrix and the error curve.
With your own data: New analysis → "Can something be predicted?" → in the result "How well does my model predict new cases? (k-nearest neighbours, cross-validation)". There you choose a group as the target (2 to 8 groups) and number columns as features. Statable puts 70 % into the training data, chooses k with 5-fold cross-validation in the training data, and shows accuracy, confusion matrix and the error curve. "As R script with data (.zip)" recalculates the same in R with the same split.
Tied votes (with two classes only with an even k or equally distant neighbours): Statable takes the class of the nearest neighbour. In class::knn, R then decides at random.
Limits
- kNN gives no coefficients and no p-values; it does not say which feature matters how much.
- Categories as features fit badly with a ruler-type distance.
What is often misunderstood
Sources
James, G., Witten, D., Hastie, T. & Tibshirani, R. (2021). An Introduction to Statistical Learning (2nd ed.). Springer; free as PDF. Ch. 2.2 (classification, k-nearest neighbours, training and test error), ch. 4 (classification, confusion matrix, ROC), ch. 5.1 (cross-validation). Venables, W. N. & Ripley, B. D. (2002). Modern Applied Statistics with S (4th ed.). Springer (R package class, function knn). Data: Gorman, K. B., Williams, T. D. & Fraser, W. R. (2014). PLoS ONE, 9(3), e90081 (palmerpenguins, CC0).
Take away
- kNN: majority of the k most similar training cases.
- Standardise features, using statistics from the training data.