How do I calculate precision, recall, specificity, sensitivity manually?

I have actual class labels and predicted class labels:

2 , 1
0 , 0
2 , 1
2 , 1
2 , 1
1 , 1
2 , 1
1 , 1
0 , 0
2 , 1
2 , 1
2 , 1
2 , 1
3 , 1
2 , 1
2 , 1
2 , 1
2 , 1
2 , 1
1 , 1
2 , 1
2 , 1
1 , 1
1 , 1
0 , 0
1 , 1
1 , 1
2 , 1
2 , 1
1 , 1
2 , 1
1 , 1
0 , 0
2 , 1
1 , 1
0 , 0
2 , 1
2 , 1
0 , 0
2 , 1
2 , 1
2 , 1

I am using a scikit-learn function to generate the confusion matrix and get the accuracy:

print(classification_report(actual_label, pred_res))

Which yields:

              precision    recall  f1-score   support

           0       1.00      1.00      1.00         6
           1       1.00      0.28      0.43        36
           2       0.00      0.00      0.00         0
           3       0.00      0.00      0.00         0

    accuracy                           0.38        42
   macro avg       0.50      0.32      0.36        42
weighted avg       1.00      0.38      0.52        4

Is there any other way to calculate the precision, recall, sensitivity, and specificity, without using the above function?

Topic confusion-matrix scikit-learn classification machine-learning

Category Data Science


There are many ways to do this. For example, you could use pandas to cross-tabulate the label values. Note that, judging by your output, the true labels are actually the second column in your table.

import pandas as pd

df = pd.read_csv('labels.csv', header=None)
df.columns = ['predicted', 'actual']

print(pd.crosstab(df.actual, df.predicted))
predicted  0   1   2  3
actual                 
0          6   0   0  0
1          0  10  25  1

From this table, you can calculate all the metrics by hand, according to their definitions. For example, the recall (a.k.a sensitivity) for the labels that are actually 1 is $$ \frac{10}{10 + 25 + 1} \approx 0.28, $$ in agreement with the scikit-learn output.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.