From 411b5c30ab8c09ff248abcc205353e51682503dd Mon Sep 17 00:00:00 2001 From: Guillaume Lemaitre Date: Wed, 25 Jan 2017 01:04:02 +0100 Subject: [PATCH 1/4] Fix esthetic example roc crossval --- examples/model_selection/plot_roc_crossval.py | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/examples/model_selection/plot_roc_crossval.py b/examples/model_selection/plot_roc_crossval.py index 6678dcb1af8b3..a806c10a21512 100644 --- a/examples/model_selection/plot_roc_crossval.py +++ b/examples/model_selection/plot_roc_crossval.py @@ -62,32 +62,34 @@ classifier = svm.SVC(kernel='linear', probability=True, random_state=random_state) -mean_tpr = 0.0 +tprs = [] mean_fpr = np.linspace(0, 1, 100) -colors = cycle(['cyan', 'indigo', 'seagreen', 'yellow', 'blue', 'darkorange']) -lw = 2 - i = 0 -for (train, test), color in zip(cv.split(X, y), colors): +for train, test in cv.split(X, y): probas_ = classifier.fit(X[train], y[train]).predict_proba(X[test]) # Compute ROC curve and area the curve fpr, tpr, thresholds = roc_curve(y[test], probas_[:, 1]) - mean_tpr += interp(mean_fpr, fpr, tpr) - mean_tpr[0] = 0.0 + tprs.append(interp(mean_fpr, fpr, tpr)) + tprs[-1][0] = 0.0 roc_auc = auc(fpr, tpr) - plt.plot(fpr, tpr, lw=lw, color=color, - label='ROC fold %d (area = %0.2f)' % (i, roc_auc)) + plt.plot(fpr, tpr, lw=1, color='b', alpha=0.15, + label='ROC fold %d (AUC = %0.2f)' % (i, roc_auc)) i += 1 -plt.plot([0, 1], [0, 1], linestyle='--', lw=lw, color='k', - label='Luck') +plt.plot([0, 1], [0, 1], linestyle='--', lw=2, color='r', + label='Luck', alpha=.8) -mean_tpr /= cv.get_n_splits(X, y) +mean_tpr = np.mean(tprs, axis=0) mean_tpr[-1] = 1.0 mean_auc = auc(mean_fpr, mean_tpr) -plt.plot(mean_fpr, mean_tpr, color='g', linestyle='--', - label='Mean ROC (area = %0.2f)' % mean_auc, lw=lw) +plt.plot(mean_fpr, mean_tpr, color='b', + label='Mean ROC (AUC = %0.2f)' % mean_auc, lw=2, alpha=.8) + +std_tpr = np.std(tprs, axis=0) +tprs_upper = np.minimum(mean_tpr + std_tpr, 1) +tprs_lower = np.maximum(mean_tpr - std_tpr, 0) +plt.fill_between(mean_fpr, tprs_lower, tprs_upper, color='grey', alpha=.2) plt.xlim([-0.05, 1.05]) plt.ylim([-0.05, 1.05]) From d71804effe15d21d9b4eea2dd0be5bc1bf03f655 Mon Sep 17 00:00:00 2001 From: Guillaume Lemaitre Date: Wed, 25 Jan 2017 10:55:08 +0100 Subject: [PATCH 2/4] different colors for the roc --- examples/model_selection/plot_roc_crossval.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/model_selection/plot_roc_crossval.py b/examples/model_selection/plot_roc_crossval.py index a806c10a21512..213941e70ef2c 100644 --- a/examples/model_selection/plot_roc_crossval.py +++ b/examples/model_selection/plot_roc_crossval.py @@ -63,6 +63,7 @@ random_state=random_state) tprs = [] +aucs = [] mean_fpr = np.linspace(0, 1, 100) i = 0 @@ -73,7 +74,8 @@ tprs.append(interp(mean_fpr, fpr, tpr)) tprs[-1][0] = 0.0 roc_auc = auc(fpr, tpr) - plt.plot(fpr, tpr, lw=1, color='b', alpha=0.15, + aucs.append(roc_auc) + plt.plot(fpr, tpr, lw=1, alpha=0.3, label='ROC fold %d (AUC = %0.2f)' % (i, roc_auc)) i += 1 @@ -83,8 +85,10 @@ mean_tpr = np.mean(tprs, axis=0) mean_tpr[-1] = 1.0 mean_auc = auc(mean_fpr, mean_tpr) +std_auc = np.std(aucs) plt.plot(mean_fpr, mean_tpr, color='b', - label='Mean ROC (AUC = %0.2f)' % mean_auc, lw=2, alpha=.8) + label=r'Mean ROC (AUC = %0.2f $\pm$ %0.2f)' % (mean_auc, std_auc), + lw=2, alpha=.8) std_tpr = np.std(tprs, axis=0) tprs_upper = np.minimum(mean_tpr + std_tpr, 1) From be461a8db9fa7a74c7a11cc7c4131e72e962b45b Mon Sep 17 00:00:00 2001 From: Guillaume Lemaitre Date: Wed, 25 Jan 2017 13:33:50 +0100 Subject: [PATCH 3/4] update for the sigma --- examples/model_selection/plot_roc_crossval.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/model_selection/plot_roc_crossval.py b/examples/model_selection/plot_roc_crossval.py index 213941e70ef2c..c339a476e66f9 100644 --- a/examples/model_selection/plot_roc_crossval.py +++ b/examples/model_selection/plot_roc_crossval.py @@ -93,7 +93,8 @@ std_tpr = np.std(tprs, axis=0) tprs_upper = np.minimum(mean_tpr + std_tpr, 1) tprs_lower = np.maximum(mean_tpr - std_tpr, 0) -plt.fill_between(mean_fpr, tprs_lower, tprs_upper, color='grey', alpha=.2) +plt.fill_between(mean_fpr, tprs_lower, tprs_upper, color='grey', alpha=.2, + label='1 sigma range') plt.xlim([-0.05, 1.05]) plt.ylim([-0.05, 1.05]) From a8ef87e0d6d097ab09ae80cbc0103478d50abadf Mon Sep 17 00:00:00 2001 From: Guillaume Lemaitre Date: Wed, 25 Jan 2017 14:04:15 +0100 Subject: [PATCH 4/4] modify std rendering --- examples/model_selection/plot_roc_crossval.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/model_selection/plot_roc_crossval.py b/examples/model_selection/plot_roc_crossval.py index c339a476e66f9..366aa0acbee06 100644 --- a/examples/model_selection/plot_roc_crossval.py +++ b/examples/model_selection/plot_roc_crossval.py @@ -94,7 +94,7 @@ tprs_upper = np.minimum(mean_tpr + std_tpr, 1) tprs_lower = np.maximum(mean_tpr - std_tpr, 0) plt.fill_between(mean_fpr, tprs_lower, tprs_upper, color='grey', alpha=.2, - label='1 sigma range') + label=r'$\pm$ 1 std. dev.') plt.xlim([-0.05, 1.05]) plt.ylim([-0.05, 1.05])