Salta al contenuto
Note per Studenti Model Selection and Validation - Hold-Out, k-Fold and Statistical Tests Report

Model Selection and Validation - Hold-Out, k-Fold and Statistical Tests

In questa pagina 7

The question of the last lectures: given two models (for instance KNN with K=1K=1 and KNN with K=5K=5, see Nearest NeighbourK-nearest neighbours (KNN) has an almost empty training phase (the data are just stored) and does all the work at test time: given a point, compute its distance from all training points, sort the distances, keep the K shortest and predict the (weighted) majority class among them. The course source has one handwritten page on it; the notebooks use it as one of six scikit-learn classifiers, and the model-comparison example contrasts KNN with K=1 and K=5.Nearest Neighbour →), who is the best model? The scores come from the metrics 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 →; the code is in 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 →. The motivation (error on the samples is not the error on new data) is in The Statistical Learning Framework and Empirical Risk MinimizationA hypothesis is judged by an error function computed pointwise on the data and averaged: the in-sample (empirical) error E_in(h) = (1/N) sum of e(f(x_i), h(x_i)); with squared differences it is the sum of squared errors. The learning algorithms of the course all search the hypothesis set for the h with small in-sample error, but what matters is the error on new inputs drawn from the same input distribution, so the data must cover the regions where the inputs live, and part of the data must be held back to check the result.The Statistical Learning Framework and Empirical Risk Minimization →.

Source note. The pages below are the handwritten notes of the final lectures. Confidence intervals and maximum-likelihood estimation of a general parameter are in the official program but are not developed here (the Bernoulli example is in Bernoulli Distribution and Maximum Likelihood EstimationA binary outcome (heads = 1, tails = 0) is modelled by a Bernoulli distribution with parameter theta: P(x | theta) = theta^x (1 - theta)^(1 - x). For independent identically distributed observations the likelihood is the product of the single probabilities; with K heads in n tosses it is L(theta) = theta^K (1 - theta)^(n - K). Maximising the log-likelihood l(theta) = K log theta + (n - K) log(1 - theta) (maximum likelihood estimation) gives theta = K / n.Bernoulli Distribution and Maximum Likelihood Estimation →).

1. Parameters, hyper-parameters, training and test

Two models are defined by choosing their hyper-parameters (for KNN, the value of KK). Each model is then trained on the training set: the training fixes the optimisation parameters (the vector w\mathbf{w}, or θ\theta). The trained models are then evaluated on a test set, which gives a performance for each. Which is the best model?

2. Hold-out

The data set DD is split in two parts:

        dataset D
  +------------------+--------+
  |      Train       |  Test  |
  +------------------+--------+

This is the hold-out approach. The split is arbitrary, so the score depends on which objects happened to land in the test part.

Repeat the hold-out KK times. Each repetition uses a different split (the page draws three: test part at the end, in the middle, near the end) and gives a performance K1,K2,K3,…K_1,K_2,K_3,\dots:

 run 1:  [ Train .............. | Test ]            -> performance k1
 run 2:  [ Train | Test | Train ........ ]          -> performance k2
 run 3:  [ Train .............. | Te | Tr ]         -> performance k3

Running both models on the same splits gives paired scores: "Perf K1K_1" of Model 1 is paired with "Perf K1K_1" of Model 2, and so on. In the notation of the earlier example: experiment ex1ex_1 uses (Train1_1, Test1_1), experiment ex2ex_2 uses (Train2_2, Test2_2), ... and both models use the same training/test split in each experiment.

3. Comparing two models on paired scores

The first sketch places on an axis from 00 to 11 the F1F_1-scores of the three experiments:

experiment Model 1 (KNN, K=1K=1) Model 2 (KNN, K=5K=5) difference Model 1 minus Model 2
ex1ex_1 0.75 0.78 −0.03-0.03 (computed here)
ex2ex_2 0.90 0.89 +0.01+0.01 (computed here)
ex3ex_3 0.65 0.68 −0.03-0.03 (computed here)

(The numbers are read from the handwritten axis; the reading of the labels ex1,ex2,ex3ex_1,ex_2,ex_3 on the second axis is slightly uncertain.) The pairing is drawn as dotted lines joining the same experiment on the two axes ("pairing, paired test"). What matters is then the list of differences M1 exi−M2 exiM_1\,ex_i-M_2\,ex_i (written M1ex1−M2ex1M_1ex_1-M_2ex_1, M1ex2−M2ex2M_1ex_2-M_2ex_2, ...), which are plotted around the value 00.

