added progress
Browse files
app.py
CHANGED
|
@@ -2,25 +2,31 @@ 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
|
|
@@ -34,14 +40,15 @@ def make_plot(n_samples, n_features, n_tasks, n_relevant_features, alpha):
|
|
| 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
|
|
@@ -56,7 +63,7 @@ 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>
|
|
@@ -67,16 +74,17 @@ with gr.Blocks() as demo:
|
|
| 67 |
gr.Markdown(
|
| 68 |
"Iterative conversion by: <a href=\"https://www.deamarialeon.com\">Dea María Léon</a>"
|
| 69 |
)
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 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()
|
|
|
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
from sklearn.linear_model import MultiTaskLasso, Lasso
|
| 4 |
import gradio as gr
|
| 5 |
+
import time
|
| 6 |
|
| 7 |
rng = np.random.RandomState(42)
|
| 8 |
|
| 9 |
# Generate some 2D coefficients with sine waves with random frequency and phase
|
| 10 |
+
def make_plot(n_samples, n_features, n_tasks, n_relevant_features, alpha, progress=gr.Progress()):
|
| 11 |
+
|
| 12 |
+
progress(0, desc="Starting...")
|
| 13 |
+
time.sleep(1)
|
| 14 |
+
for i in progress.tqdm(range(100)):
|
| 15 |
+
time.sleep(0.1)
|
| 16 |
|
| 17 |
coef = np.zeros((n_tasks, n_features))
|
| 18 |
times = np.linspace(0, 2 * np.pi, n_tasks)
|
| 19 |
for k in range(n_relevant_features):
|
| 20 |
coef[:, k] = np.sin((1.0 + rng.randn(1)) * times + 3 * rng.randn(1))
|
| 21 |
+
|
| 22 |
X = rng.randn(n_samples, n_features)
|
| 23 |
Y = np.dot(X, coef.T) + rng.randn(n_samples, n_tasks)
|
| 24 |
+
|
| 25 |
coef_lasso_ = np.array([Lasso(alpha=0.5).fit(X, y).coef_ for y in Y.T])
|
| 26 |
coef_multi_task_lasso_ = MultiTaskLasso(alpha=alpha).fit(X, Y).coef_
|
| 27 |
+
|
| 28 |
fig = plt.figure(figsize=(8, 5))
|
| 29 |
+
|
| 30 |
feature_to_plot = 0
|
| 31 |
fig = plt.figure()
|
| 32 |
lw = 2
|
|
|
|
| 40 |
linewidth=lw,
|
| 41 |
label="MultiTaskLasso",
|
| 42 |
)
|
| 43 |
+
#plt.legend(loc="upper center")
|
| 44 |
plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),
|
| 45 |
ncol=3, fancybox=True, shadow=True)
|
| 46 |
plt.axis("tight")
|
| 47 |
plt.ylim([-1.1, 1.1])
|
| 48 |
fig.suptitle("Lasso, MultiTaskLasso and Ground truth time series")
|
| 49 |
return fig
|
| 50 |
+
|
| 51 |
+
|
| 52 |
model_card=f"""
|
| 53 |
## Description
|
| 54 |
Multi-task Lasso allows us to jointly fit multiple regression problems by enforcing the selected
|
|
|
|
| 63 |
"""
|
| 64 |
|
| 65 |
with gr.Blocks() as demo:
|
| 66 |
+
|
| 67 |
gr.Markdown('''
|
| 68 |
<div>
|
| 69 |
<h1 style='text-align: center'> Joint feature selection with multi-task Lasso </h1>
|
|
|
|
| 74 |
gr.Markdown(
|
| 75 |
"Iterative conversion by: <a href=\"https://www.deamarialeon.com\">Dea María Léon</a>"
|
| 76 |
)
|
| 77 |
+
|
| 78 |
+
with gr.Row().style(equal_height=True):
|
| 79 |
+
|
| 80 |
+
n_features = gr.Slider(5,50,value=30,step=5,label='Features')
|
| 81 |
+
n_tasks = gr.Slider(5,50,value=40,step=5,label='Tasks')
|
| 82 |
+
n_relevant_features = gr.Slider(1,10,value=5,step=1,label='Relevant features')
|
| 83 |
+
alpha = gr.Slider(0,10,value=1.0,step=0.5,label='Alpha Range')
|
| 84 |
+
n_samples = gr.Slider(50,500,value=100,step=50,label='Number of samples')
|
| 85 |
+
|
| 86 |
btn = gr.Button(value = 'Submit')
|
| 87 |
|
| 88 |
btn.click(make_plot,inputs=[n_samples,n_features, n_tasks, n_relevant_features, alpha],outputs=[gr.Plot()])
|
| 89 |
|
| 90 |
+
demo.queue().launch()
|