scikit learn output metrics.classification_report into CSV/tab-delimited format

后端 未结 17 2182
青春惊慌失措
青春惊慌失措 2021-01-31 03:08

I\'m doing a multiclass text classification in Scikit-Learn. The dataset is being trained using the Multinomial Naive Bayes classifier having hundreds of labels. Here\'s an extr

17条回答
  •  说谎
    说谎 (楼主)
    2021-01-31 03:56

    It's obviously a better idea to just output the classification report as dict:

    sklearn.metrics.classification_report(y_true, y_pred, output_dict=True)
    

    But here's a function I made to convert all classes (only classes) results to a pandas dataframe.

    def report_to_df(report):
        report = [x.split(' ') for x in report.split('\n')]
        header = ['Class Name']+[x for x in report[0] if x!='']
        values = []
        for row in report[1:-5]:
            row = [value for value in row if value!='']
            if row!=[]:
                values.append(row)
        df = pd.DataFrame(data = values, columns = header)
        return df
    

    Hope this works fine for you.

提交回复
热议问题