2012-03-12 17:19:01 +08:00
|
|
|
"""
|
|
|
|
|
===============================================
|
|
|
|
|
Cross-validation on diabetes Dataset Exercise
|
|
|
|
|
===============================================
|
|
|
|
|
|
2015-01-16 04:09:35 +08:00
|
|
|
A tutorial exercise which uses cross-validation with linear models.
|
2013-07-22 21:48:44 +08:00
|
|
|
|
2012-03-26 07:20:53 +08:00
|
|
|
This exercise is used in the :ref:`cv_estimators_tut` part of the
|
|
|
|
|
:ref:`model_selection_tut` section of the :ref:`stat_learn_tut_index`.
|
2015-12-21 06:06:07 +08:00
|
|
|
|
2021-10-22 21:33:22 +08:00
|
|
|
"""
|
2012-03-26 07:20:53 +08:00
|
|
|
|
2022-03-17 18:42:01 +08:00
|
|
|
# %%
|
|
|
|
|
# Load dataset and apply GridSearchCV
|
|
|
|
|
# -----------------------------------
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2022-03-17 18:42:01 +08:00
|
|
|
import numpy as np
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2015-09-11 02:26:39 +08:00
|
|
|
from sklearn import datasets
|
|
|
|
|
from sklearn.linear_model import Lasso
|
2017-02-08 07:10:25 +08:00
|
|
|
from sklearn.model_selection import GridSearchCV
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2019-08-25 11:17:01 +08:00
|
|
|
X, y = datasets.load_diabetes(return_X_y=True)
|
|
|
|
|
X = X[:150]
|
|
|
|
|
y = y[:150]
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2019-01-17 18:41:13 +08:00
|
|
|
lasso = Lasso(random_state=0, max_iter=10000)
|
2015-12-21 06:06:07 +08:00
|
|
|
alphas = np.logspace(-4, -0.5, 30)
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2017-02-08 07:10:25 +08:00
|
|
|
tuned_parameters = [{"alpha": alphas}]
|
2018-08-21 03:22:42 +08:00
|
|
|
n_folds = 5
|
2015-12-21 06:06:07 +08:00
|
|
|
|
2017-02-08 07:10:25 +08:00
|
|
|
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"]
|
2022-03-17 18:42:01 +08:00
|
|
|
|
|
|
|
|
# %%
|
|
|
|
|
# Plot error lines showing +/- std. errors of the scores
|
|
|
|
|
# ------------------------------------------------------
|
|
|
|
|
|
2015-12-21 06:06:07 +08:00
|
|
|
plt.figure().set_size_inches(8, 6)
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.semilogx(alphas, scores)
|
2015-12-21 06:06:07 +08:00
|
|
|
|
|
|
|
|
std_error = scores_std / np.sqrt(n_folds)
|
|
|
|
|
|
|
|
|
|
plt.semilogx(alphas, scores + std_error, "b--")
|
|
|
|
|
plt.semilogx(alphas, scores - std_error, "b--")
|
|
|
|
|
|
|
|
|
|
# alpha=0.2 controls the translucency of the fill color
|
|
|
|
|
plt.fill_between(alphas, scores + std_error, scores - std_error, alpha=0.2)
|
|
|
|
|
|
2016-01-16 20:54:10 +08:00
|
|
|
plt.ylabel("CV score +/- std error")
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.xlabel("alpha")
|
|
|
|
|
plt.axhline(np.max(scores), linestyle="--", color=".5")
|
2016-01-16 20:54:10 +08:00
|
|
|
plt.xlim([alphas[0], alphas[-1]])
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2022-03-17 18:42:01 +08:00
|
|
|
# %%
|
2011-12-18 19:39:53 +08:00
|
|
|
# Bonus: how much can you trust the selection of alpha?
|
2022-03-17 18:42:01 +08:00
|
|
|
# -----------------------------------------------------
|
2012-10-26 00:11:25 +08:00
|
|
|
|
2012-10-26 18:19:46 +08:00
|
|
|
# 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.
|
2022-03-17 18:42:01 +08:00
|
|
|
|
|
|
|
|
from sklearn.linear_model import LassoCV
|
|
|
|
|
from sklearn.model_selection import KFold
|
|
|
|
|
|
2019-05-29 21:39:20 +08:00
|
|
|
lasso_cv = LassoCV(alphas=alphas, random_state=0, max_iter=10000)
|
2015-09-11 02:26:39 +08:00
|
|
|
k_fold = KFold(3)
|
2012-10-26 00:11:25 +08:00
|
|
|
|
2013-02-01 22:04:03 +08:00
|
|
|
print("Answer to the bonus question:", "how much can you trust the selection of alpha?")
|
|
|
|
|
print()
|
|
|
|
|
print("Alpha parameters maximising the generalization score on different")
|
|
|
|
|
print("subsets of the data:")
|
2015-09-11 02:26:39 +08:00
|
|
|
for k, (train, test) in enumerate(k_fold.split(X, y)):
|
2012-10-29 18:31:12 +08:00
|
|
|
lasso_cv.fit(X[train], y[train])
|
2013-02-09 05:10:40 +08:00
|
|
|
print(
|
|
|
|
|
"[fold {0}] alpha: {1:.5f}, score: {2:.5f}".format(
|
|
|
|
|
k, lasso_cv.alpha_, lasso_cv.score(X[test], y[test])
|
2021-10-07 16:13:00 +08:00
|
|
|
)
|
|
|
|
|
)
|
2013-02-01 22:04:03 +08:00
|
|
|
print()
|
|
|
|
|
print("Answer: Not very much since we obtained different alphas for different")
|
|
|
|
|
print("subsets of the data and moreover, the scores for these alphas differ")
|
|
|
|
|
print("quite substantially.")
|
2012-10-26 00:11:25 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.show()
|