.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/linear_model/plot_multi_task_lasso_support.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via JupyterLite or Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_linear_model_plot_multi_task_lasso_support.py: ============================================= Joint feature selection with multi-task Lasso ============================================= The multi-task lasso allows to fit multiple regression problems jointly enforcing the selected features to be the same across tasks. This example simulates sequential measurements, each task is a time instant, and the relevant features vary in amplitude over time while being the same. The multi-task lasso imposes that features that are selected at one time point are select for all time point. This makes feature selection by the Lasso more stable. .. GENERATED FROM PYTHON SOURCE LINES 15-19 .. code-block:: Python # Authors: The scikit-learn developers # SPDX-License-Identifier: BSD-3-Clause .. GENERATED FROM PYTHON SOURCE LINES 20-22 Generate data ------------- .. GENERATED FROM PYTHON SOURCE LINES 22-38 .. code-block:: Python import numpy as np rng = np.random.RandomState(42) # Generate some 2D coefficients with sine waves with random frequency and phase n_samples, n_features, n_tasks = 100, 30, 40 n_relevant_features = 5 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) .. GENERATED FROM PYTHON SOURCE LINES 39-41 Fit models ---------- .. GENERATED FROM PYTHON SOURCE LINES 41-47 .. code-block:: Python from sklearn.linear_model import Lasso, MultiTaskLasso coef_lasso_ = np.array([Lasso(alpha=0.5).fit(X, y).coef_ for y in Y.T]) coef_multi_task_lasso_ = MultiTaskLasso(alpha=1.0).fit(X, Y).coef_ .. GENERATED FROM PYTHON SOURCE LINES 48-50 Plot support and time series ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 50-83 .. code-block:: Python import matplotlib.pyplot as plt fig = plt.figure(figsize=(8, 5)) plt.subplot(1, 2, 1) plt.spy(coef_lasso_) plt.xlabel("Feature") plt.ylabel("Time (or Task)") plt.text(10, 5, "Lasso") plt.subplot(1, 2, 2) plt.spy(coef_multi_task_lasso_) plt.xlabel("Feature") plt.ylabel("Time (or Task)") plt.text(10, 5, "MultiTaskLasso") fig.suptitle("Coefficient non-zero location") feature_to_plot = 0 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.axis("tight") plt.ylim([-1.1, 1.1]) plt.show() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/linear_model/images/sphx_glr_plot_multi_task_lasso_support_001.png :alt: Coefficient non-zero location :srcset: /auto_examples/linear_model/images/sphx_glr_plot_multi_task_lasso_support_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/linear_model/images/sphx_glr_plot_multi_task_lasso_support_002.png :alt: plot multi task lasso support :srcset: /auto_examples/linear_model/images/sphx_glr_plot_multi_task_lasso_support_002.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.234 seconds) .. _sphx_glr_download_auto_examples_linear_model_plot_multi_task_lasso_support.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-learn/scikit-learn/1.6.X?urlpath=lab/tree/notebooks/auto_examples/linear_model/plot_multi_task_lasso_support.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/linear_model/plot_multi_task_lasso_support.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_multi_task_lasso_support.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_multi_task_lasso_support.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_multi_task_lasso_support.zip ` .. include:: plot_multi_task_lasso_support.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_