Python scikit学习如何在excel中导出分类报告和混淆矩阵结果?

nir*_*jan 5 machine-learning python-2.7 pandas confusion-matrix scikit-learn

如何将结果导出到excel文件中?我尝试了下面的脚本,但它没有给出正确的输出。在没有依赖标签的情况下预测列中测试数据集中存在的类不会在输出中显示。

有没有其他方法可以实现这一点。我想以 excel 格式显示模型结果。

import pandas as pd
expected = y_test
y_actu = pd.Series(expected, name='Actual')
y_pred = pd.Series(predicted, name='Predicted')
df_confusion = pd.crosstab(y_actu, y_pred,y_test.unique())

df_confusion


df_confusion.to_csv('SVM_Confusion_Matrix.csv')

from pandas import ExcelWriter
writer = ExcelWriter('SVM_Confusion_Matrix.xlsx')
df_confusion.to_excel(writer,'Sheet1')
writer.save()
Run Code Online (Sandbox Code Playgroud)

小智 0

您可以使用以下代码进行分类报告:

  1. classification_report = classification_report(y_actu, y_pred, output_dict=True)

  2. df = pandas.DataFrame(classification_report ).transpose()

  3. df.to_excel('classification_report.xlsx')