Additionally to sera's answer, I find the following way helpful - without having to parse the string of classification report using precision_recall_fscore_support:
from sklearn.metrics import precision_recall_fscore_support
from sklearn.utils.multiclass import unique_labels
def classification_report_to_csv_pandas_way(ground_truth,
predictions,
full_path="test_pandas.csv"):
"""
Saves the classification report to csv using the pandas module.
:param ground_truth: list: the true labels
:param predictions: list: the predicted labels
:param full_path: string: the path to the file.csv where results will be saved
:return: None
"""
import pandas as pd
# get unique labels / classes
# - assuming all labels are in the sample at least once
labels = unique_labels(ground_truth, predictions)
# get results
precision, recall, f_score, support = precision_recall_fscore_support(ground_truth,
predictions,
labels=labels,
average=None)
# a pandas way:
results_pd = pd.DataFrame({"class": labels,
"precision": precision,
"recall": recall,
"f_score": f_score,
"support": support
})
results_pd.to_csv(full_path, index=False)
def classification_report_to_csv(ground_truth,
predictions,
full_path="test_simple.csv"):
"""
Saves the classification report to csv.
:param ground_truth: list: the true labels
:param predictions: list: the predicted labels
:param full_path: string: the path to the file.csv where results will be saved
:return: None
"""
# get unique labels / classes
# - assuming all labels are in the sample at least once
labels = unique_labels(ground_truth, predictions)
# get results
precision, recall, f_score, support = precision_recall_fscore_support(ground_truth,
predictions,
labels=labels,
average=None)
# or a non-pandas way:
with open(full_path) as fp:
for line in zip(labels, precision, recall, f_score, support):
fp.write(",".join(line))
if __name__ == '__main__':
# dummy data
ground_truth = [1, 1, 4, 1, 3, 1, 4]
prediction = [1, 1, 3, 4, 3, 1, 1]
# test
classification_report_to_csv(ground_truth, prediction)
classification_report_to_csv_pandas_way(ground_truth, prediction)
outputs in either case:
class,f_score,precision,recall,support
1,0.75,0.75,0.75,4
3,0.666666666667,0.5,1.0,1
4,0.0,0.0,0.0,2