How to get support in sklearn.metrics.classification_report

1

I am using sklearn.metrics.classification_report and I would like to isolate the support values to work with them separately, but I do not know how to extract them. I made the call with print(classification_report(y_true, y_pred, target_names=target_names)).

Logistic regression using RBM features:
         precision    recall  f1-score   support

      0       0.99      0.99      0.99       174
      1       0.92      0.95      0.93       184
      2       0.95      0.98      0.97       166
      3       0.97      0.91      0.94       194
      4       0.97      0.95      0.96       186
      5       0.93      0.93      0.93       181
      6       0.98      0.97      0.97       207
      7       0.95      1.00      0.97       154
      8       0.90      0.88      0.89       182
      9       0.91      0.93      0.92       169

avg / total       0.95      0.95      0.95      1797

Can someone help me?

    
asked by Carla 23.05.2017 в 22:56
source

1 answer

0

The sklearn.metrics.classification_report output is a string. Let's start with some data as a basis to reproduce the problem:

from sklearn.metrics import classification_report

y_true = [0, 1, 2, 2, 2]
y_pred = [0, 0, 2, 2, 1]
target_names = ['class 0', 'class 1', 'class 2']
print(classification_report(y_true, y_pred, target_names=target_names))

Exit:

             precision    recall  f1-score   support

    class 0       0.50      1.00      0.67         1
    class 1       0.00      0.00      0.00         1
    class 2       1.00      0.67      0.80         3

avg / total       0.70      0.60      0.61         5

You have several options, including:

  • Use sklearn.metrics.precision_recall_fscore_support that returns NumPy arrays that we can get through Indexing:

    from sklearn.metrics import precision_recall_fscore_support
    
    y_true = [0, 1, 2, 2, 2]
    y_pred = [0, 0, 2, 2, 1]
    target_names = ['class 0', 'class 1', 'class 2']
    
    res = precision_recall_fscore_support(y_true, y_pred)
    support = res[3]
    print(support)
    

    Exit:

      

    [1, 1, 3]

    It is an array of NumPy of type int64

  • Parse the output string of sklearn.metrics.classification_report appropriately:

    from sklearn.metrics import classification_report
    
    y_true = [0, 1, 2, 2, 2]
    y_pred = [0, 0, 2, 2, 1]
    target_names = ['class 0', 'class 1', 'class 2']
    
    res = classification_report(y_true, y_pred, target_names=target_names)
    support = [int(fila.split()[-1]) for fila in res.split('\n')[2:-3]]
    print(support)
    

    Exit:

      

    [1, 1, 3]

    This is a list of Python but you can easily switch to the NumPy array if you prefer.

answered by 24.05.2017 / 05:19
source