sklearn.metrics import average_precision_score研究

众所周知,average_precision_score计算的是PR曲线下的面积,然而

import numpy as np
from sklearn.metrics import average_precision_score
y_true = np.array([0, 0, 1, 1,1,1])
y_scores = np.array([0, 0.5, 0.4, 1,1,1])
print (average_precision_score(y_true, y_scores)  )
# print (roc_auc_score(y_true, y_scores)  )

precision, recall, thresholds = precision_recall_curve(y_true, y_scores)

# precision, recall, thresholds = precision_recall_curve( 1 - test_y, 1 - test_pred_prob)

# precision, recall, thresholds = precision_recall_curve(test_pred_prob, test_y)

#print(precision)
#print(recall)
#print(thresholds)
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.grid()  # 生成网格

plt.plot(recall,precision)
plt.figure("P-R Curve")

plt.show()

程序算出的结果是0.95, 而严格按照定义算出来的结果是0.94375。这是为什么呢?参见

为什么平均精准度(Average Precision,AP)就是PR曲线的线下面积? - Mark Lue的回答 - 知乎 https://www.zhihu.com/question/422868156/answer/1523130474

提到 “事实上,大家使用PR图一般都会采用平滑锯齿操作”。所以0.95 = 0.75  + 0.8 * 0.25 /2。

此外,sklearn.metrics.average_precision_score — scikit-learn 1.0.1 documentation 

也提到, 
This implementation is not interpolated and is different from computing the area under the precision-recall curve with the trapezoidal rule, which uses linear interpolation and can be too optimistic.

总之没用使用插值法,就是用了前面的锯齿法。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值