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 , collected in a data matrix with columns and one row per object. Three classes are drawn: (squares), (circles) and (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 " or" a cost in terms of (the second symbol is hard to read), i.e. either nothing is done or the training objects are stored.
Test phase, for one given point:
- compute the distances from all training points;
- sort these distances;
- keep the first elements (the shortest distances);
- 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 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
- The comparison of two models in 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 → uses Model 1 = KNN with and Model 2 = KNN with , scored with the measure of Classification Metrics - Confusion Matrix, Precision, Recall and F1The performance of a classifier is read from the confusion matrix, whose rows are the actual classes and columns the predicted ones: TP, FN / FP, TN for a binary problem. Accuracy = (TP + TN) / total; recall = TP / (TP + FN); precision = TP / (TP + FP); F1 = 2 recall precision / (recall + precision). With an unbalanced data set accuracy can look good while recall is poor (10 positive and 90 negative objects: accuracy 0.87, recall 0.10, precision 0.20). In multi-class problems the confusion matrix is square and recall and precision are computed class by class from the diagonal.Classification Metrics - Confusion Matrix, Precision, Recall and F1 →.
- In the notebooks
KNeighborsClassifier()is one of six scikit-learn classifiers (lr,per,knn,dt,svc,rf) compared by cross-validation; in the last example, with limited computing power, the notebook decides to run the experiments with KNN only (see Python Notebooks - scikit-learn Model Comparison and Wilcoxon TestTwo example notebooks compare six scikit-learn classifiers (logistic regression, perceptron, KNN, decision tree, SVC, random forest) on a real data set loaded from the UCI repository: hold-out split with train_test_split, F1 score, k-fold cross-validation with cross_validate and scoring f1_macro, box plots of the fold scores, and the Wilcoxon signed-rank test (scipy.stats.wilcoxon) on the paired fold scores; the second notebook adds a stratified test set, more folds on the best models and the final training and test of the chosen model. A separate short notebook draws two normal samples to see how often the Wilcoxon test rejects at the 0.05 level.Python Notebooks - scikit-learn Model Comparison and Wilcoxon Test →).
Versione ripasso
- Features (columns of ); classes , , . A test point takes the class of its nearest training points.
- Training: practically nothing; the data are stored.
- Test, for one point: 1) distances from all training points; 2) sort them; 3) keep the first ; 4) predict by the (weighted) majority of their classes.
- Weights and distance function are not given in the notes.
- is a hyper-parameter (comparison example: against ), chosen before looking at the test data.
- Cost is paid at prediction time: every prediction needs all the distances.
- Used in the notebooks as
KNeighborsClassifier()among six classifiers; see Python Notebooks - scikit-learn Model Comparison and Wilcoxon TestTwo example notebooks compare six scikit-learn classifiers (logistic regression, perceptron, KNN, decision tree, SVC, random forest) on a real data set loaded from the UCI repository: hold-out split with train_test_split, F1 score, k-fold cross-validation with cross_validate and scoring f1_macro, box plots of the fold scores, and the Wilcoxon signed-rank test (scipy.stats.wilcoxon) on the paired fold scores; the second notebook adds a stratified test set, more folds on the best models and the final training and test of the chosen model. A separate short notebook draws two normal samples to see how often the Wilcoxon test rejects at the 0.05 level.Python Notebooks - scikit-learn Model Comparison and Wilcoxon Test → and 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 →. - Typical errors: choosing on the test set; forgetting that every prediction needs all distances.
Teoria collegata
- Classification Metrics - Confusion Matrix, Precision, Recall and F1
- Introduction to Machine Learning - Learning, Data and Tasks
- Introduction to Machine Learning - Program Map
- Model Selection and Validation - Hold-Out, k-Fold and Statistical Tests
- Python Notebooks - scikit-learn Model Comparison and Wilcoxon Test