Classification Metrics - Confusion Matrix, Precision, Recall and F1
In questa pagina 4
How to judge the classifiers of Halfspaces and the Perceptron AlgorithmA perceptron computes h(x) = sign(w^T x) where x includes a constant coordinate x0 = 1 that absorbs the threshold into the bias weight w0; its decision boundary is the hyperplane w^T x = 0 (a line in the plane, x2 = -(w1/w2) x1 - w0/w2). The Perceptron Learning Algorithm scans the data and, on every misclassified point, replaces w by w + y x. A single perceptron cannot separate the exclusive-or pattern; two perceptrons feeding a third give a multilayer network.Halfspaces and the Perceptron Algorithm →, Logistic RegressionLogistic regression models the probability p of the positive class: the logit (log of the odds p/(1-p)) is assumed linear in the input, logit(p) = w^T x = z. Inverting it gives p = 1/(1 + e^(-z)), the logistic sigmoid; an object goes to class 1 when the sigmoid exceeds 0.5, i.e. when z > 0. The parameters are learned by maximising the Bernoulli log-likelihood, equivalently minimising the cost J = -y log(yhat) - (1 - y) log(1 - yhat), by gradient descent with gradient (1/m) X^T (h(x) - y).Logistic Regression → and 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 →. The notebooks compute the F1 score of six models on held-out data and in cross-validation (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 →); the comparison of two models is 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 →.
1. Target, hypothesis, actual and predicted output
The target function gives the actual output (the "true output"); the hypothesis gives the predicted output. Evaluating a classifier means comparing the two on the same objects: the vector of the actual outputs against the vector of the predictions. The classification can be binary or multi-class, and, as another distinction written in the notes, single-label or multi-label (the notes name this distinction; the material that follows is single-label).
2. Binary classification: the confusion matrix
Classes: positive and negative. Rows are the actual class, columns the predicted class:
predicted
Positive Negative
actual
Positive TP FN
Negative FP TN- TP (true positive): actual positive, predicted positive.
- FN (false negative): actual positive, predicted negative.
- FP (false positive): actual negative, predicted positive.
- TN (true negative): actual negative, predicted negative.
The page builds it from two label vectors, entry by entry: is a TP; a TN; an FP; an FN.
3. Accuracy, recall, precision, F1
(Accuracy is the share of correctly classified objects over all objects; on the page the terms in the numerator and , in the denominator are circled.) Recall asks: of all the actual positives, how many did we find? Precision asks: of all objects we called positive, how many are really positive?
Worked example of the lecture
A data set has 10 positive and 90 negative objects (100 in total).
Classifier A
Pred P Pred N
Actual P 6 4
Actual N 5 85
Classifier B
Pred P Pred N
Actual P 1 9
Actual N 4 86
(Computed here from the formula, not on the page: .)
B has a high accuracy and nearly useless recall and precision, because 90 of the 100 objects are negative and most of them are classified correctly anyway. A second extreme case on the next page: a classifier that answers "positive" for every object gives
Pred P Pred N
Actual P 10 0
Actual N 90 0with recall (it finds all the positives) and precision (it calls positive 100 objects, 10 of which are). The page also writes the percentages and on the right of these results; their meaning is not clear from the handwriting. The page also notes the figure near the accuracy formula, which is the accuracy of a classifier that always answers "negative" on this data set (my reading: the matrix is not drawn).
Lesson of the example: accuracy alone is not enough when the classes are unbalanced; recall, precision and show what accuracy hides.
4. Multi-class classification
With more classes the per-class view is used: for each class one looks at the classifier "class against the rest" and computes its recall and precision.
The confusion matrix becomes square: rows are the actual class , columns the predicted class . The page fills it from two label vectors of length ten (actual and predicted ), with crosses for the errors. The diagonal entries are the true positives of each class; the row of class is "total class " (all objects whose actual class is ); the column of class collects all the objects the classifier assigned to class . Then
(The numerator of accuracy is the sum of the diagonal.) The same page draws three clouds in the plane with three lines, each with a hatched side, and test points marked "?" that fall on the hatched side of several lines or of none: with one linear classifier per class an object can be claimed by more than one class or by none (the notes do not name the scheme).
Versione ripasso
- Target = actual output; hypothesis = predicted output. Compare the vector of actual outputs with the vector of predictions on the same objects.
- Binary confusion matrix (rows actual, columns predicted): Actual P gives TP, FN; Actual N gives FP, TN. Entry by entry: TP; TN; FP; FN.
- Recall: of all actual positives, how many were found. Precision: of all objects called positive, how many are really positive.
- Example (10 positive, 90 negative). Rows and : accuracy . Rows and : accuracy , recall , precision , .
- Answering "positive" to everything: recall , precision . With unbalanced classes accuracy hides poor recall and precision.
- Multi-class: square matrix; diagonal entries ; accuracy correcttotal; (row); assigned to class (column); one class against the rest.
- Typical errors: swapping rows and columns (precision and recall exchange); trusting accuracy on unbalanced data; dividing recall by the predicted total.
- Used for: Halfspaces and the Perceptron AlgorithmA perceptron computes h(x) = sign(w^T x) where x includes a constant coordinate x0 = 1 that absorbs the threshold into the bias weight w0; its decision boundary is the hyperplane w^T x = 0 (a line in the plane, x2 = -(w1/w2) x1 - w0/w2). The Perceptron Learning Algorithm scans the data and, on every misclassified point, replaces w by w + y x. A single perceptron cannot separate the exclusive-or pattern; two perceptrons feeding a third give a multilayer network.Halfspaces and the Perceptron Algorithm →, Logistic RegressionLogistic regression models the probability p of the positive class: the logit (log of the odds p/(1-p)) is assumed linear in the input, logit(p) = w^T x = z. Inverting it gives p = 1/(1 + e^(-z)), the logistic sigmoid; an object goes to class 1 when the sigmoid exceeds 0.5, i.e. when z > 0. The parameters are learned by maximising the Bernoulli log-likelihood, equivalently minimising the cost J = -y log(yhat) - (1 - y) log(1 - yhat), by gradient descent with gradient (1/m) X^T (h(x) - y).Logistic Regression →, 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 →, and model comparison 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 →.
Teoria collegata
- 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
- Nearest Neighbour
- Python Notebooks - Exercises on Perceptron, Regression and Logistic Regression
- Python Notebooks - scikit-learn Model Comparison and Wilcoxon Test