Skip to content

Instantly share code, notes, and snippets.

@dyerrington
Last active September 5, 2020 00:48
Show Gist options
  • Save dyerrington/dc0d1899bdd3c03b5999f1522c5bdc16 to your computer and use it in GitHub Desktop.
Save dyerrington/dc0d1899bdd3c03b5999f1522c5bdc16 to your computer and use it in GitHub Desktop.
I can't tell you how many times I've plotted a roc curve for a multi-class problem from scratch. Too many times. I decided to make this gist to demonstrate how to implement a multi-class ROC (Receiver Operator Characteristic) plot in the most simple manner possible using Python.
## import any sklearn models and collect predictions / probabilities beforehand
import matplotlib.pyplot as plt
from cycler import cycler
## Line color config -- rather than create a structure with a finite color palette, use your own to cycle through a list.
default_cycler = (cycler(color=['r', 'g', 'b', 'y']) +
cycler(linestyle=['-', '--', ':', '-.']))
plt.rc('axes', prop_cycle = default_cycler)
## Set confusion metrics per class
fpr, tpr, thresh = {}, {}, {}
for index, class_name in enumerate(pipe_model.classes_):
fpr, tpr, threshold = roc_curve(y_encoded, y_hat_prob[:,index], pos_label=index)
plt.plot(fpr, tpr, label = f"Class - {class_name}")
plt.title('Multiclass ROC curve')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive rate')
plt.legend(loc='best')
@dyerrington
Copy link
Author

Also, encoder is an instance of sklearn.preprocessing.LabelEncoder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment