Skip to content

Instantly share code, notes, and snippets.

@fdabek1
Created April 15, 2020 05:04
Show Gist options
  • Save fdabek1/b029d74311a0862732cdf863d7b30051 to your computer and use it in GitHub Desktop.
Save fdabek1/b029d74311a0862732cdf863d7b30051 to your computer and use it in GitHub Desktop.
Generate Precision Recall Curve
from sklearn.metrics import precision_recall_curve, auc, average_precision_score, roc_auc_score
import matplotlib.pyplot as plt
from inspect import signature
def generate_curve(y_true, y_pred):
precision, recall, _ = precision_recall_curve(y_true, y_pred)
average_precision = average_precision_score(y_true, y_pred)
roc_auc = roc_auc_score(y_true, y_pred)
# In matplotlib < 1.5, plt.fill_between does not have a 'step' argument
step_kwargs = ({'step': 'post'}
if 'step' in signature(plt.fill_between).parameters
else {})
plt.step(recall, precision, color='b', alpha=0.2,
where='post')
plt.fill_between(recall, precision, alpha=0.2, color='b', **step_kwargs)
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.ylim([0.0, 1.05])
plt.xlim([0.0, 1.0])
plt.title('Precision-Recall Curve: AP={0:0.2f}; AUC={0:0.2f}'.format(average_precision, auc))
plt.clf()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment