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

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

    I don't know if you still need a solution or not but this is best I have done to keep it in perfect format and still save it:

    def classifcation_report_processing(model_to_report):
        tmp = list()
        for row in model_to_report.split("\n"):
            parsed_row = [x for x in row.split("  ") if len(x) > 0]
            if len(parsed_row) > 0:
                tmp.append(parsed_row)
    
        # Store in dictionary
        measures = tmp[0]
    
        D_class_data = defaultdict(dict)
        for row in tmp[1:]:
            class_label = row[0]
            for j, m in enumerate(measures):
                D_class_data[class_label][m.strip()] = float(row[j + 1].strip())
        save_report = pd.DataFrame.from_dict(D_class_data).T
        path_to_save = os.getcwd() +'/Classification_report.xlsx'
        save_report.to_excel(path_to_save, index=True)
        return save_report.head(5)
    
    
    saving_CL_report_naive_bayes = classifcation_report_processing(classification_report(y_val, prediction))
    

提交回复
热议问题