Salta al contenuto
Note per Studenti Nearest Neighbour Report

Nearest Neighbour

In questa pagina 3

Source note. The only lecture material on this topic is one handwritten page (plus its use as a model in the notebooks). Textbook chapter 19.1 (nearest neighbour) is cited by the course but not reproduced; nothing beyond the page is stated here.

1. Setting

The sketch shows objects described by two features x1,x2x_1,x_2, collected in a data matrix XX with columns x1,x2x_1,x_2 and one row per object. Three classes are drawn: C1C_1 (squares), C2C_2 (circles) and C3C_3 (triangles), the first and third partly interleaved. Two test points (marked "?") are connected by dashed lines to training points around them; a closed curve is drawn around the nearest ones. The test point's class is decided by the classes of those neighbours.

2. The algorithm (KNN, K-nearest neighbour)

Training phase. Almost nothing: the page writes a cost "O(c)O(c) or" a cost in terms of nn (the second symbol is hard to read), i.e. either nothing is done or the nn training objects are stored.

Test phase, for one given point:

  1. compute the distances from all training points;
  2. sort these distances;
  3. keep the first KK elements (the shortest distances);
  4. predict according to the (weighted) majority of their classes.

"Weighted" means the neighbours need not count equally (for instance closer neighbours may count more); the page does not specify the weights. The distance function is not specified either; the sketch draws it as the length of the dashed segment.

Consequences visible from the procedure (my remarks): all the cost is paid at prediction time, since each prediction needs a distance to every training object; and KK is a hyper-parameter, chosen before looking at the test data, not learned by the algorithm (see Model Selection and Validation - Hold-Out, k-Fold and Statistical TestsTo decide which of two models is better, fix their hyper-parameters, train each on a training set (this fits the parameters, the vector w or theta) and measure a performance score such as F1 on a separate test set; one split is too fragile, so the hold-out is repeated K times (or K-fold cross-validation is used) and the paired scores of the two models are compared with a statistical test. The null hypothesis H0 is that the two models perform equally; the significance level alpha is fixed before the experiment, and the p-value is the probability of observing the obtained differences under H0. Parametric test: t-test; non-parametric: Wilcoxon.Model Selection and Validation - Hold-Out, k-Fold and Statistical Tests →).

3. KNN in the rest of the course

Versione ripasso

Teoria collegata