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

后端 未结 17 2188
青春惊慌失措
青春惊慌失措 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 04:10

    I have modified @kindjacket's answer. Try this:

    import collections
    def classification_report_df(report):
        report_data = []
        lines = report.split('\n')
        del lines[-5]
        del lines[-1]
        del lines[1]
        for line in lines[1:]:
            row = collections.OrderedDict()
            row_data = line.split()
            row_data = list(filter(None, row_data))
            row['class'] = row_data[0] + " " + row_data[1]
            row['precision'] = float(row_data[2])
            row['recall'] = float(row_data[3])
            row['f1_score'] = float(row_data[4])
            row['support'] = int(row_data[5])
            report_data.append(row)
        df = pd.DataFrame.from_dict(report_data)
        df.set_index('class', inplace=True)
        return df
    

    You can just export that df to csv using pandas

提交回复
热议问题