python - sklearn 指标分类_报告版本输出

标签 python machine-learning

所以我正在使用 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/

相关文章:

python - 如何在 scikit-learn 中使用 SGDRegressor

machine-learning - 梯度下降似乎失败了

python - 一次在 Pandas 中重新编码多个变量

python - Tensorflow/Keras : Model accuracy during training is always 0. 5 输入大小与第一个官方教程不同

python - 如何使用 django 模板渲染树结构(递归)?

python - 训练集的不同 MSE 结果

machine-learning - 如何在内存和批处理方面使用大数据集进行多标签图像分类

python - 每个应用程序实例的单独记录器名称

python - 使用唯一键返回多行?

machine-learning - 使用 keras 示例 pretrained_word_embeddings 时出错