classification_report和precision_score有什么关系?

What is the relationship between classification_report and precision_score?

我想使用 classification_report, accuracy_score, precision_score, recall_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

错误消息解释得很清楚:您需要将 pos_label 指定为“ham”或“spam”(精度应分别为 1.00 或 0.96)。

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