Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
|
5 |
+
from sklearn.metrics import r2_score
|
6 |
+
from sklearn.linear_model import Lasso, ElasticNet
|
7 |
+
|
8 |
+
|
9 |
+
theme = gr.themes.Monochrome(
|
10 |
+
primary_hue="indigo",
|
11 |
+
secondary_hue="blue",
|
12 |
+
neutral_hue="slate",
|
13 |
+
)
|
14 |
+
model_card = f"""
|
15 |
+
## Description
|
16 |
+
|
17 |
+
This demo estimates **Lasso** and **Elastic-Net** regression models on a manually generated sparse signal corrupted with an additive noise.
|
18 |
+
You can play around with different ``regularization strength``, ``mixing ratio between L1 and L2``, ``number of samples``, ``number of features`` to see the effect
|
19 |
+
|
20 |
+
## Dataset
|
21 |
+
|
22 |
+
Simulation dataset
|
23 |
+
"""
|
24 |
+
|
25 |
+
|
26 |
+
|
27 |
+
def do_train(alpha, l1_ratio, n_samples, n_features):
|
28 |
+
np.random.seed(42)
|
29 |
+
X = np.random.randn(n_samples, n_features)
|
30 |
+
|
31 |
+
# Decreasing coef w. alternated signs for visualization
|
32 |
+
idx = np.arange(n_features)
|
33 |
+
coef = (-1) ** idx * np.exp(-idx / 10)
|
34 |
+
coef[10:] = 0 # sparsify coef
|
35 |
+
y = np.dot(X, coef)
|
36 |
+
|
37 |
+
# Add noise
|
38 |
+
y += 0.01 * np.random.normal(size=n_samples)
|
39 |
+
|
40 |
+
# Split data in train set and test set
|
41 |
+
n_samples = X.shape[0]
|
42 |
+
X_train, y_train = X[: n_samples // 2], y[: n_samples // 2]
|
43 |
+
X_test, y_test = X[n_samples // 2 :], y[n_samples // 2 :]
|
44 |
+
|
45 |
+
lasso = Lasso(alpha=alpha)
|
46 |
+
y_pred_lasso = lasso.fit(X_train, y_train).predict(X_test)
|
47 |
+
r2_score_lasso = r2_score(y_test, y_pred_lasso)
|
48 |
+
|
49 |
+
|
50 |
+
enet = ElasticNet(alpha=alpha, l1_ratio=l1_ratio)
|
51 |
+
y_pred_enet = enet.fit(X_train, y_train).predict(X_test)
|
52 |
+
r2_score_enet = r2_score(y_test, y_pred_enet)
|
53 |
+
|
54 |
+
fig, axes = plt.subplots()
|
55 |
+
|
56 |
+
m, s, _ = axes.stem(
|
57 |
+
np.where(enet.coef_)[0],
|
58 |
+
enet.coef_[enet.coef_ != 0],
|
59 |
+
markerfmt="x",
|
60 |
+
label="Elastic net coefficients",
|
61 |
+
)
|
62 |
+
plt.setp([m, s], color="#2ca02c")
|
63 |
+
|
64 |
+
m, s, _ = plt.stem(
|
65 |
+
np.where(lasso.coef_)[0],
|
66 |
+
lasso.coef_[lasso.coef_ != 0],
|
67 |
+
markerfmt="x",
|
68 |
+
label="Lasso coefficients",
|
69 |
+
)
|
70 |
+
plt.setp([m, s], color="#ff7f0e")
|
71 |
+
|
72 |
+
axes.stem(
|
73 |
+
np.where(coef)[0],
|
74 |
+
coef[coef != 0],
|
75 |
+
label="True coefficients",
|
76 |
+
markerfmt="bx",
|
77 |
+
)
|
78 |
+
|
79 |
+
axes.legend(loc="best")
|
80 |
+
axes.set_title("Elastic net and Lasso coefficients")
|
81 |
+
text = f"Lasso R^2: {r2_score_lasso:.3f}, Elastic Net R^2: {r2_score_enet:.3f}"
|
82 |
+
return fig, text
|
83 |
+
|
84 |
+
|
85 |
+
|
86 |
+
with gr.Blocks(theme=theme) as demo:
|
87 |
+
gr.Markdown('''
|
88 |
+
<div>
|
89 |
+
<h1 style='text-align: center'>Lasso and Elastic Net for Sparse Signals</h1>
|
90 |
+
</div>
|
91 |
+
''')
|
92 |
+
gr.Markdown(model_card)
|
93 |
+
gr.Markdown("Author: <a href=\"https://huggingface.co/vumichien\">Vu Minh Chien</a>. Based on the example from <a href=\"https://scikit-learn.org/stable/auto_examples/linear_model/plot_lasso_and_elasticnet.html#sphx-glr-auto-examples-linear-model-plot-lasso-and-elasticnet-py\">scikit-learn</a>")
|
94 |
+
alpha = gr.Slider(minimum=0, maximum=1, step=0.1, value=0.1, label="Constant that multiplies the L1 term, controlling regularization strength. Using alpha = 0 with the Lasso object is not advised")
|
95 |
+
l1_ratio = gr.Slider(minimum=0, maximum=1, step=0.1, value=0.7, label="The ElasticNet mixing parameter. For l1_ratio = 0 the penalty is an L2 penalty. For l1_ratio = 1 it is an L1 penalty. For 0 < l1_ratio < 1, the penalty is a combination of L1 and L2.")
|
96 |
+
n_samples = gr.Slider(minimum=50, maximum=500, step=50, value=50, label="Number of samples")
|
97 |
+
n_features = gr.Slider(minimum=50, maximum=200, step=50, value=50, label="Number of features")
|
98 |
+
with gr.Row():
|
99 |
+
with gr.Column():
|
100 |
+
plot = gr.Plot(label="Coefficients plot")
|
101 |
+
with gr.Column():
|
102 |
+
results = gr.Textbox(label="Results")
|
103 |
+
|
104 |
+
alpha.change(fn=do_train, inputs=[alpha, l1_ratio, n_samples, n_features], outputs=[plot, results])
|
105 |
+
l1_ratio.change(fn=do_train, inputs=[alpha, l1_ratio, n_samples, n_features], outputs=[plot, results])
|
106 |
+
n_samples.change(fn=do_train, inputs=[alpha, l1_ratio, n_samples, n_features], outputs=[plot, results])
|
107 |
+
n_features.change(fn=do_train, inputs=[alpha, l1_ratio, n_samples, n_features], outputs=[plot, results])
|
108 |
+
|
109 |
+
demo.launch()
|