access to numbers in classification_report - sklearn

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


You can output the classification report as dict with:

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']

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


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

classification_report(y_true,y_pred,output_dict=True)