Search code examples
scikit-learnclassificationmetricsprecision-recall

Why not look at the precision and recall of both classes combined in a classification report?


I was looking at the classification report from sklearn. I am wondering, why did they omit a potential third row with precision and recall values for both classes together? Why were they split apart, and what's the disadvantage to considering these metrics with both classes combined?


Solution

  • "Precision and recall values for both classes together" is contained in the classification_report as macro averages and weighted averages for precision, recall, and f1-score.

    Compare the column in classification_report to the values computed when calling precision_score(y_true, y_pred):

    from sklearn.metrics import classification_report
    from sklearn.metrics import precision_score
    
    y_true = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2]
    y_pred = [0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 0]
    
    print(classification_report(y_true, y_pred))
    print(round(precision_score(y_true, y_pred, average='macro'), 2))
    print(round(precision_score(y_true, y_pred, average='weighted'), 2))
    

    Running this results in the following. Notice that macro-averaged precision is 0.64 and weighted-average precision is 0.67, and both those are listed in the bottom rows of the table:

                  precision    recall  f1-score   support
    
               0       0.43      0.60      0.50         5
               1       0.50      0.57      0.53         7
               2       1.00      0.57      0.73         7
    
        accuracy                           0.58        19
       macro avg       0.64      0.58      0.59        19
    weighted avg       0.67      0.58      0.60        19
    
    0.64
    0.67