K-Nearest Neighbours
Source: Unit 2 §2
The algorithm's idea
- All instances are points in -dimensional space.
- "Nearest neighbours" are defined by a distance measure .
- The target function may be discrete or real-valued:
- discrete - return the most common value (mode) among the nearest neighbours;
- real-valued - return the mean of the nearest neighbours' values.
The training and classification algorithms
Training is trivial, because KNN is lazy:
Classification, given a query instance xq:
- Load the data (CSV / XLS).
- Initialize K, the hyperparameter: 3, 5, 7, …
- For each sample in the training data:
- calculate
distance(query, current point); - add the pair
(distance, index)to an ordered collection.
- calculate
- Sort the collection by distance, ascending.
- Take the first K entries.
- Get the labels of those K entries.
- If classification, return the mode of the K labels.
- If regression, return the mean of the K values.
Choosing K
Use 3, 5, 7, … so that a binary vote cannot tie. With k = 4 and two
neighbours of each class you cannot decide at all - see the worked example
below.
| Small K | Large K |
|---|---|
| Captures the structure of the problem space better | Less sensitive to noise, especially class noise |
| May be necessary for a small training set | Gives better probability estimates for discrete classes |
| Prone to noise | Needs a large training set |
With K = 1 the space is divided into regions called the Voronoi partition: each region contains exactly the points closest to one training example.
The elbow method
- Compute the error rate for different K values.
- Plot error rate against K. The elbow of that curve is the optimal K.
- Retrain with the best K and redo the classification report and confusion matrix.
Worked example
The data set has two attributes and a binary class:
| Attribute1 | Attribute2 | Class |
|---|---|---|
| 7 | 7 | False |
| 7 | 4 | False |
| 3 | 4 | True |
| 1 | 4 | True |
Several distance measures are available - Euclidean, Manhattan, Minkowski and others. Here we use Euclidean:
Only if all three of these hold:
- the attributes have a similar scale;
- the attributes are scaled to equal range and equal variance;
- the classes are spherical.
The problem: classify x = (Attribute1 = 3, Attribute2 = 7) with k = 3.
| Point | Distance to (3,7) | Class |
|---|---|---|
| (7,7) | √((3−7)² + (7−7)²) = √16 = 4 | False |
| (7,4) | √((3−7)² + (7−4)²) = √25 = 5 | False |
| (3,4) | √((3−3)² + (7−4)²) = √9 = 3 | True |
| (1,4) | √((3−1)² + (7−4)²) = √13 = 3.6 | True |
- Sort by distance: 3 at (3,4), 3.6 at (1,4), 4 at (7,7), then 5 at (7,4).
- Take the first : (3,4) True, (1,4) True, (7,7) False.
- The majority is True, two votes against one.
- Prediction: x is classified as
True. - Now try : the neighbours become 2 True and 2 False, a tie, and the query cannot be classified. This is why K must be odd.
KNN for regression
For a real-valued target, KNN returns the average of the target values of the nearest neighbours:
taken over the nearest .