Skip to content

Commit a4cc05d

Browse files
committed
Pushing the docs to dev/ for branch: main, commit 6343cd74c9ff90526212cfaf65ac58a6c59a82e3
1 parent 0501554 commit a4cc05d

File tree

1,516 files changed

+6011
-5978
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,516 files changed

+6011
-5978
lines changed

dev/_downloads/010337852815f8103ac6cca38a812b3c/plot_roc_crossval.py

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -62,46 +62,56 @@
6262
# Classification and ROC analysis
6363
# -------------------------------
6464
#
65-
# Here we run a :class:`~sklearn.svm.SVC` classifier with cross-validation and
66-
# plot the ROC curves fold-wise. Notice that the baseline to define the chance
65+
# Here we run :func:`~sklearn.model_selection.cross_validate` on a
66+
# :class:`~sklearn.svm.SVC` classifier, then use the computed cross-validation results
67+
# to plot the ROC curves fold-wise. Notice that the baseline to define the chance
6768
# level (dashed ROC curve) is a classifier that would always predict the most
6869
# frequent class.
6970

7071
import matplotlib.pyplot as plt
7172

7273
from sklearn import svm
7374
from sklearn.metrics import RocCurveDisplay, auc
74-
from sklearn.model_selection import StratifiedKFold
75+
from sklearn.model_selection import StratifiedKFold, cross_validate
7576

7677
n_splits = 6
7778
cv = StratifiedKFold(n_splits=n_splits)
7879
classifier = svm.SVC(kernel="linear", probability=True, random_state=random_state)
80+
cv_results = cross_validate(
81+
classifier, X, y, cv=cv, return_estimator=True, return_indices=True
82+
)
83+
84+
prop_cycle = plt.rcParams["axes.prop_cycle"]
85+
colors = prop_cycle.by_key()["color"]
86+
curve_kwargs_list = [
87+
dict(alpha=0.3, lw=1, color=colors[fold % len(colors)]) for fold in range(n_splits)
88+
]
89+
names = [f"ROC fold {idx}" for idx in range(n_splits)]
7990

80-
tprs = []
81-
aucs = []
8291
mean_fpr = np.linspace(0, 1, 100)
92+
interp_tprs = []
93+
94+
_, ax = plt.subplots(figsize=(6, 6))
95+
viz = RocCurveDisplay.from_cv_results(
96+
cv_results,
97+
X,
98+
y,
99+
ax=ax,
100+
name=names,
101+
curve_kwargs=curve_kwargs_list,
102+
plot_chance_level=True,
103+
)
83104

84-
fig, ax = plt.subplots(figsize=(6, 6))
85-
for fold, (train, test) in enumerate(cv.split(X, y)):
86-
classifier.fit(X[train], y[train])
87-
viz = RocCurveDisplay.from_estimator(
88-
classifier,
89-
X[test],
90-
y[test],
91-
name=f"ROC fold {fold}",
92-
curve_kwargs=dict(alpha=0.3, lw=1),
93-
ax=ax,
94-
plot_chance_level=(fold == n_splits - 1),
95-
)
96-
interp_tpr = np.interp(mean_fpr, viz.fpr, viz.tpr)
105+
for idx in range(n_splits):
106+
interp_tpr = np.interp(mean_fpr, viz.fpr[idx], viz.tpr[idx])
97107
interp_tpr[0] = 0.0
98-
tprs.append(interp_tpr)
99-
aucs.append(viz.roc_auc)
108+
interp_tprs.append(interp_tpr)
100109

101-
mean_tpr = np.mean(tprs, axis=0)
110+
mean_tpr = np.mean(interp_tprs, axis=0)
102111
mean_tpr[-1] = 1.0
103112
mean_auc = auc(mean_fpr, mean_tpr)
104-
std_auc = np.std(aucs)
113+
std_auc = np.std(viz.roc_auc)
114+
105115
ax.plot(
106116
mean_fpr,
107117
mean_tpr,
@@ -111,7 +121,7 @@
111121
alpha=0.8,
112122
)
113123

