8000 Revert "EXA Use stem plot for ElasticNet and Lasso coefficients (#134… · xhluca/scikit-learn@f9d117d · GitHub
[go: up one dir, main page]

Skip to content

Commit f9d117d

Browse files
author
Xing
authored
Revert "EXA Use stem plot for ElasticNet and Lasso coefficients (scikit-learn#13435)"
This reverts commit fa10650.
1 parent b0511de commit f9d117d

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

examples/linear_model/plot_lasso_and_elasticnet.py

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

22-
n_samples, n_features = 50, 100
22+
n_samples, n_features = 50, 200
2323
X = np.random.randn(n_samples, n_features)
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
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
2928
y = np.dot(X, coef)
3029

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

3433
# Split data in train set and test set
@@ -59,16 +58,12 @@
5958
print(enet)
6059
print("r^2 on test data : %f" % r2_score_enet)
6160

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-
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')
7166
plt.legend(loc='best')
72-
plt.title("Lasso $R^2$: %.3f, Elastic Net $R^2$: %.3f"
67+
plt.title("Lasso R^2: %f, Elastic Net R^2: %f"
7368
% (r2_score_lasso, r2_score_enet))
7469
plt.show()

0 commit comments

Comments
 (0)
0