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+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"id": "896cacc6",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [
|
| 9 |
+
{
|
| 10 |
+
"name": "stdout",
|
| 11 |
+
"output_type": "stream",
|
| 12 |
+
"text": [
|
| 13 |
+
"Running on local URL: http://127.0.0.1:7860\n",
|
| 14 |
+
"\n",
|
| 15 |
+
"To create a public link, set `share=True` in `launch()`.\n"
|
| 16 |
+
]
|
| 17 |
+
},
|
| 18 |
+
{
|
| 19 |
+
"data": {
|
| 20 |
+
"text/html": [
|
| 21 |
+
"<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
| 22 |
+
],
|
| 23 |
+
"text/plain": [
|
| 24 |
+
"<IPython.core.display.HTML object>"
|
| 25 |
+
]
|
| 26 |
+
},
|
| 27 |
+
"metadata": {},
|
| 28 |
+
"output_type": "display_data"
|
| 29 |
+
},
|
| 30 |
+
{
|
| 31 |
+
"data": {
|
| 32 |
+
"text/plain": []
|
| 33 |
+
},
|
| 34 |
+
"execution_count": 1,
|
| 35 |
+
"metadata": {},
|
| 36 |
+
"output_type": "execute_result"
|
| 37 |
+
}
|
| 38 |
+
],
|
| 39 |
+
"source": [
|
| 40 |
+
"import numpy as np\n",
|
| 41 |
+
"import matplotlib.pyplot as plt\n",
|
| 42 |
+
"from sklearn.linear_model import MultiTaskLasso, Lasso\n",
|
| 43 |
+
"import gradio as gr\n",
|
| 44 |
+
"\n",
|
| 45 |
+
"rng = np.random.RandomState(42)\n",
|
| 46 |
+
"\n",
|
| 47 |
+
"# Generate some 2D coefficients with sine waves with random frequency and phase\n",
|
| 48 |
+
"def make_plot(n_samples, n_features, n_tasks, n_relevant_features, alpha):\n",
|
| 49 |
+
" \n",
|
| 50 |
+
" coef = np.zeros((n_tasks, n_features))\n",
|
| 51 |
+
" times = np.linspace(0, 2 * np.pi, n_tasks)\n",
|
| 52 |
+
" for k in range(n_relevant_features):\n",
|
| 53 |
+
" coef[:, k] = np.sin((1.0 + rng.randn(1)) * times + 3 * rng.randn(1))\n",
|
| 54 |
+
" \n",
|
| 55 |
+
" X = rng.randn(n_samples, n_features)\n",
|
| 56 |
+
" Y = np.dot(X, coef.T) + rng.randn(n_samples, n_tasks)\n",
|
| 57 |
+
" \n",
|
| 58 |
+
" coef_lasso_ = np.array([Lasso(alpha=0.5).fit(X, y).coef_ for y in Y.T])\n",
|
| 59 |
+
" coef_multi_task_lasso_ = MultiTaskLasso(alpha=alpha).fit(X, Y).coef_\n",
|
| 60 |
+
" \n",
|
| 61 |
+
" fig = plt.figure(figsize=(8, 5))\n",
|
| 62 |
+
" \n",
|
| 63 |
+
" feature_to_plot = 0\n",
|
| 64 |
+
" fig = plt.figure()\n",
|
| 65 |
+
" lw = 2\n",
|
| 66 |
+
" plt.plot(coef[:, feature_to_plot], color=\"seagreen\", linewidth=lw, label=\"Ground truth\")\n",
|
| 67 |
+
" plt.plot(\n",
|
| 68 |
+
" coef_lasso_[:, feature_to_plot], color=\"cornflowerblue\", linewidth=lw, label=\"Lasso\"\n",
|
| 69 |
+
" )\n",
|
| 70 |
+
" plt.plot(\n",
|
| 71 |
+
" coef_multi_task_lasso_[:, feature_to_plot],\n",
|
| 72 |
+
" color=\"gold\",\n",
|
| 73 |
+
" linewidth=lw,\n",
|
| 74 |
+
" label=\"MultiTaskLasso\",\n",
|
| 75 |
+
" )\n",
|
| 76 |
+
" plt.legend(loc=\"upper center\")\n",
|
| 77 |
+
" plt.axis(\"tight\")\n",
|
| 78 |
+
" plt.ylim([-1.1, 1.1])\n",
|
| 79 |
+
" fig.suptitle(\"Lasso, MultiTaskLasso and Ground truth time series\")\n",
|
| 80 |
+
" return fig\n",
|
| 81 |
+
" \n",
|
| 82 |
+
" \n",
|
| 83 |
+
"model_card=f\"\"\"\n",
|
| 84 |
+
"## Description\n",
|
| 85 |
+
"The multi-task lasso allows to fit multiple regression problems jointly enforcing the selected\n",
|
| 86 |
+
"features to be the same across tasks. This example simulates sequential measurements, each task \n",
|
| 87 |
+
"is a time instant, and the relevant features vary in amplitude over time while being the same. \n",
|
| 88 |
+
"The multi-task lasso imposes that features that are selected at one time point are select \n",
|
| 89 |
+
"for all time point. This makes feature selection by the Lasso more stable.\n",
|
| 90 |
+
"## Model\n",
|
| 91 |
+
"currentmodule: sklearn.linear_model\n",
|
| 92 |
+
"class:`Lasso` and class: `MultiTaskLasso` are used in this example.\n",
|
| 93 |
+
"Plots represent Lasso, MultiTaskLasso and Ground truth time series\n",
|
| 94 |
+
"\"\"\"\n",
|
| 95 |
+
"\n",
|
| 96 |
+
"with gr.Blocks() as demo:\n",
|
| 97 |
+
" gr.Markdown('''\n",
|
| 98 |
+
" <div>\n",
|
| 99 |
+
" <h1 style='text-align: center'> Joint feature selection with multi-task Lasso </h1>\n",
|
| 100 |
+
" </div>\n",
|
| 101 |
+
" ''')\n",
|
| 102 |
+
" gr.Markdown(model_card)\n",
|
| 103 |
+
" gr.Markdown(\"Original example Author: Alexandre Gramfort <[email protected]>\")\n",
|
| 104 |
+
" gr.Markdown(\n",
|
| 105 |
+
" \"Iterative conversion by: <a href=\\\"https://github.com/DeaMariaLeon\\\">Dea María Léon</a>\"\n",
|
| 106 |
+
" )\n",
|
| 107 |
+
" n_samples = gr.Slider(50,500,value=100,step=50,label='Select number of samples')\n",
|
| 108 |
+
" n_features = gr.Slider(5,50,value=30,step=5,label='Select number of features')\n",
|
| 109 |
+
" n_tasks = gr.Slider(5,50,value=40,step=5,label='Select number of tasks')\n",
|
| 110 |
+
" n_relevant_features = gr.Slider(1,10,value=5,step=1,label='Select number of relevant_features')\n",
|
| 111 |
+
" with gr.Column():\n",
|
| 112 |
+
" with gr.Tab('Select Alpha Range'):\n",
|
| 113 |
+
" alpha = gr.Slider(0,10,value=1.0,step=0.5,label='alpha')\n",
|
| 114 |
+
" \n",
|
| 115 |
+
" btn = gr.Button(value = 'Submit')\n",
|
| 116 |
+
"\n",
|
| 117 |
+
" btn.click(make_plot,inputs=[n_samples,n_features, n_tasks, n_relevant_features, alpha],outputs=[gr.Plot()])\n",
|
| 118 |
+
"\n",
|
| 119 |
+
"demo.launch()"
|
| 120 |
+
]
|
| 121 |
+
},
|
| 122 |
+
{
|
| 123 |
+
"cell_type": "code",
|
| 124 |
+
"execution_count": null,
|
| 125 |
+
"id": "c8043d31",
|
| 126 |
+
"metadata": {},
|
| 127 |
+
"outputs": [],
|
| 128 |
+
"source": []
|
| 129 |
+
}
|
| 130 |
+
],
|
| 131 |
+
"metadata": {
|
| 132 |
+
"kernelspec": {
|
| 133 |
+
"display_name": "scikit-ex",
|
| 134 |
+
"language": "python",
|
| 135 |
+
"name": "scikit-ex"
|
| 136 |
+
},
|
| 137 |
+
"language_info": {
|
| 138 |
+
"codemirror_mode": {
|
| 139 |
+
"name": "ipython",
|
| 140 |
+
"version": 3
|
| 141 |
+
},
|
| 142 |
+
"file_extension": ".py",
|
| 143 |
+
"mimetype": "text/x-python",
|
| 144 |
+
"name": "python",
|
| 145 |
+
"nbconvert_exporter": "python",
|
| 146 |
+
"pygments_lexer": "ipython3",
|
| 147 |
+
"version": "3.11.2"
|
| 148 |
+
}
|
| 149 |
+
},
|
| 150 |
+
"nbformat": 4,
|
| 151 |
+
"nbformat_minor": 5
|
| 152 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
matplotlib==3.6.3
|
| 2 |
+
scikit-learn==1.2.1
|