access to numbers in classification_report - sklearn
Asked Answered
J

5

25

This is a simple example of a classification_report in sklearn:

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))
#             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

I want to have access to avg/total row. For instance, I want to extract the f1-score from the report, which is 0.61.

How can I have access to the number in classification_report?

Joy answered 24/1, 2018 at 8:27 Comment(2)
are you interested in the f1-score or extracting f1-score from classification report?Lannie
@PratikKumar extracting from classification report. I need other reports also.Joy
N
26

You can output the classification report by adding output_dict=True to the report:

report = classification_report(y_true, y_pred, output_dict=True)

And then access its single values as in a normal python dictionary.

For example, the macro metrics:

macro_precision =  report['macro avg']['precision'] 
macro_recall = report['macro avg']['recall']    
macro_f1 = report['macro avg']['f1-score']

or Accuracy:

accuracy = report['accuracy']
Nonmetallic answered 17/6, 2019 at 14:32 Comment(0)
L
21

you can use precision_recall_fscore_support for getting all at once

from sklearn.metrics import precision_recall_fscore_support as score
y_true = [0, 1, 2, 2, 2]
y_pred = [0, 0, 2, 2, 1]
precision,recall,fscore,support=score(y_true,y_pred,average='macro')
print 'Precision : {}'.format(precision)
print 'Recall    : {}'.format(recall)
print 'F-score   : {}'.format(fscore)
print 'Support   : {}'.format(support)

here is the link to the module

Lannie answered 24/1, 2018 at 8:47 Comment(2)
The answer is correct, but please note that you have used the wrong parameters, since the first parameter is y_true, the second one should be y_pred.Creath
Does this also work for multi-class datasets?Holloweyed
I
9

You can use output_dict parameter in build-in classification_report to return a dictionary:

classification_report(y_true,y_pred,output_dict=True)

Impinge answered 4/3, 2019 at 18:5 Comment(0)
W
5

classification_report is string so I would suggest you to use f1_score from scikit-learn

from sklearn.metrics import f1_score
y_true = [0, 1, 2, 2, 2]
y_pred = [0, 0, 2, 2, 1]
target_names = ['class 0', 'class 1', 'class 2']

print(f1_score(y_true, y_pred, average=None)

output

Walliw answered 24/1, 2018 at 8:36 Comment(3)
Thank you. so there is no way to extract from classification_report? what about the other reports?Joy
maybe you can use regex to extract this value. can you name the other reports ?Walliw
If you are talking about recall and precision, yes there are functions like recall_score and precision_score in sklearnWalliw
D
1

You have to set the parameter output_dict to True it's not mentioned in the documentation but it is in repo L2636

Example

report = classification_report(y_true, y_pred, 
                               output_dict=True) #<== Right here

This will output the dict with keys report.keys() the output is;

dict_keys(['0', '1', '2', 'accuracy', 'macro avg', 'weighted avg'])
Dowel answered 15/9, 2023 at 4:55 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.