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.

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

你可能感兴趣的:(机器学习相关,sklearn,python,机器学习)