【问题标题】:What is the relationship between classification_report and precision_score?classification_report 和precision_score 有什么关系?
【发布时间】:2022-01-03 10:41:17
【问题描述】:

我想使用classification_reportaccuracy_scoreprecision_scorerecall_scoref1_score评估指数来评估我的机器学习模型。

classification_report有正常输出但是我的precision_score报错了。

from sklearn.metrics import accuracy_score  
from sklearn.metrics import precision_score   
from sklearn.metrics import recall_score      
from sklearn.metrics import f1_score         

print(accuracy_score(y_test,predicted))
print(precision_score(y_test,predicted))
print(recall_score(y_test,predicted))
print(f1_score(y_test,predicted))
ValueError: pos_label=1 is not a valid label. It should be one of ['ham', 'spam']

分类报告:

from sklearn.metrics import classification_report
model_report_test_correct=classification_report(y_test,predicted)
print(model_report_test_correct)

              precision    recall  f1-score   support

     ham       0.96      1.00      0.98      1208
    spam       1.00      0.74      0.85       185

accuracy                           0.96      1393
macro avg      0.98      0.87      0.91      1393
weighted avg   0.97      0.96      0.96      1393

【问题讨论】:

  • classification_report 有显示标签的选项,而其他 sklearn.metrics 不显示标签。它需要接收 1 和 0 作为标签。

标签: python scikit-learn classification


【解决方案1】:

错误信息非常明确:您需要将pos_label 指定为“ham”或“spam”(精度应分别为 1.00 或 0.96)。

(准确度不会出错,因为它的定义不关心哪个是“正”类,但召回和 f1 也需要指定。)

【讨论】:

    猜你喜欢
    • 2016-09-28
    • 2013-07-02
    • 2017-02-06
    • 2020-03-05
    • 2012-09-27
    • 2020-04-17
    • 2016-03-21
    • 2015-07-08
    • 2018-12-31
    相关资源
    最近更新 更多