Last active
June 9, 2017 16:33
-
-
Save vene/64e11174e3333ca695b7453629cdf5b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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