1
$\begingroup$

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.

$\endgroup$
2

1 Answer 1

1
$\begingroup$

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.