.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_group_lasso.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. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_group_lasso.py: =============================================================== Run GroupLasso and GroupLasso CV for structured sparse recovery =============================================================== The example runs the GroupLasso scikit-learn like estimators. .. GENERATED FROM PYTHON SOURCE LINES 8-31 .. code-block:: Python import numpy as np import matplotlib.pyplot as plt from celer import GroupLassoCV, LassoCV from celer.datasets import make_correlated_data from celer.plot_utils import configure_plt print(__doc__) configure_plt(fontsize=16) # Generating X, y, and true regression coefs with 4 groups of 5 non-zero values n_samples, n_features = 100, 50 w_true = np.zeros(n_features) w_true[:5] = 1 w_true[10:15] = 1 w_true[30:35] = -1 w_true[45:] = 1 X, y, w_true = make_correlated_data( n_samples, n_features, w_true=w_true, snr=5, random_state=0) .. rst-class:: sphx-glr-script-out .. code-block:: none usetex mode requires TeX. .. GENERATED FROM PYTHON SOURCE LINES 32-33 Get group Lasso's optimal alpha for prediction by cross validation .. GENERATED FROM PYTHON SOURCE LINES 33-58 .. code-block:: Python groups = 5 # groups are contiguous and of size 5 # irregular groups are also supported, group_lasso = GroupLassoCV(groups=groups) group_lasso.fit(X, y) print("Estimated regularization parameter alpha: %s" % group_lasso.alpha_) fig = plt.figure(figsize=(6, 3), constrained_layout=True) plt.semilogx(group_lasso.alphas_, group_lasso.mse_path_, ':') plt.semilogx(group_lasso.alphas_, group_lasso.mse_path_.mean(axis=-1), 'k', label='Average across the folds', linewidth=2) plt.axvline(group_lasso.alpha_, linestyle='--', color='k', label='alpha: CV estimate') plt.legend() plt.xlabel(r'$\alpha$') plt.ylabel('Mean square prediction error') plt.show(block=False) lasso = LassoCV().fit(X, y) .. image-sg:: /auto_examples/images/sphx_glr_plot_group_lasso_001.png :alt: plot group lasso :srcset: /auto_examples/images/sphx_glr_plot_group_lasso_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Estimated regularization parameter alpha: 0.2750549815570016 .. GENERATED FROM PYTHON SOURCE LINES 59-60 Show optimal regression vector for prediction, obtained by cross validation .. GENERATED FROM PYTHON SOURCE LINES 60-74 .. code-block:: Python fig = plt.figure(figsize=(8, 3), constrained_layout=True) m, s, _ = plt.stem(np.where(w_true)[0], w_true[w_true != 0], label=r"true regression coefficients") labels = ["LassoCV-estimated regression coefficients", "GroupLassoCV-estimated regression coefficients"] colors = [u'#ff7f0e', u'#2ca02c'] for w, label, color in zip([lasso.coef_, group_lasso.coef_], labels, colors): m, s, _ = plt.stem(np.where(w)[0], w[w != 0], label=label, markerfmt='x') plt.setp([m, s], color=color) plt.xlabel("feature index") plt.legend(fontsize=12) plt.show(block=False) .. image-sg:: /auto_examples/images/sphx_glr_plot_group_lasso_002.png :alt: plot group lasso :srcset: /auto_examples/images/sphx_glr_plot_group_lasso_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.716 seconds) .. _sphx_glr_download_auto_examples_plot_group_lasso.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_group_lasso.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_group_lasso.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_group_lasso.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_