Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot multiple ROC curves in one plot with legend and AUC scores in python?

Tags:

python

plot

roc

auc

I am building 2 models.

Model 1

modelgb = GradientBoostingClassifier()
modelgb.fit(x_train,y_train)
predsgb = modelgb.predict_proba(x_test)[:,1]
metrics.roc_auc_score(y_test,predsgb, average='macro', sample_weight=None)

Model 2

model = LogisticRegression()
model = model.fit(x_train,y_train)
predslog = model.predict_proba(x_test)[:,1]
metrics.roc_auc_score(y_test,predslog, average='macro', sample_weight=None)

How do i plot both the ROC curves in one plot , with a legend & text of AUC scores for each model ?

like image 621
Learner_seeker Avatar asked Mar 20 '17 02:03

Learner_seeker


People also ask

Can ROC and AUC curve be used for multiclass classification?

AUC-ROC for Multi-Class Classification Like I said before, the AUC-ROC curve is only for binary classification problems. But we can extend it to multiclass classification problems by using the One vs All technique.

How do you plot a ROC curve for different models?

To plot the ROC curve, we need to calculate the TPR and FPR for many different thresholds (This step is included in all relevant libraries as scikit-learn ). For each threshold, we plot the FPR value in the x-axis and the TPR value in the y-axis. We then join the dots with a line. That's it!


1 Answers

Try adapting this to your data:

from sklearn import metrics
import numpy as np
import matplotlib.pyplot as plt

plt.figure(0).clf()

pred = np.random.rand(1000)
label = np.random.randint(2, size=1000)
fpr, tpr, thresh = metrics.roc_curve(label, pred)
auc = metrics.roc_auc_score(label, pred)
plt.plot(fpr,tpr,label="data 1, auc="+str(auc))

pred = np.random.rand(1000)
label = np.random.randint(2, size=1000)
fpr, tpr, thresh = metrics.roc_curve(label, pred)
auc = metrics.roc_auc_score(label, pred)
plt.plot(fpr,tpr,label="data 2, auc="+str(auc))

plt.legend(loc=0)
like image 117
Julien Avatar answered Sep 18 '22 19:09

Julien