If the models were equivalent, the differences would be spread around 00 in a bell shape: a bump with crosses on both sides, the left side meaning M1exi<M2exiM_1ex_i<M_2ex_i, the right side M1exi>M2exiM_1ex_i>M_2ex_i. If the observed differences fall far on one side, in the tail of that distribution, equivalence becomes hard to believe.

4. Significance, α\alpha and the p-value

The tails of the distribution of the differences are marked with two areas A1A_1 (left) and A2A_2 (right). The notes show the same bell curve with

  • A1+A2=5%A_1+A_2=5\% of the total distribution;
  • a wider rejection region, A1+A2=10%A_1+A_2=10\%;
  • a narrower one, A1+A2=1%A_1+A_2=1\% (the tails are almost invisible).

This total tail area is the α\alpha-value (the significance level). It is decided before the experiment. After the experiments, the differences obtained (ex1,ex2,ex3,ex4,…ex_1,ex_2,ex_3,ex_4,\dots placed on the axis) are compared with these regions, and the p-value is defined on the page as the probability of observing the specific sequence of differences in the experiments. In the notebook (see below) the rule applied to the p-value is: if it is greater than 0.050.05 the two samples are "probably the same distribution", otherwise "probably different distributions". Reading α\alpha as the probability of a false alarm when the two models are equal follows from the tail-area picture (my remark): with α=5%\alpha=5\%, in about 5 comparisons out of 100 between two equivalent models a "significant" difference will appear by chance.

5. The test: null hypothesis, parametric and non-parametric

  • Hypothesis (null, H0H_0, the initial one): the two models perform equally. The test asks whether the data are compatible with it.
  • Parametric tests: the page names the t-test; the sketch draws two bell curves (assumed shape) with samples under them. It assumes a distribution for the scores.
  • Non-parametric tests: the Wilcoxon test, which does not assume a distribution.

Wilcoxon on the paired example. Scores of Model 1 on k1,k2,k3k_1,k_2,k_3 and of Model 2 on the same folds: the page lists the pairs and the differences

P1M1−P1M2=0.8  (k1),P2M1−P2M2=−0.3  (k2),P3M1−P3M2=0.2  (k3).P_1M_1-P_1M_2=0.8\;(k_1),\qquad P_2M_1-P_2M_2=-0.3\;(k_2),\qquad P_3M_1-P_3M_2=0.2\;(k_3).

Next the absolute values are put in decreasing order and each is labelled with the model that was better in that run (the positive difference means Model 1, the negative one Model 2):

   0.8  -> M1
   0.3  -> M2
   0.2  -> M1

The drawing then stacks the ordered differences into two columns, one for M1M_1 and one for M2M_2. The notes stop here: the exact statistic computed from the two columns is not written in the course source, and it is not reproduced from memory. In practice it is obtained with scipy.stats.wilcoxon(scores_1, scores_2) (see the notebooks), which returns the statistic and the p-value.

6. Cross-validation (k-fold)

Instead of repeating random hold-outs, the data set is divided once into KK folds. In the page's example K=3K=3:

 split 1:   fold1  fold2  fold3        Train = fold1 + fold2     Test = fold3
 split 2:   fold1  fold2  fold3        Train = fold1 + fold3     Test = fold2
 split 3:   fold1  fold2  fold3        Train = fold2 + fold3     Test = fold1

Each fold plays the role of the test set exactly once, and the other folds are the training set; one performance is collected per fold. Usual values written in the notes: K=5K=5 and K=10K=10.

Pipeline (the handwritten box):

  1. create the k-fold cross-validation data set;
  2. set the hyper-parameters, which defines the model (and repeat for each model or hyper-parameter value to compare);
  3. collect the performance measures for each fold (and repeat for each model);
  4. compare the models with the paired statistical test of section 5.

The numbers "1, 2, 3" on the page mark the steps and the loops "Repeat" in a way that the handwriting leaves a little ambiguous; the above is the reading that matches the notebook.

7. The procedure in the final notebook

The last example of the course (letter recognition, 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 →) combines the ideas:

  1. hold out a test set (20% of the data, stratified) that is not touched until the end;
  2. on the training set only, run k-fold cross-validation (3 folds, then 5 on the best models) for six models, scoring with macro F1F_1;
  3. compare the best models with the Wilcoxon test; the notebook says "stop here and decide the α\alpha value" before reading the p-value;
  4. choose the best model, train it on the whole training set and evaluate it once on the test set.

The notebook also remarks that if the best model (random forest) is too expensive, a cheaper one (KNN) can be used instead.

Versione ripasso

Model Selection and Validation - Hold-Out, k-Fold and Statistical Tests

Teoria collegata