所以我正在用Python做一些机器学习,使用Jupiter notebook,我对sklearn classification_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 notebook中来回切换版本?任何建议都将不胜感激。
发布于 2019-11-24 03:27:36
您可以使用classification_report选项来获取字典而不是字符串作为返回值,然后可以根据需要对其进行操作。相关参数如下:
output_dict :布尔值(默认值= False)如果为True,则返回作为dict的输出
(有关v0.20文档,请参阅here )
这是根据您的需求更改输出的一种方法:
# 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"],因为指标名称已经更改了`
https://stackoverflow.com/questions/55787854
复制相似问题