【问题标题】:Precision, recall and confusion matrix problems in sklearnsklearn 中的精确率、召回率和混淆矩阵问题
【发布时间】:2019-10-08 18:25:09
【问题描述】:

我在 keras 中训练了一个模型,并做出了一些预测。为了评估我的模型的性能,我使用 sklearn 库计算了精度和召回分数以及混淆矩阵。这是我的代码:

final_predictions = model.predict_generator(generator_test, steps=steps_per_epoch_test)
rounded_pred = [0 if x<=0.5 else 1 for x in final_predictions]
test_precision_score = round(precision_score(y_test, rounded_pred), 3)
test_recall_score = round(recall_score(y_test, rounded_pred), 3)
test_confusion_matrix = confusion_matrix(y_test, rounded_pred)

这些是我的结果:

Test confusion matrix :

 [[1555   13]

 [   9   49]]

Precision and recall:

Test Precision :: 0.845
Test Recall :: 0.79

有人知道为什么精度分数计算不正确吗? (应该是(1555/(1555+13) 而不是(13/(13+49))

【问题讨论】:

  • 你有没有试过切换confusion_matrix()的参数。在我看来,准确率和召回率是用 49 被视为真阳性而不是真阴性来计算的。

标签: scikit-learn neural-network artificial-intelligence metrics


【解决方案1】:

precision_scorerecall_score 的默认pos_label1

from sklearn.metrics import confusion_matrix,precision_score,recall_score,classification_report

y_true = [0]*1568 + [1]*58
y_pred = [0]*1555 + [1]*13 + [0]* 9+ [1]* 49

print('confusion matrix :\n',confusion_matrix(y_true,y_pred))
print('precision_score :\n',precision_score(y_true,y_pred,pos_label=1))
print('recall_score :\n',recall_score(y_true,y_pred,pos_label=1))
print('classification_report :\n',classification_report(y_true,y_pred))

confusion matrix :
 [[1555   13]
 [   9   49]]
precision_score :
 0.7903225806451613
recall_score :
 0.8448275862068966
classification_report :
               precision    recall  f1-score   support

           0       0.99      0.99      0.99      1568
           1       0.79      0.84      0.82        58

   micro avg       0.99      0.99      0.99      1626
   macro avg       0.89      0.92      0.90      1626
weighted avg       0.99      0.99      0.99      1626

如果你想得到precision_scorerecall_scorelabel=1。可以设置pos_label=0设置类。

print('precision_score :\n',precision_score(y_true,y_pred,pos_label=0))
print('recall_score :\n',recall_score(y_true,y_pred,pos_label=0))

precision_score :
 0.9942455242966752
recall_score :
 0.9917091836734694

【讨论】:

  • 感谢您的回复,但设置python label=0 我得到的召回分数结果不正确。我得到 0.991,应该是 0.844。
  • @Negreanu 0.8448label=1 的召回分数。您可以在答案中看到classification_report
  • 哦,是的!谢谢!
  • 不是“如果你想得到label=0的precision_score和recall_score。你可以设置pos_Label=0来设置class”?而不是标签=1?我很困惑。然而,代码 sn-ps 是准确的。
猜你喜欢
  • 2017-04-05
  • 1970-01-01
  • 1970-01-01
  • 2022-12-01
  • 2021-10-07
  • 2020-08-29
  • 2013-04-09
  • 2022-01-16
  • 2018-07-04
相关资源
最近更新 更多