Skip to content

Instantly share code, notes, and snippets.

@vene
Last active June 9, 2017 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vene/64e11174e3333ca695b7453629cdf5b1 to your computer and use it in GitHub Desktop.
Save vene/64e11174e3333ca695b7453629cdf5b1 to your computer and use it in GitHub Desktop.
import numpy as np
from sklearn.metrics import precision_recall_curve, average_precision_score
def naive_interpolated_precision(y_true, y_scores):
precisions, recalls, _ = precision_recall_curve(y_true, y_scores)
interp_precisions = []
# the final point
precisions = precisions[:-1]
recalls = recalls[:-1]
for recall_point in [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]:
interp_precisions.append(precisions[recalls >= recall_point].max())
return np.mean(interp_precisions)
rng = np.random.RandomState(4)
for _ in range(100):
y_scores = rng.randn(100)
y_true = rng.randn(100) > 0.4
naive = naive_interpolated_precision(y_true, y_scores)
fast = average_precision_score(y_true, y_scores, interpolation="eleven_point")
print(np.abs(naive - fast))
0.0
0.000297088532383
0.0
0.0
0.0
0.0
0.0
0.000829967354617
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.000884127314333
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.00134815280426
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.00149653786017
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment