简体   繁体   English

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

[英]sklearn metrics classification_report version output

So I am doing some machine learning with Python using Jupiter notebook and I have a problem with the output format with sklearn classification_report.所以我正在使用 Jupiter notebook 使用 Python 进行一些机器学习,并且我对 sklearn 分类报告的输出格式有问题。 There are two version.有两个版本。 One which is 0.18.2 and the other is 0.20.3.一个是 0.18.2,另一个是 0.20.3。 The 20.3 version has the following output with my code: 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          

however, I want the following output to be like this:但是,我希望以下输出是这样的:

              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  

The above output is the 0.18.2 version of sklearn classification report which is not running with my version for some reason.上面的输出是 0.18.2 版本的 sklearn 分类报告,由于某种原因没有在我的版本上运行。 The syntax for output is the same in both 0.18.2 and 0.20.3.输出的语法在 0.18.2 和 0.20.3 中是相同的。 Is there a way to switch versions back and forth in Jupiter notebook?有没有办法在 Jupiter notebook 中来回切换版本? Any advice would be appreciated.任何意见,将不胜感激。

You can use the option of classification_report to get a dictionary instead of a string as return value which you can then manipulate according to your needs.您可以使用classification_report选项来获取字典而不是字符串作为返回值,然后您可以根据需要对其进行操作。 This is the relevant parameter:这是相关参数:

output_dict : bool (default = False) If True, return output as dict output_dict : bool (default = False) 如果为 True,则将输出作为 dict 返回

(see here for v0.20 documentation) (有关 v0.20 文档,请参见此处

And this one way to then change the output to your requirements:这是一种将输出更改为您的要求的方法:

# 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())

the output should look like this (tested with v0.21.3*):输出应如下所示(使用 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 

*in v0.21.3 you need to use del report["accuracy"] instead of del report["micro avg"] since metric names have changed``` *在 v0.21.3 中,您需要使用del report["accuracy"]而不是del report["micro avg"]因为度量名称已更改```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM