Skip to content

Commit ef0eade

Browse files
committed
Pushing the docs for revision for branch: master, commit 22c687b525d15e97278ea14b535cf3fecce10727
1 parent e9acf15 commit ef0eade

File tree

837 files changed

+2826
-2806
lines changed

Some content is hidden

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

837 files changed

+2826
-2806
lines changed

dev/_downloads/plot_cv_diabetes.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
This exercise is used in the :ref:`cv_estimators_tut` part of the
99
:ref:`model_selection_tut` section of the :ref:`stat_learn_tut_index`.
1010
"""
11+
1112
from __future__ import print_function
1213
print(__doc__)
1314

@@ -24,28 +25,38 @@
2425
X = diabetes.data[:150]
2526
y = diabetes.target[:150]
2627

27-
lasso = Lasso()
28-
alphas = np.logspace(-4, -.5, 30)
28+
lasso = Lasso(random_state=0)
29+
alphas = np.logspace(-4, -0.5, 30)
2930

3031
scores = list()
3132
scores_std = list()
3233

34+
n_folds = 3
35+
3336
for alpha in alphas:
3437
lasso.alpha = alpha
35-
this_scores = cross_val_score(lasso, X, y, n_jobs=1)
38+
this_scores = cross_val_score(lasso, X, y, cv=n_folds, n_jobs=1)
3639
scores.append(np.mean(this_scores))
3740
scores_std.append(np.std(this_scores))
3841

39-
plt.figure(figsize=(4, 3))
42+
scores, scores_std = np.array(scores), np.array(scores_std)
43+
44+
plt.figure().set_size_inches(8, 6)
4045
plt.semilogx(alphas, scores)
46+
4147
# plot error lines showing +/- std. errors of the scores
42-
plt.semilogx(alphas, np.array(scores) + np.array(scores_std) / np.sqrt(len(X)),
43-
'b--')
44-
plt.semilogx(alphas, np.array(scores) - np.array(scores_std) / np.sqrt(len(X)),
45-
'b--')
46-
plt.ylabel('CV score')
48+
std_error = scores_std / np.sqrt(n_folds)
49+
50+
plt.semilogx(alphas, scores + std_error, 'b--')
51+
plt.semilogx(alphas, scores - std_error, 'b--')
52+
53+
# alpha=0.2 controls the translucency of the fill color
54+
plt.fill_between(alphas, scores + std_error, scores - std_error, alpha=0.2)
55+
56+
plt.ylabel('CV score +/- std error')
4757
plt.xlabel('alpha')
4858
plt.axhline(np.max(scores), linestyle='--', color='.5')
59+
plt.xlim([alphas[0], alphas[-1]])
4960

5061
##############################################################################
5162
# Bonus: how much can you trust the selection of alpha?
@@ -55,7 +66,7 @@
5566
# performs cross-validation on the training data it receives).
5667
# We use external cross-validation to see how much the automatically obtained
5768
# alphas differ across different cross-validation folds.
58-
lasso_cv = LassoCV(alphas=alphas)
69+
lasso_cv = LassoCV(alphas=alphas, random_state=0)
5970
k_fold = KFold(3)
6071

6172
print("Answer to the bonus question:",
101 Bytes
101 Bytes
317 Bytes
317 Bytes
323 Bytes
323 Bytes
-334 Bytes
-334 Bytes
98 Bytes

0 commit comments

Comments
 (0)