File size: 3,708 Bytes
84297b0 e6f71f0 84297b0 e6f71f0 84297b0 e6f71f0 84297b0 e6f71f0 84297b0 e6f71f0 84297b0 e6f71f0 84297b0 e6f71f0 84297b0 e6f71f0 312eb58 84297b0 b289808 84297b0 f97cbba 119377e e9a91ac e6f71f0 84297b0 7b8fa75 84297b0 f97cbba e6f71f0 f97cbba e6f71f0 84297b0 e6f71f0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import MultiTaskLasso, Lasso
import gradio as gr
import time
rng = np.random.RandomState(42)
# Generate some 2D coefficients with sine waves with random frequency and phase
def make_plot(n_samples, n_features, n_tasks, n_relevant_features, alpha, progress=gr.Progress()):
progress(0, desc="Starting...")
time.sleep(1)
for i in progress.tqdm(range(100)):
time.sleep(0.1)
coef = np.zeros((n_tasks, n_features))
times = np.linspace(0, 2 * np.pi, n_tasks)
for k in range(n_relevant_features):
coef[:, k] = np.sin((1.0 + rng.randn(1)) * times + 3 * rng.randn(1))
X = rng.randn(n_samples, n_features)
Y = np.dot(X, coef.T) + rng.randn(n_samples, n_tasks)
coef_lasso_ = np.array([Lasso(alpha=0.5).fit(X, y).coef_ for y in Y.T])
coef_multi_task_lasso_ = MultiTaskLasso(alpha=alpha).fit(X, Y).coef_
fig = plt.figure(figsize=(8, 5))
feature_to_plot = 0
fig = plt.figure()
lw = 2
plt.plot(coef[:, feature_to_plot], color="seagreen", linewidth=lw, label="Ground truth")
plt.plot(
coef_lasso_[:, feature_to_plot], color="cornflowerblue", linewidth=lw, label="Lasso"
)
plt.plot(
coef_multi_task_lasso_[:, feature_to_plot],
color="gold",
linewidth=lw,
label="MultiTaskLasso",
)
#plt.legend(loc="upper center")
plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),
ncol=3, fancybox=True, shadow=True)
plt.axis("tight")
plt.ylim([-1.1, 1.1])
fig.suptitle("Lasso, MultiTaskLasso and Ground truth time series")
return fig
model_card = f"""
## Description
Multi-task Lasso allows us to jointly fit multiple regression problems by enforcing the selected features to be the same across tasks. This example simulates sequential measurement.
Each task is a time instant, and the relevant features, while being the same, vary in amplitude over time. Multi-task lasso imposes that features that are selected at one time point
are selected for all time points. This makes feature selection more stable than by regular Lasso.
## Model
currentmodule: sklearn.linear_model
class:`Lasso` and class: `MultiTaskLasso` are used in this example.
Plots represent Lasso, MultiTaskLasso and Ground truth time series
"""
with gr.Blocks(theme=gr.themes.Glass(primary_hue=gr.themes.colors.gray,
secondary_hue=gr.themes.colors.sky,
text_size=gr.themes.sizes.text_lg),
css=".gradio-container {background-color: #9ea9a9 }") as demo:
gr.Markdown('''
<div>
<h1 style='text-align: center'> Joint feature selection with multi-task Lasso </h1>
</div>
''')
gr.Markdown(model_card)
gr.Markdown("Original example Author: Alexandre Gramfort <alexandre.gramfort@inria.fr>")
gr.Markdown(
"Iterative conversion by: <a href=\"https://www.deamarialeon.com\">Dea María Léon</a>"
)
gr.Markdown("### Please select values and click submit:")
with gr.Row().style(equal_height=True):
n_samples = gr.Slider(50,500,value=100,step=50,label='Number of samples')
n_features = gr.Slider(5,50,value=30,step=5,label='Features')
n_tasks = gr.Slider(5,50,value=40,step=5,label='Tasks')
n_relevant_features = gr.Slider(1,10,value=5,step=1,label='Relevant features')
alpha = gr.Slider(0,10,value=1.0,step=0.5,label='Alpha Range')
btn = gr.Button(value = 'Submit')
btn.click(make_plot,inputs=[n_samples,n_features, n_tasks, n_relevant_features, alpha],outputs=[gr.Plot()])
demo.queue().launch() |