Skip to content

Repository containing script that converts a sklearn classification report into an easy-to-read Excel file(s).

Notifications You must be signed in to change notification settings

seanswyi/sklearn-cls-report2excel

Repository files navigation

sklearn-cls-report2excel

Repository containing script that converts the classification report from sklearn.metrics.classification_report to a formatted Excel spreadsheet.

Usage

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.

Using convert_report2excel as a Function

Some things to keep in mind:

  1. Set output_dict=True in classification_report and feed the dictionary to the function.
  2. Instantiate a openpyxl.Workbook object first.
  3. 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")

Using convert_report2excel as a Script

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:

Results

About

Repository containing script that converts a sklearn classification report into an easy-to-read Excel file(s).

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages