Skip to content

Commit 30ee0c9

Browse files
committed
Pushing the docs to dev/ for branch: main, commit c4be686fad50aa19b7c72e52d717d5b7ca40d123
1 parent 22c5ee1 commit 30ee0c9

File tree

1,219 files changed

+4663
-4461
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,219 files changed

+4663
-4461
lines changed
Binary file not shown.

dev/_downloads/3d58721191491072eecc520f0a45cdb3/plot_lasso_and_elasticnet.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
1010
"""
1111

12+
# %%
13+
# Data Generation
14+
# ---------------------------------------------------
15+
1216
import numpy as np
1317
import matplotlib.pyplot as plt
1418

1519
from sklearn.metrics import r2_score
1620

17-
# #############################################################################
18-
# Generate some sparse data to play with
1921
np.random.seed(42)
2022

2123
n_samples, n_features = 50, 100
@@ -35,8 +37,10 @@
3537
X_train, y_train = X[: n_samples // 2], y[: n_samples // 2]
3638
X_test, y_test = X[n_samples // 2 :], y[n_samples // 2 :]
3739

38-
# #############################################################################
40+
# %%
3941
# Lasso
42+
# ---------------------------------------------------
43+
4044
from sklearn.linear_model import Lasso
4145

4246
alpha = 0.1
@@ -47,8 +51,10 @@
4751
print(lasso)
4852
print("r^2 on test data : %f" % r2_score_lasso)
4953

50-
# #############################################################################
54+
# %%
5155
# ElasticNet
56+
# ---------------------------------------------------
57+
5258
from sklearn.linear_model import ElasticNet
5359

5460
enet = ElasticNet(alpha=alpha, l1_ratio=0.7)
@@ -58,6 +64,11 @@
5864
print(enet)
5965
print("r^2 on test data : %f" % r2_score_enet)
6066

67+
68+
# %%
69+
# Plot
70+
# ---------------------------------------------------
71+
6172
m, s, _ = plt.stem(
6273
np.where(enet.coef_)[0],
6374
enet.coef_[enet.coef_ != 0],

dev/_downloads/6cd2f23417e24a8a2a445c34f1b57930/plot_lasso_and_elasticnet.ipynb

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,67 @@
1818
"\n# Lasso and Elastic Net for Sparse Signals\n\nEstimates Lasso and Elastic-Net regression models on a manually generated\nsparse signal corrupted with an additive noise. Estimated coefficients are\ncompared with the ground-truth.\n"
1919
]
2020
},
21+
{
22+
"cell_type": "markdown",
23+
"metadata": {},
24+
"source": [
25+
"## Data Generation\n\n"
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": null,
31+
"metadata": {
32+
"collapsed": false
33+
},
34+
"outputs": [],
35+
"source": [
36+
"import numpy as np\nimport matplotlib.pyplot as plt\n\nfrom sklearn.metrics import r2_score\n\nnp.random.seed(42)\n\nn_samples, n_features = 50, 100\nX = np.random.randn(n_samples, n_features)\n\n# Decreasing coef w. alternated signs for visualization\nidx = np.arange(n_features)\ncoef = (-1) ** idx * np.exp(-idx / 10)\ncoef[10:] = 0 # sparsify coef\ny = np.dot(X, coef)\n\n# Add noise\ny += 0.01 * np.random.normal(size=n_samples)\n\n# Split data in train set and test set\nn_samples = X.shape[0]\nX_train, y_train = X[: n_samples // 2], y[: n_samples // 2]\nX_test, y_test = X[n_samples // 2 :], y[n_samples // 2 :]"
37+
]
38+
},
39+
{
40+
"cell_type": "markdown",
41+
"metadata": {},
42+
"source": [
43+
"## Lasso\n\n"
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": null,
49+
"metadata": {
50+
"collapsed": false
51+
},
52+
"outputs": [],
53+
"source": [
54+
"from sklearn.linear_model import Lasso\n\nalpha = 0.1\nlasso = Lasso(alpha=alpha)\n\ny_pred_lasso = lasso.fit(X_train, y_train).predict(X_test)\nr2_score_lasso = r2_score(y_test, y_pred_lasso)\nprint(lasso)\nprint(\"r^2 on test data : %f\" % r2_score_lasso)"
55+
]
56+
},
57+
{
58+
"cell_type": "markdown",
59+
"metadata": {},
60+
"source": [
61+
"## ElasticNet\n\n"
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": null,
67+
"metadata": {
68+
"collapsed": false
69+
},
70+
"outputs": [],
71+
"source": [
72+
"from sklearn.linear_model import ElasticNet\n\nenet = ElasticNet(alpha=alpha, l1_ratio=0.7)\n\ny_pred_enet = enet.fit(X_train, y_train).predict(X_test)\nr2_score_enet = r2_score(y_test, y_pred_enet)\nprint(enet)\nprint(\"r^2 on test data : %f\" % r2_score_enet)"
73+
]
74+
},
75+
{
76+
"cell_type": "markdown",
77+
"metadata": {},
78+
"source": [
79+
"## Plot\n\n"
80+
]
81+
},
2182
{
2283
"cell_type": "code",
2384
"execution_count": null,
@@ -26,7 +87,7 @@
2687
},
2788
"outputs": [],
2889
"source": [
29-
"import numpy as np\nimport matplotlib.pyplot as plt\n\nfrom sklearn.metrics import r2_score\n\n# #############################################################################\n# Generate some sparse data to play with\nnp.random.seed(42)\n\nn_samples, n_features = 50, 100\nX = np.random.randn(n_samples, n_features)\n\n# Decreasing coef w. alternated signs for visualization\nidx = np.arange(n_features)\ncoef = (-1) ** idx * np.exp(-idx / 10)\ncoef[10:] = 0 # sparsify coef\ny = np.dot(X, coef)\n\n# Add noise\ny += 0.01 * np.random.normal(size=n_samples)\n\n# Split data in train set and test set\nn_samples = X.shape[0]\nX_train, y_train = X[: n_samples // 2], y[: n_samples // 2]\nX_test, y_test = X[n_samples // 2 :], y[n_samples // 2 :]\n\n# #############################################################################\n# Lasso\nfrom sklearn.linear_model import Lasso\n\nalpha = 0.1\nlasso = Lasso(alpha=alpha)\n\ny_pred_lasso = lasso.fit(X_train, y_train).predict(X_test)\nr2_score_lasso = r2_score(y_test, y_pred_lasso)\nprint(lasso)\nprint(\"r^2 on test data : %f\" % r2_score_lasso)\n\n# #############################################################################\n# ElasticNet\nfrom sklearn.linear_model import ElasticNet\n\nenet = ElasticNet(alpha=alpha, l1_ratio=0.7)\n\ny_pred_enet = enet.fit(X_train, y_train).predict(X_test)\nr2_score_enet = r2_score(y_test, y_pred_enet)\nprint(enet)\nprint(\"r^2 on test data : %f\" % r2_score_enet)\n\nm, s, _ = plt.stem(\n np.where(enet.coef_)[0],\n enet.coef_[enet.coef_ != 0],\n markerfmt=\"x\",\n label=\"Elastic net coefficients\",\n use_line_collection=True,\n)\nplt.setp([m, s], color=\"#2ca02c\")\nm, s, _ = plt.stem(\n np.where(lasso.coef_)[0],\n lasso.coef_[lasso.coef_ != 0],\n markerfmt=\"x\",\n label=\"Lasso coefficients\",\n use_line_collection=True,\n)\nplt.setp([m, s], color=\"#ff7f0e\")\nplt.stem(\n np.where(coef)[0],\n coef[coef != 0],\n label=\"true coefficients\",\n markerfmt=\"bx\",\n use_line_collection=True,\n)\n\nplt.legend(loc=\"best\")\nplt.title(\n \"Lasso $R^2$: %.3f, Elastic Net $R^2$: %.3f\" % (r2_score_lasso, r2_score_enet)\n)\nplt.show()"
90+
"m, s, _ = plt.stem(\n np.where(enet.coef_)[0],\n enet.coef_[enet.coef_ != 0],\n markerfmt=\"x\",\n label=\"Elastic net coefficients\",\n use_line_collection=True,\n)\nplt.setp([m, s], color=\"#2ca02c\")\nm, s, _ = plt.stem(\n np.where(lasso.coef_)[0],\n lasso.coef_[lasso.coef_ != 0],\n markerfmt=\"x\",\n label=\"Lasso coefficients\",\n use_line_collection=True,\n)\nplt.setp([m, s], color=\"#ff7f0e\")\nplt.stem(\n np.where(coef)[0],\n coef[coef != 0],\n label=\"true coefficients\",\n markerfmt=\"bx\",\n use_line_collection=True,\n)\n\nplt.legend(loc=\"best\")\nplt.title(\n \"Lasso $R^2$: %.3f, Elastic Net $R^2$: %.3f\" % (r2_score_lasso, r2_score_enet)\n)\nplt.show()"
3091
]
3192
}
3293
],
Binary file not shown.

dev/_downloads/scikit-learn-docs.zip

4.97 KB
Binary file not shown.

0 commit comments

Comments
 (0)