1

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.

Rob
  • 13
  • 3
  • Possibly duplicated [Area under Precision-Recall Curve (AUC of PR-curve) and Average Precision (AP)](https://stats.stackexchange.com/questions/157012/area-under-precision-recall-curve-auc-of-pr-curve-and-average-precision-ap) – msuzen Jun 23 '21 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 '21 at 00:37

1 Answers1

1

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.

msuzen
  • 1,709
  • 6
  • 27
  • You could add very small noise, let's say 1e-5 on the second value to make it monotonically increasing. – msuzen Jun 24 '21 at 08:55