Repository containing script that converts the classification report from sklearn.metrics.classification_report
to a formatted Excel spreadsheet.
You can either use the function convert_report2excel
inside a separate script so you're saving your formatted files as you go, or you can run convert_report2excel.py
as a script itself and provide the directory containing the report files as an argument.
Some things to keep in mind:
- Set
output_dict=True
inclassification_report
and feed the dictionary to the function. - Instantiate a
openpyxl.Workbook
object first. - If you don't want the default sheet, delete it.
import numpy as np
from openpyxl import Workbook
import pandas as pd
from sklearn.metrics import classification_report
from convert_report2excel import convert_report2excel
workbook = Workbook()
workbook.remove(workbook.active) # Delete default sheet.
y_true = np.array(['cat', 'dog', 'pig', 'cat', 'dog', 'pig'])
y_pred = np.array(['cat', 'pig', 'dog', 'cat', 'cat', 'dog'])
report = classification_report(
y_true,
y_pred,
digits=4,
zero_division=0,
output_dict=True
)
workbook = convert_report2excel(
workbook=workbook,
report=report,
sheet_name="animal_report"
)
workbook.save("animal_report.xlsx")
If you don't specify the --save_dir
argument, the results will be saved automatically to --report_dir
or --report_filename
's parent directory.
python convert_report2excel.py --report_dir $PATH_TO_REPORTS
python convert_report2excel.py --report_filename $PATH_TO_SINGLE_REPORT
The final results should look as follows: