.. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_grouplasso.py: Joint feature selection with the Group Lasso ============================================ The GroupLasso estimates a set of sparse coefficients for multiple regression models that share the same non-zero features. All features (variable) are either zero for all tasks or for None of them. The `GroupLasso` takes a 3D `X (n_tasks, n_samples, n_features)` and a 2D `y (n_tasks, n_samples)`. If the design matrix `X` of the data is the same for all tasks, we recommand using scikit-learn's MultiTaskLasso. The Group Lasso solves the optimization problem:: (1 / (2 * n_samples)) * ||Y - XW||_Fro^2 + alpha * ||W||_21 .. code-block:: default # Author: Hicham Janati (hicham.janati@inria.fr) # # License: BSD (3-clause) import numpy as np from matplotlib import pyplot as plt from mutar import GroupLasso Generate multi-task data .. code-block:: default rng = np.random.RandomState(42) n_tasks, n_samples, n_features = 10, 100, 30 X = rng.randn(n_tasks, n_samples, n_features) coef = np.zeros((n_features, n_tasks)) # The features 0, 2, 4, 6 are shared for all 10 tasks coef[:7:2] = rng.randn(4, n_tasks) # We pick some additional features for tasks 1 and 3 coef[[10, 20], [1, 3]] = rng.randn(2) y = np.array([x.dot(c) for x, c in zip(X, coef.T)]) # add noise y += 0.2 * np.std(y) + rng.randn(n_tasks, n_samples) Group Lasso fit .. code-block:: default alpha = 0.5 gl = GroupLasso(alpha=alpha) gl.fit(X, y) Plot the supports of the true and obtained coefficients. .. code-block:: default f, axes = plt.subplots(1, 2, figsize=(12, 4)) for ax, coef, name in zip(axes, [coef, gl.coef_], ["True", "GroupLasso"]): ax.imshow(coef != 0) ax.set_title(name) ax.set_xlabel("Tasks") ax.set_ylabel("Features") plt.show() .. image:: /auto_examples/images/sphx_glr_plot_grouplasso_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 3.641 seconds) .. _sphx_glr_download_auto_examples_plot_grouplasso.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download :download:`Download Python source code: plot_grouplasso.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: plot_grouplasso.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_