1import sklearn.metrics as metrics
2# calculate the fpr and tpr for all thresholds of the classification
3probs = model.predict_proba(X_test)
4preds = probs[:,1]
5fpr, tpr, threshold = metrics.roc_curve(y_test, preds)
6roc_auc = metrics.auc(fpr, tpr)
7
8# method I: plt
9import matplotlib.pyplot as plt
10plt.title('Receiver Operating Characteristic')
11plt.plot(fpr, tpr, 'b', label = 'AUC = %0.2f' % roc_auc)
12plt.legend(loc = 'lower right')
13plt.plot([0, 1], [0, 1],'r--')
14plt.xlim([0, 1])
15plt.ylim([0, 1])
16plt.ylabel('True Positive Rate')
17plt.xlabel('False Positive Rate')
18plt.show()
19
20# method II: ggplot
21from ggplot import *
22df = pd.DataFrame(dict(fpr = fpr, tpr = tpr))
23ggplot(df, aes(x = 'fpr', y = 'tpr')) + geom_line() + geom_abline(linetype = 'dashed')
1#ROC curve code snippet from external source(Module notes)
2def draw_roc( actual, probs ):
3 fpr, tpr, thresholds = metrics.roc_curve( actual, probs,
4 drop_intermediate = False )
5 auc_score = metrics.roc_auc_score( actual, probs )
6 plt.figure(figsize=(5, 5))
7 plt.plot( fpr, tpr, label='ROC curve (area = %0.2f)' % auc_score )
8 plt.plot([0, 1], [0, 1], 'k--')
9 plt.xlim([0.0, 1.0])
10 plt.ylim([0.0, 1.05])
11 plt.xlabel('False Positive Rate or [1 - True Negative Rate]')
12 plt.ylabel('True Positive Rate')
13 plt.title('Receiver operating characteristic example')
14 plt.legend(loc="lower right")
15 plt.show()
16
17 return None
18
19fpr, tpr, thresholds = metrics.roc_curve( y_train_pred_final.Converted, y_train_pred_final.Converted_prob, drop_intermediate = False )
20
21draw_roc(y_train_pred_final.Converted, y_train_pred_final.Converted_prob)