首页 > 解决方案 > Sklearn:使用precision_score和recall_score计算时是否交换了召回和精度?

问题描述

我正在将 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 函数计算这些值时交换了这些值。我做错什么了吗?拜托,你能给我一些建议吗?

谢谢,

丹尼尔

标签: pythonmachine-learningscikit-learnconfusion-matrixprecision-recall

解决方案


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

精密计算:

precision = tp/(tp+fp)

召回计算:

recall = tp/(tp+fn)

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


推荐阅读