Sklearn:在用precision_score和callback_score计算时,召回率和精度会互换吗?

问题描述 投票:0回答:2

我正在将scikit-learn(0.22.1版)用于机器学习应用程序。

我正在使用随机森林算法,并且在使用精度和召回率评估算法性能时遇到一些问题。我有测试集的标签(Y_test)和使用随机森林算法预测的标签(Y_pred)。两个数据都包含两个标签(1和0)

详细来说,我有这个矩阵:

print(confusion_matrix(y_true=Y_test, y_pred=Y_pred, labels=[1,0]))

[[78 20]
 [36 41]]

因此:

True Positive (tp) =  78
False Negative (fn) =  36
False Positive (fp) =  20

所以:

PRECISION =  tp/(tp+fn) = 78/(78+36) = 0.7959183673469388
RECALL =  = tp/(tp+fp) = 78/(78+20) 0.6842105263157895

但是,使用此代码:

precision = precision_score(Y_test, Y_pred, pos_label=1)
recall = recall_score(y_true=Y_test, y_pred=Y_pred, pos_label=1)

print("precision: ",precision)
print("recall: ",recall)

我得到以下输出:

recall:  0.7959183673469388
precision:  0.6842105263157895

似乎使用标准sklearn函数计算值时会交换它们。我做错什么了吗?拜托,你能给我一些建议吗?

谢谢,

丹妮尔

python machine-learning scikit-learn confusion-matrix precision-recall
2个回答
0
投票

您当前正在错误地计算这些值。正确的计算是;

精度计算:

precision = tp/(tp+fp)

召回计算:

recall = tp/(tp+fn)

参考:https://developers.google.com/machine-learning/crash-course/classification/precision-and-recall


0
投票

公式不正确:

Precision: tp / tp+fp
Recall : tp/tp+fn
© www.soinside.com 2019 - 2024. All rights reserved.