Search code examples
pythonscikit-learnconfusion-matrix

How can I extract TP, FN, FP from classification_report


I know I can output the contents of the sklearn classification_report as a dict and extract micro/macro averaged scores along with scores for individual labels.

How can I get the counts for the associated confusion matrix inputs, specifically, TP, FN, and FP that go into the computations for the macro averaged measures? I have my vectors for y_pred and y_true, so I assume there is a way to get the desired parameters I want.

This is for a binary classification problem, but I will also need it for a multiclass task, as well.


Solution

  • Got it: tn, fp, fn, tp = confusion_matrix([0, 1, 0, 1], [1, 1, 1, 0]).ravel()

    and sklearn.metrics.multilabel_confusion_matrix