Skip to main content
 首页 » 编程设计

python之如何将Sklearn的分类报告输出成csv文件

2024年10月01日8lovecherry

谁知道有没有办法将分类报告输出为文本文件或 CSV 文件?

python 中的 print(metrics.classification_report(y_test, y_pred)) 行给我分类报告。我想要此报告的格式为 csv。

我尝试复制和粘贴,但这些列会混在一起!任何帮助表示赞赏!

请您参考如下方法:

该函数有一个参数可以解决这个问题。

import pandas as pd 
from sklearn.metrics import classification_report 
 
report_dict = classification_report(y_true, y_pred, output_dict=True) 
pd.DataFrame(report_dict) 

将字典转换为数据框后,您可以将其写入 csv,轻松绘制它,对其进行操作或其他任何操作。