有人知道有没有输出分类报告作为文本文件或CSV文件?这行" print(metrics.classification_report(y_test,y_pred))"在python中给我分类报告。我希望以csv格式提供此报告。我试图复制和粘贴,但列将集中在一起!任何帮助表示赞赏!
答案 0 :(得分:1)
这是可能的,但您需要创建一个函数。
让我们说我想将报告写入我的report.csv文件(这需要在运行代码之前创建)
完整示例:
from sklearn.metrics import classification_report
import csv
import pandas as pd
y_true = [0, 1, 2, 2, 2]
y_pred = [0, 0, 2, 2, 1]
target_names = ['class 0', 'class 1', 'class 2']
def classifaction_report_csv(report):
report_data = []
lines = report.split('\n')
for line in lines[2:-3]:
row = {}
row_data = line.split(' ')
row['class'] = row_data[0]
row['precision'] = float(row_data[1])
row['recall'] = float(row_data[2])
row['f1_score'] = float(row_data[3])
row['support'] = float(row_data[4])
report_data.append(row)
dataframe = pd.DataFrame.from_dict(report_data)
dataframe.to_csv('report.csv', index = False)
#call the classification_report first and then our new function
report = classification_report(y_true, y_pred, target_names=target_names)
classifaction_report_csv(report)
希望这会有所帮助。 打开csv文件,然后看:
截图:
答案 1 :(得分:1)
除了血清的回答,我发现以下方法很有用 - 无需使用precision_recall_fscore_support解析分类报告字符串:
from sklearn.metrics import precision_recall_fscore_support
from sklearn.utils.multiclass import unique_labels
def classification_report_to_csv_pandas_way(ground_truth,
predictions,
full_path="test_pandas.csv"):
"""
Saves the classification report to csv using the pandas module.
:param ground_truth: list: the true labels
:param predictions: list: the predicted labels
:param full_path: string: the path to the file.csv where results will be saved
:return: None
"""
import pandas as pd
# get unique labels / classes
# - assuming all labels are in the sample at least once
labels = unique_labels(ground_truth, predictions)
# get results
precision, recall, f_score, support = precision_recall_fscore_support(ground_truth,
predictions,
labels=labels,
average=None)
# a pandas way:
results_pd = pd.DataFrame({"class": labels,
"precision": precision,
"recall": recall,
"f_score": f_score,
"support": support
})
results_pd.to_csv(full_path, index=False)
def classification_report_to_csv(ground_truth,
predictions,
full_path="test_simple.csv"):
"""
Saves the classification report to csv.
:param ground_truth: list: the true labels
:param predictions: list: the predicted labels
:param full_path: string: the path to the file.csv where results will be saved
:return: None
"""
# get unique labels / classes
# - assuming all labels are in the sample at least once
labels = unique_labels(ground_truth, predictions)
# get results
precision, recall, f_score, support = precision_recall_fscore_support(ground_truth,
predictions,
labels=labels,
average=None)
# or a non-pandas way:
with open(full_path) as fp:
for line in zip(labels, precision, recall, f_score, support):
fp.write(",".join(line))
if __name__ == '__main__':
# dummy data
ground_truth = [1, 1, 4, 1, 3, 1, 4]
prediction = [1, 1, 3, 4, 3, 1, 1]
# test
classification_report_to_csv(ground_truth, prediction)
classification_report_to_csv_pandas_way(ground_truth, prediction)
两种情况下的输出:
class,f_score,precision,recall,support
1,0.75,0.75,0.75,4
3,0.666666666667,0.5,1.0,1
4,0.0,0.0,0.0,2
答案 2 :(得分:0)
该函数具有一个可以解决此确切问题的参数。
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,轻松绘制,对其进行操作或执行其他操作。
答案 3 :(得分:0)
我发现Rabeez Riaz解决方案要容易得多。我想补充一点,您可以使用report_dict作为参数转置到数据框。
df = pandas.DataFrame(report_dict).transpose()
从这里开始,您可以自由使用标准的pandas方法来生成所需的输出格式(CSV,HTML,LaTeX等)。您想要的输出格式(CSV,HTML,LaTeX等)。
答案 4 :(得分:0)
要有一个类似于分类报告输出的csv,你可以这样使用:
labels = list(set(targcol))
report_dict = classification_report(targcol, predcol, output_dict=True)
repdf = pd.DataFrame(report_dict).round(2).transpose()
repdf.insert(loc=0, column='class', value=labels + ["accuracy", "macro avg", "weighted avg"])
repdf.to_csv("results.csv", index=False)