Upload 2 files
Browse files- app.py +80 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
from sklearn.linear_model import MultiTaskLasso, Lasso
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
rng = np.random.RandomState(42)
|
7 |
+
|
8 |
+
# Generate some 2D coefficients with sine waves with random frequency and phase
|
9 |
+
def make_plot(n_samples, n_features, n_tasks, n_relevant_features, alpha):
|
10 |
+
|
11 |
+
coef = np.zeros((n_tasks, n_features))
|
12 |
+
times = np.linspace(0, 2 * np.pi, n_tasks)
|
13 |
+
for k in range(n_relevant_features):
|
14 |
+
coef[:, k] = np.sin((1.0 + rng.randn(1)) * times + 3 * rng.randn(1))
|
15 |
+
|
16 |
+
X = rng.randn(n_samples, n_features)
|
17 |
+
Y = np.dot(X, coef.T) + rng.randn(n_samples, n_tasks)
|
18 |
+
|
19 |
+
coef_lasso_ = np.array([Lasso(alpha=0.5).fit(X, y).coef_ for y in Y.T])
|
20 |
+
coef_multi_task_lasso_ = MultiTaskLasso(alpha=alpha).fit(X, Y).coef_
|
21 |
+
|
22 |
+
fig = plt.figure(figsize=(8, 5))
|
23 |
+
|
24 |
+
feature_to_plot = 0
|
25 |
+
fig = plt.figure()
|
26 |
+
lw = 2
|
27 |
+
plt.plot(coef[:, feature_to_plot], color="seagreen", linewidth=lw, label="Ground truth")
|
28 |
+
plt.plot(
|
29 |
+
coef_lasso_[:, feature_to_plot], color="cornflowerblue", linewidth=lw, label="Lasso"
|
30 |
+
)
|
31 |
+
plt.plot(
|
32 |
+
coef_multi_task_lasso_[:, feature_to_plot],
|
33 |
+
color="gold",
|
34 |
+
linewidth=lw,
|
35 |
+
label="MultiTaskLasso",
|
36 |
+
)
|
37 |
+
plt.legend(loc="upper center")
|
38 |
+
plt.axis("tight")
|
39 |
+
plt.ylim([-1.1, 1.1])
|
40 |
+
fig.suptitle("Lasso, MultiTaskLasso and Ground truth time series")
|
41 |
+
return fig
|
42 |
+
|
43 |
+
|
44 |
+
model_card=f"""
|
45 |
+
## Description
|
46 |
+
The multi-task lasso allows to fit multiple regression problems jointly enforcing the selected
|
47 |
+
features to be the same across tasks. This example simulates sequential measurements, each task
|
48 |
+
is a time instant, and the relevant features vary in amplitude over time while being the same.
|
49 |
+
The multi-task lasso imposes that features that are selected at one time point are select
|
50 |
+
for all time point. This makes feature selection by the Lasso more stable.
|
51 |
+
## Model
|
52 |
+
currentmodule: sklearn.linear_model
|
53 |
+
class:`Lasso` and class: `MultiTaskLasso` are used in this example.
|
54 |
+
Plots represent Lasso, MultiTaskLasso and Ground truth time series
|
55 |
+
"""
|
56 |
+
|
57 |
+
with gr.Blocks() as demo:
|
58 |
+
gr.Markdown('''
|
59 |
+
<div>
|
60 |
+
<h1 style='text-align: center'> Joint feature selection with multi-task Lasso </h1>
|
61 |
+
</div>
|
62 |
+
''')
|
63 |
+
gr.Markdown(model_card)
|
64 |
+
gr.Markdown("Original example Author: Alexandre Gramfort <alexandre.gramfort@inria.fr>")
|
65 |
+
gr.Markdown(
|
66 |
+
"Iterative conversion by: <a href=\"https://github.com/DeaMariaLeon\">Dea María Léon</a>"
|
67 |
+
)
|
68 |
+
n_samples = gr.Slider(50,500,value=100,step=50,label='Select number of samples')
|
69 |
+
n_features = gr.Slider(5,50,value=30,step=5,label='Select number of features')
|
70 |
+
n_tasks = gr.Slider(5,50,value=40,step=5,label='Select number of tasks')
|
71 |
+
n_relevant_features = gr.Slider(1,10,value=5,step=1,label='Select number of relevant_features')
|
72 |
+
with gr.Column():
|
73 |
+
with gr.Tab('Select Alpha Range'):
|
74 |
+
alpha = gr.Slider(0,10,value=1.0,step=0.5,label='alpha')
|
75 |
+
|
76 |
+
btn = gr.Button(value = 'Submit')
|
77 |
+
|
78 |
+
btn.click(make_plot,inputs=[n_samples,n_features, n_tasks, n_relevant_features, alpha],outputs=[gr.Plot()])
|
79 |
+
|
80 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
matplotlib==3.6.3
|
2 |
+
scikit-learn==1.2.1
|