DOC update notebook-style example plot_cv_diabetes.py
@@ -10,13 +10,14 @@ This exercise is used in the :ref:`cv_estimators_tut` part of the
"""
-import numpy as np
+# %%
+# Load dataset and apply GridSearchCV
+# -----------------------------------
import matplotlib.pyplot as plt
+import numpy as np
from sklearn import datasets
-from sklearn.linear_model import LassoCV
from sklearn.linear_model import Lasso
-from sklearn.model_selection import KFold
from sklearn.model_selection import GridSearchCV
X, y = datasets.load_diabetes(return_X_y=True)
@@ -33,10 +34,14 @@ clf = GridSearchCV(lasso, tuned_parameters, cv=n_folds, refit=False)
clf.fit(X, y)
scores = clf.cv_results_["mean_test_score"]
scores_std = clf.cv_results_["std_test_score"]
+
+# %%
+# Plot error lines showing +/- std. errors of the scores
+# ------------------------------------------------------
+
plt.figure().set_size_inches(8, 6)
plt.semilogx(alphas, scores)
-# plot error lines showing +/- std. errors of the scores
std_error = scores_std / np.sqrt(n_folds)
plt.semilogx(alphas, scores + std_error, "b--")
@@ -50,14 +55,19 @@ plt.xlabel("alpha")
plt.axhline(np.max(scores), linestyle="--", color=".5")
plt.xlim([alphas[0], alphas[-1]])
-# #############################################################################
+# %%
# Bonus: how much can you trust the selection of alpha?
+# -----------------------------------------------------
# To answer this question we use the LassoCV object that sets its alpha
# parameter automatically from the data by internal cross-validation (i.e. it
# performs cross-validation on the training data it receives).
# We use external cross-validation to see how much the automatically obtained
# alphas differ across different cross-validation folds.
+
+from sklearn.linear_model import LassoCV
+from sklearn.model_selection import KFold
+
lasso_cv = LassoCV(alphas=alphas, random_state=0, max_iter=10000)
k_fold = KFold(3)
