Confusion matrix

 

Terminology and derivations
from a confusion matrix
condition positive (P)
the number of real positive cases in the data
condition negative (N)
the number of real negative cases in the data

true positive (TP)
eqv. with hit
true negative (TN)
eqv. with correct rejection
false positive (FP)
eqv. with false alarmtype I error
false negative (FN)
eqv. with miss, type II error

sensitivityrecallhit rate, or true positive rate (TPR)
specificityselectivity or true negative rate (TNR)
precision or positive predictive value (PPV)
negative predictive value (NPV)
miss rate or false negative rate (FNR)
fall-out or false positive rate (FPR)
false discovery rate (FDR)
false omission rate (FOR)
prevalence threshold (PT)
threat score (TS) or critical success index (CSI)

accuracy (ACC)
balanced accuracy (BA)
F1 score
is the harmonic mean of precision and sensitivity
Matthews correlation coefficient (MCC)
Fowlkes–Mallows index (FM)
informedness or bookmaker informedness (BM)
markedness (MK) or deltaP

Sources: Fawcett (2006),[1] Powers (2011),[2] Ting (2011),[3] CAWCR,[4] D. Chicco & G. Jurman (2020, 2021),[5][6] Tharwat (2018).[7]

In the field of machine learning and specifically the problem of statistical classification, a confusion matrix, also known as an error matrix,[8] is a specific table layout that allows visualization of the performance of an algorithm, typically a supervised learning one (in unsupervised learning it is usually called a matching matrix). Each row of the matrix represents the instances in a predicted class, while each column represents the instances in an actual class (or vice versa).[9] The name stems from the fact that it makes it easy to see whether the system is confusing two classes (i.e. commonly mislabeling one as another).

It is a special kind of contingency table, with two dimensions ("actual" and "predicted"), and identical sets of "classes" in both dimensions (each combination of dimension and class is a variable in the contingency table).

ExampleEdit

Given a sample of 13 pictures, 8 of cats and 5 of dogs, where cats belong to class 1 and dogs belong to class 0,

actual = [1,1,1,1,1,1,1,1,0,0,0,0,0],

assume that a classifier that distinguishes between cats and dogs is trained, and we take the 13 pictures and run them through the classifier, and the classifier makes 8 accurate predictions and misses 5: 3 cats wrongly predicted as dogs (first 3 predictions) and 2 dogs wrongly predicted as cats (last 2 predictions).

prediction = [0,0,0,1,1,1,1,1,0,0,0,1,1]

With these two labelled sets (actual and predictions) we can create a confusion matrix that will summarize the results of testing the classifier:

Actual class
CatDog
Predicted
class
Cat52
Dog33

In this confusion matrix, of the 8 cat pictures, the system judged that 3 were dogs, and of the 5 dog pictures, it predicted that 2 were cats. All correct predictions are located in the diagonal of the table (highlighted in bold), so it is easy to visually inspect the table for prediction errors, as they will be represented by values outside the diagonal.

In abstract terms, the confusion matrix is as follows:

Actual class
PN
Predicted
class
PTPFP
NFNTN

where: P = Positive; N = Negative; TP = True Positive; FP = False Positive; TN = True Negative; FN = False Negative.

Table of confusionEdit

In predictive analytics, a table of confusion (sometimes also called a confusion matrix) is a table with two rows and two columns that reports the number of false positivesfalse negativestrue positives, and true negatives. This allows more detailed analysis than mere proportion of correct classifications (accuracy). Accuracy will yield misleading results if the data set is unbalanced; that is, when the numbers of observations in different classes vary greatly. For example, if there were 95 cats and only 5 dogs in the data, a particular classifier might classify all the observations as cats. The overall accuracy would be 95%, but in more detail the classifier would have a 100% recognition rate (sensitivity) for the cat class but a 0% recognition rate for the dog class. F1 score is even more unreliable in such cases, and here would yield over 97.4%, whereas informedness removes such bias and yields 0 as the probability of an informed decision for any form of guessing (here always guessing cat).

According to Davide Chicco and Giuseppe Jurman, the most informative metric to evaluate a confusion matrix is the Matthews correlation coefficient (MCC).[10]

Assuming the confusion matrix above, its corresponding table of confusion, for the cat class, would be:

Actual class
CatNon-cat
Predicted
class
Cat5 true positives2 false positives
Non-cat3 false negatives3 true negatives

The final table of confusion would contain the average values for all classes combined.

Let us define an experiment from P positive instances and N negative instances for some condition. The four outcomes can be formulated in a 2×2 confusion matrix, as follows:

True condition
Total populationCondition positiveCondition negativePrevalence Σ Condition positive/Σ Total populationAccuracy (ACC) = Σ True positive + Σ True negative/Σ Total population
Predicted condition
Predicted condition
positive
True positiveFalse positive,
Type I error
Positive predictive value (PPV), Precision = Σ True positive/Σ Predicted condition positiveFalse discovery rate (FDR) = Σ False positive/Σ Predicted condition positive
Predicted condition
negative
False negative,
Type II error
True negativeFalse omission rate (FOR) = Σ False negative/Σ Predicted condition negativeNegative predictive value (NPV) = Σ True negative/Σ Predicted condition negative
True positive rate (TPR), RecallSensitivity, probability of detection, Power Σ True positive/Σ Condition positiveFalse positive rate (FPR), Fall-out, probability of false alarm Σ False positive/Σ Condition negativePositive likelihood ratio (LR+) TPR/FPRDiagnostic odds ratio (DOR) LR+/LR−F1 score = 2 · Precision · Recall/Precision + Recall
False negative rate (FNR), Miss rate Σ False negative/Σ Condition positiveSpecificity (SPC), Selectivity, True negative rate (TNR) Σ True negative/Σ Condition negativeNegative likelihood ratio (LR−) FNR/TNR