
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/linear_model/plot_lasso_and_elasticnet.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_auto_examples_linear_model_plot_lasso_and_elasticnet.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_linear_model_plot_lasso_and_elasticnet.py:


========================================
Lasso and Elastic Net for Sparse Signals
========================================

Estimates Lasso and Elastic-Net regression models on a manually generated
sparse signal corrupted with an additive noise. Estimated coefficients are
compared with the ground-truth.

.. GENERATED FROM PYTHON SOURCE LINES 13-15

Data Generation
---------------------------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 15-40

.. code-block:: default


    import numpy as np
    import matplotlib.pyplot as plt

    from sklearn.metrics import r2_score

    np.random.seed(42)

    n_samples, n_features = 50, 100
    X = np.random.randn(n_samples, n_features)

    # Decreasing coef w. alternated signs for visualization
    idx = np.arange(n_features)
    coef = (-1) ** idx * np.exp(-idx / 10)
    coef[10:] = 0  # sparsify coef
    y = np.dot(X, coef)

    # Add noise
    y += 0.01 * np.random.normal(size=n_samples)

    # Split data in train set and test set
    n_samples = X.shape[0]
    X_train, y_train = X[: n_samples // 2], y[: n_samples // 2]
    X_test, y_test = X[n_samples // 2 :], y[n_samples // 2 :]








.. GENERATED FROM PYTHON SOURCE LINES 41-43

Lasso
---------------------------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 43-54

.. code-block:: default


    from sklearn.linear_model import Lasso

    alpha = 0.1
    lasso = Lasso(alpha=alpha)

    y_pred_lasso = lasso.fit(X_train, y_train).predict(X_test)
    r2_score_lasso = r2_score(y_test, y_pred_lasso)
    print(lasso)
    print("r^2 on test data : %f" % r2_score_lasso)





.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    Lasso(alpha=0.1)
    r^2 on test data : 0.658064




.. GENERATED FROM PYTHON SOURCE LINES 55-57

ElasticNet
---------------------------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 57-68

.. code-block:: default


    from sklearn.linear_model import ElasticNet

    enet = ElasticNet(alpha=alpha, l1_ratio=0.7)

    y_pred_enet = enet.fit(X_train, y_train).predict(X_test)
    r2_score_enet = r2_score(y_test, y_pred_enet)
    print(enet)
    print("r^2 on test data : %f" % r2_score_enet)






.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    ElasticNet(alpha=0.1, l1_ratio=0.7)
    r^2 on test data : 0.642515




.. GENERATED FROM PYTHON SOURCE LINES 69-71

Plot
---------------------------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 71-98

.. code-block:: default


    m, s, _ = plt.stem(
        np.where(enet.coef_)[0],
        enet.coef_[enet.coef_ != 0],
        markerfmt="x",
        label="Elastic net coefficients",
    )
    plt.setp([m, s], color="#2ca02c")
    m, s, _ = plt.stem(
        np.where(lasso.coef_)[0],
        lasso.coef_[lasso.coef_ != 0],
        markerfmt="x",
        label="Lasso coefficients",
    )
    plt.setp([m, s], color="#ff7f0e")
    plt.stem(
        np.where(coef)[0],
        coef[coef != 0],
        label="true coefficients",
        markerfmt="bx",
    )

    plt.legend(loc="best")
    plt.title(
        "Lasso $R^2$: %.3f, Elastic Net $R^2$: %.3f" % (r2_score_lasso, r2_score_enet)
    )
    plt.show()



.. image-sg:: /auto_examples/linear_model/images/sphx_glr_plot_lasso_and_elasticnet_001.png
   :alt: Lasso $R^2$: 0.658, Elastic Net $R^2$: 0.643
   :srcset: /auto_examples/linear_model/images/sphx_glr_plot_lasso_and_elasticnet_001.png
   :class: sphx-glr-single-img






.. rst-class:: sphx-glr-timing

   **Total running time of the script:** ( 0 minutes  0.091 seconds)


.. _sphx_glr_download_auto_examples_linear_model_plot_lasso_and_elasticnet.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download sphx-glr-download-python

     :download:`Download Python source code: plot_lasso_and_elasticnet.py <plot_lasso_and_elasticnet.py>`



  .. container:: sphx-glr-download sphx-glr-download-jupyter

     :download:`Download Jupyter notebook: plot_lasso_and_elasticnet.ipynb <plot_lasso_and_elasticnet.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
