Skip to content

Instantly share code, notes, and snippets.

@wassname
Last active March 10, 2017 04:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wassname/b7a99112b8f77004df85f535df95e550 to your computer and use it in GitHub Desktop.
Save wassname/b7a99112b8f77004df85f535df95e550 to your computer and use it in GitHub Desktop.
Digitize scikit-learn's classification_report
from io import StringIO
import pandas as pd
import numpy as np
import sklearn
def parse_classification_report(classification_report):
"""Parses sklearn classification report into a pandas dataframe."""
return pd.read_fwf(StringIO(classification_report),lineterminator='\n', index_col=0, colspecs=[(0,12),(12,22),(22,32),(32,42),(42,52)]).dropna()
target_names['leak','no leak']
y_pred = np.random.random(100)>0.5
y_true = np.random.random(100)>0.5
s = sklearn.metrics.classification_report(y_true,y_pred,target_names=target_names)
parse_classification_report(s).to_dict()
"""
{'f1-score': {'avg / total': 0.41999999999999998,
'leak': 0.40000000000000002,
'no leak': 0.44},
'precision': {'avg / total': 0.42999999999999999,
'leak': 0.35999999999999999,
'no leak': 0.48999999999999999},
'recall': {'avg / total': 0.41999999999999998,
'leak': 0.44,
'no leak': 0.40000000000000002},
'support': {'avg / total': 100.0, 'leak': 43.0, 'no leak': 57.0}}
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment