2
votes

According to the definition of the average precision (AP) score in here, the AP is calculated based on the precision and increment in recall across thresholds.

In the average_precision_score function, the mandatory parameters are as follows:
y_true: True binary labels in binary label indicators.

y_score: Target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions (as returned by "decision_function" on some classifiers).

To my understanding, y_true is a list of the ground truth classes to which each record belongs. y_score is the list of predicted classes corresponding to each record. Likewise, for a binary classification task, y_true = [1,0,0,1,1] and y_score = [0,0,0,1,0] would be feasible parameters, where for record 1, the ground truth class is positive (1) but the predicted class is negative (0).

We attempt our classification task with different thresholds. Hence we get different y_score lists for each threshold. In that case, when I apply the average_precision_score function, I would obtain an AP value per each threshold. Not a single value across thresholds as implied by the definition. In the sklearn function there is no 'threshold' parameter anyway.

Can you please tell me whether my understanding of the average_precision_score function in sklearn is correct? If so, what is the reason for the contradiction with the definition?

1

1 Answers

4
votes

You made one mistake:

Y_score is not a of the form [0,0,1,0]

from doc:

y_score : array, shape = [n_samples] or [n_samples, n_classes] Target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions (as returned by "decision_function" on some classifiers).

meaning that it is of the following form [[0.33,0.66][0.65,0.35][0.1,0.9]...]. You get the probabilities for each class. Now the threshold is the value from wich you will decide wich class you will assign to every point.

For example in the list i gave above with a Threshold of 0.4, if the proba of being 1 is superior to 0.4 then i assigne 1. With this threshold, my vector is now [1,0,1,...] but if the threshold is 0.70 then it's [0,0,1,....].

So with one prediction, you can apply different threshold ans thus compute the average_precision_score. Thre precision and recall are based on the threshold.

a good explanation was made a few days ago here: Understanding ROC curve