Dea22 commited on
Commit
84297b0
1 Parent(s): caec178

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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', bbox_to_anchor=(0.5, -0.05),
38
+ ncol=3, fancybox=True, shadow=True)
39
+ plt.axis("tight")
40
+ plt.ylim([-1.1, 1.1])
41
+ fig.suptitle("Lasso, MultiTaskLasso and Ground truth time series")
42
+ return fig
43
+
44
+
45
+ model_card=f"""
46
+ ## Description
47
+ Multi-task Lasso allows us to jointly fit multiple regression problems by enforcing the selected
48
+ features to be the same across tasks. This example simulates sequential measurement. Each task
49
+ is a time instant, and the relevant features, while being the same, vary in amplitude over time.
50
+ Multi-task lasso imposes that features that are selected at one time point are selected
51
+ for all time points. This makes feature selection more stable than by regular Lasso.
52
+ ## Model
53
+ currentmodule: sklearn.linear_model
54
+ class:`Lasso` and class: `MultiTaskLasso` are used in this example.
55
+ Plots represent Lasso, MultiTaskLasso and Ground truth time series
56
+ """
57
+
58
+ with gr.Blocks() as demo:
59
+
60
+ gr.Markdown('''
61
+ <div>
62
+ <h1 style='text-align: center'> Joint feature selection with multi-task Lasso </h1>
63
+ </div>
64
+ ''')
65
+ gr.Markdown(model_card)
66
+ gr.Markdown("Original example Author: Alexandre Gramfort <alexandre.gramfort@inria.fr>")
67
+ gr.Markdown(
68
+ "Iterative conversion by: <a href=\"https://github.com/DeaMariaLeon\">Dea María Léon</a>"
69
+ )
70
+ n_samples = gr.Slider(50,500,value=100,step=50,label='Select number of samples')
71
+ n_features = gr.Slider(5,50,value=30,step=5,label='Select number of features')
72
+ n_tasks = gr.Slider(5,50,value=40,step=5,label='Select number of tasks')
73
+ n_relevant_features = gr.Slider(1,10,value=5,step=1,label='Select number of relevant_features')
74
+ with gr.Column():
75
+ with gr.Tab('Select Alpha Range'):
76
+ alpha = gr.Slider(0,10,value=1.0,step=0.5,label='alpha')
77
+
78
+ btn = gr.Button(value = 'Submit')
79
+
80
+ btn.click(make_plot,inputs=[n_samples,n_features, n_tasks, n_relevant_features, alpha],outputs=[gr.Plot()])
81
+
82
+ demo.launch()