所以我正在使用 Jupiter Notebook 使用 Python 进行一些机器学习,并且我对 sklearnclassification_report 的输出格式有问题。有两个版本。一个是 0.18.2,另一个是 0.20.3。 20.3 版本的代码具有以下输出:
from sklearn.metrics import classification_report
final=(classification_report(y_test, predictions))
print(final)
precision recall fl-score support
Female 0.47. 0.21. 0.34. 26
Male. 0.71 0.85. 0.78. 55
micro avg 0.67. 0.67. 0.67. 81
macro avg. 0.59. 0.56 0.56. 81
weighted avg 0.63. 0.67. 0.64. 81
但是,我希望以下输出如下所示:
precision recall fl-score support
Female 0.47. 0.21. 0.34. 26
Male. 0.71 0.85. 0.78. 55
avg/total. 0.63. 0.67. 0.64. 81
以上输出为0.18.2版本的sklearn分类报告 由于某种原因,它没有与我的版本一起运行。 0.18.2 和 0.20.3 中的输出语法相同。有没有办法 在 Jupiter 笔记本中来回切换版本?任何意见,将不胜感激。
最佳答案
您可以使用classification_report
选项获取字典而不是字符串作为返回值,然后您可以根据需要进行操作。这是相关参数:
output_dict : bool (default = False) If True, return output as dict
(请参阅 here v0.20 文档)
这是一种根据您的要求更改输出的方法:
# get report as dict instead of a string
report = classification_report(y_test, predictions, output_dict=True)
# delete entries for keys "micro avg" and "macro avg" from report dict
del report["micro avg"]
del report["macro avg"]
# rename dict key "weighted avg" to "avg total"
report["avg/total"] = report.pop("weighted avg")
print(pd.DataFrame(report).transpose())
输出应如下所示(使用 v0.21.3* 测试):
precision recall fl-score support
Female 0.47 0.21 0.34 26
Male 0.71 0.85 0.78 55
avg/total 0.63 0.67 0.64 81
*在 v0.21.3 中,您需要使用 del report["accuracy"]
而不是 del report["micro avg"]
因为指标名称已更改`` `
关于python - sklearn 指标分类_报告版本输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55787854/