如何获得精确召回曲线下的面积

How to get the area under precision-recall curve

我正在打印分类报告。我正在使用的代码是打印 ROC 曲线的 AUC 值,而不是精确召回曲线(它只绘制图形)。如何获取精确召回曲线的AUC值?

df_test = pd.read_csv("D:/a.csv")
df_testPred = pd.read_csv("D:/b.csv")     
y_true1 = df_test["anomaly"].values[:-1]
y_score1 = df_testPred["anomaly_scores"].values[:-1]
y_pred1 = df_testPred["anomaly"].values[:-1].astype(int)     
ap1 = average_precision_score(y_true1, y_score1)
auc1 = roc_auc_score(y_true1, y_score1)
print(f"ap: {ap1}")
print(f"AUC: {auc1}")

print(classification_report(y_true1, y_pred1))

precision1, recall1, thresholds1 = precision_recall_curve(y_true1, y_score1)
#plt.plot([0, 1], [0, 1],'r--')
plt.plot(recall1, precision1)

因为你已经计算了precision1recall1,你可以简单地使用相关的scikit-learn函数auc(docs):

from sklearn.metrics import auc
auc_score = auc(recall1, precision1)

参见ROC Curves and Precision-Recall Curves for Imbalanced Classification(尽管根据我的经验,与更常见的 ROC AUC 相比,precision-recall AUC 的使用并不广泛)。