Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 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