Skip to content

Commit 3e7069e

Browse files
committed
Pushing the docs to dev/ for branch: main, commit 835904a7642b6d14b230b11b7038de07d1f9b29f
1 parent a504dcc commit 3e7069e

File tree

1,231 files changed

+4707
-4529
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,231 files changed

+4707
-4529
lines changed
Binary file not shown.
Binary file not shown.

dev/_downloads/b49810e68af99a01e25ba2dfc951b687/plot_train_error_vs_test_error.ipynb

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,61 @@
2626
},
2727
"outputs": [],
2828
"source": [
29-
"# Author: Alexandre Gramfort <alexandre.gramfort@inria.fr>\n# License: BSD 3 clause\n\nimport numpy as np\nfrom sklearn import linear_model\n\n# #############################################################################\n# Generate sample data\nn_samples_train, n_samples_test, n_features = 75, 150, 500\nnp.random.seed(0)\ncoef = np.random.randn(n_features)\ncoef[50:] = 0.0 # only the top 10 features are impacting the model\nX = np.random.randn(n_samples_train + n_samples_test, n_features)\ny = np.dot(X, coef)\n\n# Split train and test data\nX_train, X_test = X[:n_samples_train], X[n_samples_train:]\ny_train, y_test = y[:n_samples_train], y[n_samples_train:]\n\n# #############################################################################\n# Compute train and test errors\nalphas = np.logspace(-5, 1, 60)\nenet = linear_model.ElasticNet(l1_ratio=0.7, max_iter=10000)\ntrain_errors = list()\ntest_errors = list()\nfor alpha in alphas:\n enet.set_params(alpha=alpha)\n enet.fit(X_train, y_train)\n train_errors.append(enet.score(X_train, y_train))\n test_errors.append(enet.score(X_test, y_test))\n\ni_alpha_optim = np.argmax(test_errors)\nalpha_optim = alphas[i_alpha_optim]\nprint(\"Optimal regularization parameter : %s\" % alpha_optim)\n\n# Estimate the coef_ on full data with optimal regularization parameter\nenet.set_params(alpha=alpha_optim)\ncoef_ = enet.fit(X, y).coef_\n\n# #############################################################################\n# Plot results functions\n\nimport matplotlib.pyplot as plt\n\nplt.subplot(2, 1, 1)\nplt.semilogx(alphas, train_errors, label=\"Train\")\nplt.semilogx(alphas, test_errors, label=\"Test\")\nplt.vlines(\n alpha_optim,\n plt.ylim()[0],\n np.max(test_errors),\n color=\"k\",\n linewidth=3,\n label=\"Optimum on test\",\n)\nplt.legend(loc=\"lower left\")\nplt.ylim([0, 1.2])\nplt.xlabel(\"Regularization parameter\")\nplt.ylabel(\"Performance\")\n\n# Show estimated coef_ vs true coef\nplt.subplot(2, 1, 2)\nplt.plot(coef, label=\"True coef\")\nplt.plot(coef_, label=\"Estimated coef\")\nplt.legend()\nplt.subplots_adjust(0.09, 0.04, 0.94, 0.94, 0.26, 0.26)\nplt.show()"
29+
"# Author: Alexandre Gramfort <alexandre.gramfort@inria.fr>\n# License: BSD 3 clause"
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"metadata": {},
35+
"source": [
36+
"## Generate sample data\n\n"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": null,
42+
"metadata": {
43+
"collapsed": false
44+
},
45+
"outputs": [],
46+
"source": [
47+
"import numpy as np\nfrom sklearn import linear_model\nfrom sklearn.datasets import make_regression\nfrom sklearn.model_selection import train_test_split\n\nn_samples_train, n_samples_test, n_features = 75, 150, 500\nX, y, coef = make_regression(\n n_samples=n_samples_train + n_samples_test,\n n_features=n_features,\n n_informative=50,\n shuffle=False,\n noise=1.0,\n coef=True,\n)\nX_train, X_test, y_train, y_test = train_test_split(\n X, y, train_size=n_samples_train, test_size=n_samples_test, shuffle=False\n)"
48+
]
49+
},
50+
{
51+
"cell_type": "markdown",
52+
"metadata": {},
53+
"source": [
54+
"## Compute train and test errors\n\n"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": null,
60+
"metadata": {
61+
"collapsed": false
62+
},
63+
"outputs": [],
64+
"source": [
65+
"alphas = np.logspace(-5, 1, 60)\nenet = linear_model.ElasticNet(l1_ratio=0.7, max_iter=10000)\ntrain_errors = list()\ntest_errors = list()\nfor alpha in alphas:\n enet.set_params(alpha=alpha)\n enet.fit(X_train, y_train)\n train_errors.append(enet.score(X_train, y_train))\n test_errors.append(enet.score(X_test, y_test))\n\ni_alpha_optim = np.argmax(test_errors)\nalpha_optim = alphas[i_alpha_optim]\nprint(\"Optimal regularization parameter : %s\" % alpha_optim)\n\n# Estimate the coef_ on full data with optimal regularization parameter\nenet.set_params(alpha=alpha_optim)\ncoef_ = enet.fit(X, y).coef_"
66+
]
67+
},
68+
{
69+
"cell_type": "markdown",
70+
"metadata": {},
71+
"source": [
72+
"## Plot results functions\n\n"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": null,
78+
"metadata": {
79+
"collapsed": false
80+
},
81+
"outputs": [],
82+
"source": [
83+
"import matplotlib.pyplot as plt\n\nplt.subplot(2, 1, 1)\nplt.semilogx(alphas, train_errors, label=\"Train\")\nplt.semilogx(alphas, test_errors, label=\"Test\")\nplt.vlines(\n alpha_optim,\n plt.ylim()[0],\n np.max(test_errors),\n color=\"k\",\n linewidth=3,\n label=\"Optimum on test\",\n)\nplt.legend(loc=\"lower left\")\nplt.ylim([0, 1.2])\nplt.xlabel(\"Regularization parameter\")\nplt.ylabel(\"Performance\")\n\n# Show estimated coef_ vs true coef\nplt.subplot(2, 1, 2)\nplt.plot(coef, label=\"True coef\")\nplt.plot(coef_, label=\"Estimated coef\")\nplt.legend()\nplt.subplots_adjust(0.09, 0.04, 0.94, 0.94, 0.26, 0.26)\nplt.show()"
3084
]
3185
}
3286
],

dev/_downloads/dcb776e3eb7cce048909ddcd70100917/plot_train_error_vs_test_error.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,29 @@
1515
# Author: Alexandre Gramfort <alexandre.gramfort@inria.fr>
1616
# License: BSD 3 clause
1717

18+
# %%
19+
# Generate sample data
20+
# --------------------
1821
import numpy as np
1922
from sklearn import linear_model
23+
from sklearn.datasets import make_regression
24+
from sklearn.model_selection import train_test_split
2025

21-
# #############################################################################
22-
# Generate sample data
2326
n_samples_train, n_samples_test, n_features = 75, 150, 500
24-
np.random.seed(0)
25-
coef = np.random.randn(n_features)
26-
coef[50:] = 0.0 # only the top 10 features are impacting the model
27-
X = np.random.randn(n_samples_train + n_samples_test, n_features)
28-
y = np.dot(X, coef)
29-
30-
# Split train and test data
31-
X_train, X_test = X[:n_samples_train], X[n_samples_train:]
32-
y_train, y_test = y[:n_samples_train], y[n_samples_train:]
33-
34-
# #############################################################################
27+
X, y, coef = make_regression(
28+
n_samples=n_samples_train + n_samples_test,
29+
n_features=n_features,
30+
n_informative=50,
31+
shuffle=False,
32+
noise=1.0,
33+
coef=True,
34+
)
35+
X_train, X_test, y_train, y_test = train_test_split(
36+
X, y, train_size=n_samples_train, test_size=n_samples_test, shuffle=False
37+
)
38+
# %%
3539
# Compute train and test errors
40+
# -----------------------------
3641
alphas = np.logspace(-5, 1, 60)
3742
enet = linear_model.ElasticNet(l1_ratio=0.7, max_iter=10000)
3843
train_errors = list()
@@ -51,8 +56,9 @@
5156
enet.set_params(alpha=alpha_optim)
5257
coef_ = enet.fit(X, y).coef_
5358

54-
# #############################################################################
59+
# %%
5560
# Plot results functions
61+
# ----------------------
5662

5763
import matplotlib.pyplot as plt
5864

dev/_downloads/scikit-learn-docs.zip

18.6 KB
Binary file not shown.
-179 Bytes
112 Bytes
304 Bytes
0 Bytes
-5 Bytes
89 Bytes
-44 Bytes
37 Bytes
888 Bytes
106 Bytes
-388 Bytes
-82 Bytes
151 Bytes
103 Bytes
-175 Bytes
-175 Bytes
69 Bytes
-246 Bytes
0 Bytes
-143 Bytes
-23 Bytes
-159 Bytes
331 Bytes
-164 Bytes
-643 Bytes
-40 Bytes

dev/_sources/auto_examples/applications/plot_cyclical_feature_engineering.rst.txt

Lines changed: 1 addition & 1 deletion

dev/_sources/auto_examples/applications/plot_digits_denoising.rst.txt

Lines changed: 1 addition & 1 deletion

dev/_sources/auto_examples/applications/plot_face_recognition.rst.txt

Lines changed: 5 additions & 5 deletions

dev/_sources/auto_examples/applications/plot_model_complexity_influence.rst.txt

Lines changed: 15 additions & 15 deletions

0 commit comments

Comments
 (0)