Skip to content

Implemented step-function interpolation for average_precision_score #6065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions sklearn/metrics/ranking.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ def average_precision_score(y_true, y_score, average="macro",
"""Compute average precision (AP) from prediction scores

This score corresponds to the area under the precision-recall curve.
Interpolation between operating points is by horizontal sections.
For all recall values r not corresponding to an operating point, the
precision p is equal to the precision p' at the first operating point
(r', p') such that r' > r.

Note: this implementation is restricted to the binary classification task
or multilabel classification task.
Expand Down Expand Up @@ -166,13 +170,14 @@ def average_precision_score(y_true, y_score, average="macro",
>>> y_true = np.array([0, 0, 1, 1])
>>> y_scores = np.array([0.1, 0.4, 0.35, 0.8])
>>> average_precision_score(y_true, y_scores) # doctest: +ELLIPSIS
0.79...
0.83...

"""
def _binary_average_precision(y_true, y_score, sample_weight=None):
precision, recall, thresholds = precision_recall_curve(
y_true, y_score, sample_weight=sample_weight)
return auc(recall, precision)
return sum([(recall[i] - recall[i+1]) * precision[i]
for i in xrange(len(recall) - 1)])

return _average_binary_score(_binary_average_precision, y_true, y_score,
average, sample_weight=sample_weight)
Expand Down