Skip to content

Instantly share code, notes, and snippets.

@tylerneylon
Created May 9, 2019 18:56
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 tylerneylon/89efac81b4546bfe766d759d1e9d266a to your computer and use it in GitHub Desktop.
Save tylerneylon/89efac81b4546bfe766d759d1e9d266a to your computer and use it in GitHub Desktop.
# Have 1d array-likes y_true and y_pred.
# y_true is expected to be 0/1,
# and y_pred is expected to have floats in [0, 1].
#
# I learned how to do this from here:
# https://scikit-learn.org/stable/auto_examples/model_selection/plot_precision_recall.html
from sklearn.metrics import average_precision_score
from sklearn.metrics import precision_recall_curve
prec, recall, _ = precision_recall_curve(y_true, y_pred)
avg_prec = average_precision_score(y_true, y_pred)
plt.step(recall, prec, color='b', alpha=0.2, where='post')
plt.fill_between(recall, prec, alpha=0.2, color='b')
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.ylim([0.0, 1.05])
plt.xlim([0.0, 1.0])
plt.title('2-class Precision-Recall curve: AP={0:0.2f}'.format(avg_prec))
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment