Confusion matrix


In the field of machine learning and specifically the problem of statistical classification, a confusion matrix, also known as an error matrix, is a specific table layout that allows visualization of the performance of an algorithm, typically a supervised learning one. Each row of the matrix represents the instances in a predicted class while each column represents the instances in an actual class. The name stems from the fact that it makes it easy to see if the system is confusing two classes.
It is a special kind of contingency table, with two dimensions, and identical sets of "classes" in both dimensions.

Example

Given a sample of 13 labeled animals — 8 cats and 5 dogs where Cats belong to class=1 & Dogs belong to class=0.
actual =
Now assuming we had previously trained a classifier that distinguish between Cats and Dogs. Now assuming we took the 13 samples and run them through the classifier and the classifier made 8 accurate predictions and missed 5: 3 Cats wrongly predicted as Dogs and 2 Dogs wrongly predicted as Cats.
prediction =
With these two label sets we can create a confusion matrix that will summarize the results of testing the classifier for further inspection. The resulting confusion matrix looks like the table below:
In this confusion matrix, of the 8 actual cats, the system predicted that 3 were dogs, and of the 5 dogs, it predicted that 2 were cats. All correct predictions are located in the diagonal of the table, 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:
where: P = positive; N = Negative; TP = True Positive; FP = False Positive; TN = True Negative; FN = False Negative.

Table of confusion

In predictive analytics, a table of confusion, is a table with two rows and two columns that reports the number of false positives, false negatives, true positives, and true negatives. This allows more detailed analysis than mere proportion of correct classifications. 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 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.
According to Davide Chicco and Giuseppe Jurman, the most informative metric to evaluate a confusion matrix is the Matthews correlation coefficient.
Assuming the confusion matrix above, its corresponding table of confusion, for the cat class, would be:
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: