I'm using Python and have some confusion matrixes. I'd like to calculate precisions and recalls and f-measure by confusion matrixes in multiclass classification. My result logs don't contain y_true
and y_pred
, just contain confusion matrix.
Could you tell me how to get these scores from confusion matrix in multiclass classification?
Let's consider the case of MNIST data classification (10 classes), where for a test set of 10,000 samples we get the following confusion matrix
cm
(Numpy array):In order to get the precision & recall (per class), we need to compute the TP, FP, and FN per class. We don't need TN, but we will compute it, too, as it will help us for our sanity check.
The True Positives are simply the diagonal elements:
The False Positives are the sum of the respective column, minus the diagonal element (i.e. the TP element):
Similarly, the False Negatives are the sum of the respective row, minus the diagonal (i.e. TP) element:
Now, the True Negatives are a little trickier; let's first think what exactly a True Negative means, with respect to, say class
0
: it means all the samples that have been correctly identified as not being0
. So, essentially what we should do is remove the corresponding row & column from the confusion matrix, and then sum up all the remaining elements:Let's make a sanity check: for each class, the sum of TP, FP, FN, and TN must be equal to the size of our test set (here 10,000): let's confirm that this is indeed the case:
The result is
Having calculated these quantities, it is now straightforward to get the precision & recall per class:
which for this example are
You should now be able to compute these quantities virtually for any size of your confusion matrix.
If you have confusion matrix in the form of:
Following simple function can be made:
Testing:
Output:
Above function can also be extended to produce other scores, the formulae for which are mentioned on https://en.wikipedia.org/wiki/Confusion_matrix