简体   繁体   English

Sklearn:使用precision_score 和recall_score 计算时召回率和精度是否交换?

[英]Sklearn: are recall and precision swapped when are computed with precision_score and recall_score?

I'm using scikit-learn (version 0.22.1) for a machine learning application.我将 scikit-learn(0.22.1 版)用于机器学习应用程序。

I'm using a Random Forest algorithm and I have some problems in evaluating the performance of the algorithm using precision and recall.我正在使用随机森林算法,并且在使用精度和召回率评估算法的性能时遇到了一些问题。 I have the labels of my test set (Y_test) and the labels predicted using the Random Forest algorithm (Y_pred).我有我的测试集 (Y_test) 的标签和使用随机森林算法 (Y_pred) 预测的标签。 Both data contains two labels (1 and 0)两个数据都包含两个标签(1 和 0)

In detail, I have this matrix:详细地说,我有这个矩阵:

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

[[78 20]
 [36 41]]

Consequently:最后:

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

So:所以:

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

However, using this code:但是,使用此代码:

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)

I get the following output:我得到以下输出:

recall:  0.7959183673469388
precision:  0.6842105263157895

It seems that the values are swapped when they are computed using the standard sklearn functions.当使用标准 sklearn 函数计算这些值时,这些值似乎被交换了。 Did I do something wrong?我做错什么了吗? Please, can you give me some advice?拜托,你能给我一些建议吗?

Thanks,谢谢,

Daniele丹尼尔

You are currently calculating those values wrong.您当前计算这些值是错误的。 The correct calculations are;正确的计算是;

Precision Calculation:精度计算:

precision = tp/(tp+fp)

Recall Calculation:召回计算:

recall = tp/(tp+fn)

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

Formula is incorrect:公式不正确:

Precision: tp / tp+fp
Recall : tp/tp+fn

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM