Skip to content

Commit 7ad4e59

Browse files
committed
Pushing the docs to dev/ for branch: master, commit ffc63c6508817112ebb5157fc36696d4bf00e53b
1 parent 80f0fb9 commit 7ad4e59

File tree

1,087 files changed

+3424
-3409
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,087 files changed

+3424
-3409
lines changed
237 Bytes
Binary file not shown.
230 Bytes
Binary file not shown.

dev/_downloads/plot_lasso_and_elasticnet.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"outputs": [],
2828
"source": [
29-
"print(__doc__)\n\nimport 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, 200\nX = np.random.randn(n_samples, n_features)\ncoef = 3 * np.random.randn(n_features)\ninds = np.arange(n_features)\nnp.random.shuffle(inds)\ncoef[inds[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\nplt.plot(enet.coef_, color='lightgreen', linewidth=2,\n label='Elastic net coefficients')\nplt.plot(lasso.coef_, color='gold', linewidth=2,\n label='Lasso coefficients')\nplt.plot(coef, '--', color='navy', label='original coefficients')\nplt.legend(loc='best')\nplt.title(\"Lasso R^2: %f, Elastic Net R^2: %f\"\n % (r2_score_lasso, r2_score_enet))\nplt.show()"
29+
"print(__doc__)\n\nimport 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(np.where(enet.coef_)[0], enet.coef_[enet.coef_ != 0],\n markerfmt='x', label='Elastic net coefficients')\nplt.setp([m, s], color=\"#2ca02c\")\nm, s, _ = plt.stem(np.where(lasso.coef_)[0], lasso.coef_[lasso.coef_ != 0],\n markerfmt='x', label='Lasso coefficients')\nplt.setp([m, s], color='#ff7f0e')\nplt.stem(np.where(coef)[0], coef[coef != 0], label='true coefficients',\n markerfmt='bx')\n\nplt.legend(loc='best')\nplt.title(\"Lasso $R^2$: %.3f, Elastic Net $R^2$: %.3f\"\n % (r2_score_lasso, r2_score_enet))\nplt.show()"
3030
]
3131
}
3232
],

dev/_downloads/plot_lasso_and_elasticnet.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@
1919
# Generate some sparse data to play with
2020
np.random.seed(42)
2121

22-
n_samples, n_features = 50, 200
22+
n_samples, n_features = 50, 100
2323
X = np.random.randn(n_samples, n_features)
24-
coef = 3 * np.random.randn(n_features)
25-
inds = np.arange(n_features)
26-
np.random.shuffle(inds)
27-
coef[inds[10:]] = 0 # sparsify coef
24+
25+
# Decreasing coef w. alternated signs for visualization
26+
idx = np.arange(n_features)
27+
coef = (-1) ** idx * np.exp(-idx / 10)
28+
coef[10:] = 0 # sparsify coef
2829
y = np.dot(X, coef)
2930

30-
# add noise
31+
# Add noise
3132
y += 0.01 * np.random.normal(size=n_samples)
3233

3334
# Split data in train set and test set
@@ -58,12 +59,16 @@
5859
print(enet)
5960
print("r^2 on test data : %f" % r2_score_enet)
6061

61-
plt.plot(enet.coef_, color='lightgreen', linewidth=2,
62-
label='Elastic net coefficients')
63-
plt.plot(lasso.coef_, color='gold', linewidth=2,
64-
label='Lasso coefficients')
65-
plt.plot(coef, '--', color='navy', label='original coefficients')
62+
m, s, _ = plt.stem(np.where(enet.coef_)[0], enet.coef_[enet.coef_ != 0],
63+
markerfmt='x', label='Elastic net coefficients')
64+
plt.setp([m, s], color="#2ca02c")
65+
m, s, _ = plt.stem(np.where(lasso.coef_)[0], lasso.coef_[lasso.coef_ != 0],
66+
markerfmt='x', label='Lasso coefficients')
67+
plt.setp([m, s], color='#ff7f0e')
68+
plt.stem(np.where(coef)[0], coef[coef != 0], label='true coefficients',
69+
markerfmt='bx')
70+
6671
plt.legend(loc='best')
67-
plt.title("Lasso R^2: %f, Elastic Net R^2: %f"
72+
plt.title("Lasso $R^2$: %.3f, Elastic Net $R^2$: %.3f"
6873
% (r2_score_lasso, r2_score_enet))
6974
plt.show()

dev/_downloads/scikit-learn-docs.pdf

-45.1 KB
Binary file not shown.

dev/_images/iris.png

0 Bytes
-132 Bytes
-132 Bytes
-334 Bytes
-334 Bytes

0 commit comments

Comments
 (0)