python - 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 函数计算这些值时交换了这些值。我做错什么了吗?拜托,你能给我一些建议吗?
谢谢,
丹尼尔
解决方案
您当前计算这些值是错误的。正确的计算是;
精密计算:
precision = tp/(tp+fp)
召回计算:
recall = tp/(tp+fn)
参考:https ://developers.google.com/machine-learning/crash-course/classification/precision-and-recall
推荐阅读
- python - 改进算法输入验证
- regex - 如何从多个文本文件中提取某些特定单词
- javascript - 如何在 Typescript 中使用绝对路径?
- c++ - 如何将输入存储在动态数组中?
- java - 为什么 when().thenReturn 总是返回 NullPointerExeption?
- php - call_user_func_array 和使用参数解包/三个点(...$array)的调用有什么区别?
- jquery - jQuery ui 滑块 - div 外的 ui 滑块句柄
- c - 总是在 mmap 中为常规文件添加 MAP_NORESERVE 标志?
- excel - Sumproduct 减去不同月份列中最早月份的总金额
- java - 带有客户端证书返回访问的 https 请求在 android 中被拒绝,但它在 post man 中运行良好,改造和 okhttp3 返回相同的错误