114-
std_tpr = np.std(tprs, axis=0)
124+
std_tpr = np.std(interp_tprs, axis=0)
115125
tprs_upper = np.minimum(mean_tpr + std_tpr, 1)
116126
tprs_lower = np.maximum(mean_tpr - std_tpr, 0)
117127
ax.fill_between(
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

dev/_downloads/055e8313e28f2f3b5fd508054dfe5fe0/plot_roc_crossval.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"cell_type": "markdown",
5959
"metadata": {},
6060
"source": [
61-
"### Classification and ROC analysis\n\nHere we run a :class:`~sklearn.svm.SVC` classifier with cross-validation and\nplot the ROC curves fold-wise. Notice that the baseline to define the chance\nlevel (dashed ROC curve) is a classifier that would always predict the most\nfrequent class.\n\n"
61+
"### Classification and ROC analysis\n\nHere we run :func:`~sklearn.model_selection.cross_validate` on a\n:class:`~sklearn.svm.SVC` classifier, then use the computed cross-validation results\nto plot the ROC curves fold-wise. Notice that the baseline to define the chance\nlevel (dashed ROC curve) is a classifier that would always predict the most\nfrequent class.\n\n"
6262
]
6363
},
6464
{
@@ -69,7 +69,7 @@
6969
},
7070
"outputs": [],
7171
"source": [
72-
"import matplotlib.pyplot as plt\n\nfrom sklearn import svm\nfrom sklearn.metrics import RocCurveDisplay, auc\nfrom sklearn.model_selection import StratifiedKFold\n\nn_splits = 6\ncv = StratifiedKFold(n_splits=n_splits)\nclassifier = svm.SVC(kernel=\"linear\", probability=True, random_state=random_state)\n\ntprs = []\naucs = []\nmean_fpr = np.linspace(0, 1, 100)\n\nfig, ax = plt.subplots(figsize=(6, 6))\nfor fold, (train, test) in enumerate(cv.split(X, y)):\n classifier.fit(X[train], y[train])\n viz = RocCurveDisplay.from_estimator(\n classifier,\n X[test],\n y[test],\n name=f\"ROC fold {fold}\",\n curve_kwargs=dict(alpha=0.3, lw=1),\n ax=ax,\n plot_chance_level=(fold == n_splits - 1),\n )\n interp_tpr = np.interp(mean_fpr, viz.fpr, viz.tpr)\n interp_tpr[0] = 0.0\n tprs.append(interp_tpr)\n aucs.append(viz.roc_auc)\n\nmean_tpr = np.mean(tprs, axis=0)\nmean_tpr[-1] = 1.0\nmean_auc = auc(mean_fpr, mean_tpr)\nstd_auc = np.std(aucs)\nax.plot(\n mean_fpr,\n mean_tpr,\n color=\"b\",\n label=r\"Mean ROC (AUC = %0.2f $\\pm$ %0.2f)\" % (mean_auc, std_auc),\n lw=2,\n alpha=0.8,\n)\n\nstd_tpr = np.std(tprs, axis=0)\ntprs_upper = np.minimum(mean_tpr + std_tpr, 1)\ntprs_lower = np.maximum(mean_tpr - std_tpr, 0)\nax.fill_between(\n mean_fpr,\n tprs_lower,\n tprs_upper,\n color=\"grey\",\n alpha=0.2,\n label=r\"$\\pm$ 1 std. dev.\",\n)\n\nax.set(\n xlabel=\"False Positive Rate\",\n ylabel=\"True Positive Rate\",\n title=f\"Mean ROC curve with variability\\n(Positive label '{target_names[1]}')\",\n)\nax.legend(loc=\"lower right\")\nplt.show()"
72+
"import matplotlib.pyplot as plt\n\nfrom sklearn import svm\nfrom sklearn.metrics import RocCurveDisplay, auc\nfrom sklearn.model_selection import StratifiedKFold, cross_validate\n\nn_splits = 6\ncv = StratifiedKFold(n_splits=n_splits)\nclassifier = svm.SVC(kernel=\"linear\", probability=True, random_state=random_state)\ncv_results = cross_validate(\n classifier, X, y, cv=cv, return_estimator=True, return_indices=True\n)\n\nprop_cycle = plt.rcParams[\"axes.prop_cycle\"]\ncolors = prop_cycle.by_key()[\"color\"]\ncurve_kwargs_list = [\n dict(alpha=0.3, lw=1, color=colors[fold % len(colors)]) for fold in range(n_splits)\n]\nnames = [f\"ROC fold {idx}\" for idx in range(n_splits)]\n\nmean_fpr = np.linspace(0, 1, 100)\ninterp_tprs = []\n\n_, ax = plt.subplots(figsize=(6, 6))\nviz = RocCurveDisplay.from_cv_results(\n cv_results,\n X,\n y,\n ax=ax,\n name=names,\n curve_kwargs=curve_kwargs_list,\n plot_chance_level=True,\n)\n\nfor idx in range(n_splits):\n interp_tpr = np.interp(mean_fpr, viz.fpr[idx], viz.tpr[idx])\n interp_tpr[0] = 0.0\n interp_tprs.append(interp_tpr)\n\nmean_tpr = np.mean(interp_tprs, axis=0)\nmean_tpr[-1] = 1.0\nmean_auc = auc(mean_fpr, mean_tpr)\nstd_auc = np.std(viz.roc_auc)\n\nax.plot(\n mean_fpr,\n mean_tpr,\n color=\"b\",\n label=r\"Mean ROC (AUC = %0.2f $\\pm$ %0.2f)\" % (mean_auc, std_auc),\n lw=2,\n alpha=0.8,\n)\n\nstd_tpr = np.std(interp_tprs, axis=0)\ntprs_upper = np.minimum(mean_tpr + std_tpr, 1)\ntprs_lower = np.maximum(mean_tpr - std_tpr, 0)\nax.fill_between(\n mean_fpr,\n tprs_lower,\n tprs_upper,\n color=\"grey\",\n alpha=0.2,\n label=r\"$\\pm$ 1 std. dev.\",\n)\n\nax.set(\n xlabel=\"False Positive Rate\",\n ylabel=\"True Positive Rate\",\n title=f\"Mean ROC curve with variability\\n(Positive label '{target_names[1]}')\",\n)\nax.legend(loc=\"lower right\")\nplt.show()"
7373
]
7474
}
7575
],
Binary file not shown.

0 commit comments

Comments
 (0)