Last active
March 10, 2017 04:25
-
-
Save wassname/b7a99112b8f77004df85f535df95e550 to your computer and use it in GitHub Desktop.
Digitize scikit-learn's classification_report
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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