# Get AP score with Precision and Recall values

To display a Precision-Recall curve, I calculated my values of Recalls and Precision by varying the confidence threshold from 0 to 1. The PR curve is right but I don't understand exactly how to calculate the AP score, the area under my curve. Based on the scikit learn formula here I did :

precision = [0.9117647058823529, 0.9117647058823529, 0.9090909090909091, ..]
recall = [0.32978723404255317, 0.34065934065934067, 0.3448275862068966, ..]

AP = sum([(recall[i]-recall[i-1])*precision[i] for i in range(len(precision)-1)])
print(AP)


But the value of my AP score is not correct. Of course sizes of the recall and precision lists are identical.

Why is my formula wrong and How do I calculate the AP score? Thanks for your attention.

• Jun 23, 2021 at 17:53
• Thanks but not exactly. I just want to know what is the formula in python to calculate the AP score according to the precision and recall.
– Rob
Jun 24, 2021 at 0:37

Formula looks fine as a naive integration $$AP={\large \Sigma}_{i} p(i) \Delta r$$. auc function from sklearn gives a similar result.

import numpy as np
import sklearn
np.random.seed(42)
precision = np.linspace(0.0, 1.0, 100)
recall = np.linspace(0.0, 1.0, 100)
from sklearn.metrics import auc
AP = sum([(recall[i]-recall[i-1])*precision[i] for i in range(len(precision)-1)])
AP, auc(precision, recall) # (0.4949494949494951, 0.5)


Please note average_precision_score from sklearn requires scores and class labels, not precision and recall values, so we can't feed precision and recall vectors into that function directly.

• You could add very small noise, let's say 1e-5 on the second value to make it monotonically increasing. Jun 24, 2021 at 8:55