2010-05-28 17:36:17 +08:00
|
|
|
"""
|
2010-11-02 18:38:06 +08:00
|
|
|
=============================================================
|
2013-08-01 09:13:22 +08:00
|
|
|
Receiver Operating Characteristic (ROC) with cross validation
|
2010-11-02 18:38:06 +08:00
|
|
|
=============================================================
|
2010-05-28 17:36:17 +08:00
|
|
|
|
2013-08-01 09:13:22 +08:00
|
|
|
Example of Receiver Operating Characteristic (ROC) metric to evaluate
|
|
|
|
|
classifier output quality using cross-validation.
|
2013-07-26 01:12:57 +08:00
|
|
|
|
2013-08-01 09:13:22 +08:00
|
|
|
ROC curves typically feature true positive rate on the Y axis, and false
|
|
|
|
|
positive rate on the X axis. This means that the top left corner of the plot is
|
|
|
|
|
the "ideal" point - a false positive rate of zero, and a true positive rate of
|
|
|
|
|
one. This is not very realistic, but it does mean that a larger area under the
|
|
|
|
|
curve (AUC) is usually better.
|
2013-07-26 01:12:57 +08:00
|
|
|
|
2013-08-01 09:13:22 +08:00
|
|
|
The "steepness" of ROC curves is also important, since it is ideal to maximize
|
|
|
|
|
the true positive rate while minimizing the false positive rate.
|
|
|
|
|
|
|
|
|
|
This example shows the ROC response of different datasets, created from K-fold
|
|
|
|
|
cross-validation. Taking all of these curves, it is possible to calculate the
|
|
|
|
|
mean area under curve, and see the variance of the curve when the
|
|
|
|
|
training set is split into different subsets. This roughly shows how the
|
|
|
|
|
classifier output is affected by changes in the training data, and how
|
|
|
|
|
different the splits generated by K-fold cross-validation are from one another.
|
2013-07-26 19:33:10 +08:00
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
2017-11-16 14:25:28 +08:00
|
|
|
See also :func:`sklearn.metrics.roc_auc_score`,
|
2015-09-11 02:26:39 +08:00
|
|
|
:func:`sklearn.model_selection.cross_val_score`,
|
2016-01-25 07:18:26 +08:00
|
|
|
:ref:`sphx_glr_auto_examples_model_selection_plot_roc.py`,
|
2013-07-26 19:33:10 +08:00
|
|
|
|
2010-05-28 17:36:17 +08:00
|
|
|
"""
|
|
|
|
|
|
2022-03-18 21:58:27 +08:00
|
|
|
# %%
|
|
|
|
|
# Data IO and generation
|
|
|
|
|
# ----------------------
|
2010-05-28 17:36:17 +08:00
|
|
|
import numpy as np
|
|
|
|
|
|
2022-03-18 21:58:27 +08:00
|
|
|
from sklearn import datasets
|
2010-06-16 17:12:21 +08:00
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# Import some data to play with
|
2010-05-28 17:36:17 +08:00
|
|
|
iris = datasets.load_iris()
|
|
|
|
|
X = iris.data
|
|
|
|
|
y = iris.target
|
2011-12-20 01:16:51 +08:00
|
|
|
X, y = X[y != 2], y[y != 2]
|
2010-05-28 17:36:17 +08:00
|
|
|
n_samples, n_features = X.shape
|
|
|
|
|
|
|
|
|
|
# Add noisy features
|
2013-08-01 09:13:22 +08:00
|
|
|
random_state = np.random.RandomState(0)
|
|
|
|
|
X = np.c_[X, random_state.randn(n_samples, 200 * n_features)]
|
2010-05-28 17:36:17 +08:00
|
|
|
|
2022-03-18 21:58:27 +08:00
|
|
|
# %%
|
2010-06-16 17:12:21 +08:00
|
|
|
# Classification and ROC analysis
|
2022-03-18 21:58:27 +08:00
|
|
|
# -------------------------------
|
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
|
|
from sklearn import svm
|
|
|
|
|
from sklearn.metrics import auc
|
|
|
|
|
from sklearn.metrics import RocCurveDisplay
|
|
|
|
|
from sklearn.model_selection import StratifiedKFold
|
2010-06-16 17:12:21 +08:00
|
|
|
|
2013-07-26 19:33:10 +08:00
|
|
|
# Run classifier with cross-validation and plot ROC curves
|
2016-08-17 04:56:55 +08:00
|
|
|
cv = StratifiedKFold(n_splits=6)
|
2021-09-03 23:19:46 +08:00
|
|
|
classifier = svm.SVC(kernel="linear", probability=True, random_state=random_state)
|
2010-05-28 17:36:17 +08:00
|
|
|
|
2017-01-25 21:09:54 +08:00
|
|
|
tprs = []
|
|
|
|
|
aucs = []
|
2010-05-28 17:36:17 +08:00
|
|
|
mean_fpr = np.linspace(0, 1, 100)
|
|
|
|
|
|
2019-08-08 08:28:17 +08:00
|
|
|
fig, ax = plt.subplots()
|
|
|
|
|
for i, (train, test) in enumerate(cv.split(X, y)):
|
|
|
|
|
classifier.fit(X[train], y[train])
|
2021-09-03 23:19:46 +08:00
|
|
|
viz = RocCurveDisplay.from_estimator(
|
|
|
|
|
classifier,
|
|
|
|
|
X[test],
|
|
|
|
|
y[test],
|
|
|
|
|
name="ROC fold {}".format(i),
|
|
|
|
|
alpha=0.3,
|
|
|
|
|
lw=1,
|
|
|
|
|
ax=ax,
|
|
|
|
|
)
|
2020-05-08 08:01:04 +08:00
|
|
|
interp_tpr = np.interp(mean_fpr, viz.fpr, viz.tpr)
|
2019-08-08 08:28:17 +08:00
|
|
|
interp_tpr[0] = 0.0
|
|
|
|
|
tprs.append(interp_tpr)
|
|
|
|
|
aucs.append(viz.roc_auc)
|
|
|
|
|
|
2021-09-03 23:19:46 +08:00
|
|
|
ax.plot([0, 1], [0, 1], linestyle="--", lw=2, color="r", label="Chance", alpha=0.8)
|
2010-05-28 17:36:17 +08:00
|
|
|
|
2017-01-25 21:09:54 +08:00
|
|
|
mean_tpr = np.mean(tprs, axis=0)
|
2010-05-28 17:36:17 +08:00
|
|
|
mean_tpr[-1] = 1.0
|
|
|
|
|
mean_auc = auc(mean_fpr, mean_tpr)
|
2017-01-25 21:09:54 +08:00
|
|
|
std_auc = np.std(aucs)
|
2021-09-03 23:19:46 +08:00
|
|
|
ax.plot(
|
|
|
|
|
mean_fpr,
|
|
|
|
|
mean_tpr,
|
|
|
|
|
color="b",
|
|
|
|
|
label=r"Mean ROC (AUC = %0.2f $\pm$ %0.2f)" % (mean_auc, std_auc),
|
|
|
|
|
lw=2,
|
|
|
|
|
alpha=0.8,
|
|
|
|
|
)
|
2017-01-25 21:09:54 +08:00
|
|
|
|
|
|
|
|
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)
|
2021-09-03 23:19:46 +08:00
|
|
|
ax.fill_between(
|
|
|
|
|
mean_fpr,
|
|
|
|
|
tprs_lower,
|
|
|
|
|
tprs_upper,
|
|
|
|
|
color="grey",
|
|
|
|
|
alpha=0.2,
|
|
|
|
|
label=r"$\pm$ 1 std. dev.",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
ax.set(
|
|
|
|
|
xlim=[-0.05, 1.05],
|
|
|
|
|
ylim=[-0.05, 1.05],
|
|
|
|
|
title="Receiver operating characteristic example",
|
|
|
|
|
)
|
2019-08-08 08:28:17 +08:00
|
|
|
ax.legend(loc="lower right")
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.show()
|