Lecture
Quality assessment in classification tasks — is the process of measuring the effectiveness of a machine learning model in predicting the classes of objects. There are several standard metrics that are commonly used to evaluate classification quality:
Accuracy:
Precision:
Recall (Sensitivity):
F1-Score:
ROC-AUC (Receiver Operating Characteristic - Area Under the Curve):
Confusion Matrix:
Specificity:
Cohen's Kappa:
The choice of metrics depends on the specific task and business requirements. It is important to consider the context and the cost of errors depending on the goals of classification.
Here, when we talk about TP, TN, FP, FN and the concepts expressed through them, we are speaking within the context of a single class of binary classification. That is, in such a system it is implied that the actual number of objects of class 0 (for the binary case 0/1) can be expressed as TP₀ + FN₀ = FP₁ + TN₁
Confusion matrix (error / mismatch / loss matrix, CM)
— is a square matrix of size k × k, where CMt,c — is the number of objects of class t , that were classified as class c , and k — is the number of classes. The values of the CM cells can be computed using the formula:
, where yi — is the actual class of the object, and yi^ — is the predicted one.
For the binary case:
| Belongs to class (P) | Does not belong to class (N) | |
|---|---|---|
| Predicted to belong to the class | TP | FP |
| Predicted not to belong to the class | FN | TN |
For multiclass classification, the confusion matrix is constructed following the same principle:
| Predicted class | Class 1 (C₁) | Class 2 (C₂) | Class 3 (C₃) |
|---|---|---|---|
| 1 (P₁) | T₁ | F₁₂ | F₁₃ |
| 2 (P₂) | F₂₁ | T₂ | F₂₃ |
| 3 (P₃) | F₃₁ | F₃₂ | T₃ |
In this case, TP, TN, FP, and FN are calculated relative to some class (i) as follows:

Acc=(TP + TN)/(TP + TN + FP + FN)
Recall=TP/(TP + FN)
Precision=TP/(TP + FP )
Specificity=TN /(FP + TN)
FPR=FP.(FP + TN)
Given that such metrics do not take into account the original distribution of classes in the sample (which can significantly affect the resulting value), there are also weighted variants of these metrics (in terms of multiclass classification):

Examples and images are taken from the lectures of the course «Introduction to Machine Learning» by K.V. Vorontsov
Arithmetic mean:
A=(1/2) *(precision + recall)
Thus, taking the arithmetic mean is not representative.
Minimum:
M = min(precision, recall)
That is, it reflects the quality of the classifier fairly well, without overstating it.
But it does not distinguish classifiers with different non-minimal metrics.
Harmonic mean, or F-measure:

It is the most accurate averaging, taking both metrics into account.
Geometric mean, or the Fowlkes–Mallows Index (Fowlkes–Mallows index)

A less strict measure.
For an overall assessment of classifier quality, the F₁-measure is often used. It is originally computed for the positive class in the binary classification case, and is generalized using the «one-vs-all» principle (described in more detail below, for multiclass classification). The F₁-measure — is the harmonic mean between precision and recall:

The weighted harmonic mean Fβ (F1-measure — a special case of the Fβ-measure for β = 1). Fβ measures the effectiveness of a classifier by weighting recall β times more important than precision:

F-measure for Multiclass Classification. Three Types of Averaging
To calculate the F-measure (and other) metrics within multiclass classification, the «one-vs-all» approach is used: each class becomes «positive» exactly once, while the rest are — negative (an example of the calculation is shown on the matrix).
Thus, depending on the stage of computation at which averaging is performed, one can calculate the micro-average, macro-average, and average F-measure (the calculation logic is shown in the diagram on the right). Micro and macro:

where for micro-average, precision and recall are computed from the averaged TP, FP, FN;
for macro-average, precision and recall are computed from the averaged precisioni, recalli;
Averaged:

where i — is the class index, and k — is the number of classes.
For a clear assessment of algorithm quality, the ROC curve is used. The curve is plotted on a plane defined by TPR (on the y-axis) and FPR (on the x-axis).
To plot the graph, soft classification is used: instead of definitively assigning an object to a class, the classifier returns the probabilities of the object belonging to various classes. This confidence is compared against a threshold (how much confidence is «enough» to assign an object to the positive class). Depending on the value of this threshold, the TPR and FPR values change.
Algorithm for constructing the curve:
Thus: the number of points does not exceed the number of objects; the ideal algorithm corresponds to an ROC curve passing through the point (0;1)(0;1); the worst algorithm (for example, a coin flip) corresponds to the straight line TPR = FPR.
To numerically evaluate an algorithm using the ROC curve, the value of the area under it is used (AUC, area under curve). The ideal algorithm has an AUC equal to 1, the worst — 0.5.
On the other hand, to construct an ROC curve it is not necessary to recalculate TPR and FPR.
There is an alternative algorithm for constructing an ROC curve.
The correctness of the algorithm is justified by the fact that, as the prediction for a single object changes depending on its class, either TPR or FPR changes (the value of the other parameter remains unchanged). Below, a different line of reasoning is described that leads to the algorithm above.
Recall that we are working with soft classification.
Let's consider examples (accuracy graphs, with color indicating the actual class of the object: red — positive, blue — negative). Let's sort our objects in increasing order of the classifier's confidence that the object belongs to the positive class. Suppose that the objects are located at an equal (unit) distance from one another.
Let's start moving the «decision boundary»: if the boundary is at zero — we decide to assign all objects to the positive class, so accuracy = 1/2. We successively shift the boundary one unit to the right:
Thus, it can be seen on the graphs on the left that:
Note that by rotating the graph 45 degrees, we obtain the ROC curves for the corresponding classifiers (the accuracy graphs on the left correspond to the ROC curves on the right). This explains the alternative algorithm for constructing the ROC curve.
Rationale: Sensitivity to Class Ratio.
Let us consider the task of identifying mathematical articles from a set of scientific articles. Suppose there are 1.000.100 articles in total, of which only 100 relate to mathematics. If we manage to build an algorithm a(x)�(�), that solves the task perfectly, then its TPR will be equal to one, and its FPR — to zero. Now consider a «bad» algorithm that gives a positive answer for 95 mathematical and 50.000 non-mathematical articles. Such an algorithm is completely useless, yet it has TPR = 0.95 and FPR = 0.05, which is extremely close to the metrics of the ideal algorithm. Thus, if the positive class is significantly smaller in size, AUC-ROC can give an inadequate assessment of the algorithm's quality, since it measures the proportion of incorrectly accepted objects relative to the total number of negatives. For example, the algorithm b(x) placing 100 relevant documents at positions 50.001 through 50.101, will have an AUC-ROC of 0.95.
Precision-recall (PR) curve.
This problem of imbalanced classes can be avoided by moving from the ROC curve to the PR curve. It is defined analogously to the ROC curve, except that instead of FPR and TPR, the axes plot recall (on the x-axis) and precision (on the y-axis). The quality criterion for a family of algorithms is the area under the PR curve (English Area Under the Curve — AUC-PR)
Comments