Search code examples
pythonplotrocauc

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


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 ?


Solution

  • 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)