Skip to content

Instantly share code, notes, and snippets.

@esmitt
Last active October 15, 2020 20:37
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 esmitt/28cf2c9ebc58de570d62830af51b6a25 to your computer and use it in GitHub Desktop.
Save esmitt/28cf2c9ebc58de570d62830af51b6a25 to your computer and use it in GitHub Desktop.
Plotting the ROC curve using matplotlib
from sklearn.metrics import roc_auc_score, roc_curve
def plot_roc(name: str, labels: numpy.ndarray, predictions: numpy.ndarray, **kwargs) -> ():
fp, tp, _ = roc_curve(labels, predictions)
auc_roc = roc_auc_score(labels, predictions)
plt.plot(100*fp, 100*tp, label=name + " (" + str(round(auc_roc, 3)) + ")",
linewidth=2, **kwargs)
plt.xlabel('False positives [%]')
plt.ylabel('True positives [%]')
plt.title('ROC curve')
plt.grid(True)
plt.legend(loc='best')
ax = plt.gca()
ax.set_aspect('equal')
plot_roc("Train Base", y_train, y_train_pred, color=colors[0])
plot_roc("Test Base", y_test, y_test_pred, color=colors[0], linestyle='--')
plt.legend(loc='lower right')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment