{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Lasso and Elastic Net for Sparse Signals\n\nEstimates Lasso and Elastic-Net regression models on a manually generated\nsparse signal corrupted with an additive noise. Estimated coefficients are\ncompared with the ground-truth.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Data Generation\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport matplotlib.pyplot as plt\n\nfrom sklearn.metrics import r2_score\n\nnp.random.seed(42)\n\nn_samples, n_features = 50, 100\nX = np.random.randn(n_samples, n_features)\n\n# Decreasing coef w. alternated signs for visualization\nidx = np.arange(n_features)\ncoef = (-1) ** idx * np.exp(-idx / 10)\ncoef[10:] = 0  # sparsify coef\ny = np.dot(X, coef)\n\n# Add noise\ny += 0.01 * np.random.normal(size=n_samples)\n\n# Split data in train set and test set\nn_samples = X.shape[0]\nX_train, y_train = X[: n_samples // 2], y[: n_samples // 2]\nX_test, y_test = X[n_samples // 2 :], y[n_samples // 2 :]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Lasso\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from sklearn.linear_model import Lasso\n\nalpha = 0.1\nlasso = Lasso(alpha=alpha)\n\ny_pred_lasso = lasso.fit(X_train, y_train).predict(X_test)\nr2_score_lasso = r2_score(y_test, y_pred_lasso)\nprint(lasso)\nprint(\"r^2 on test data : %f\" % r2_score_lasso)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## ElasticNet\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from sklearn.linear_model import ElasticNet\n\nenet = ElasticNet(alpha=alpha, l1_ratio=0.7)\n\ny_pred_enet = enet.fit(X_train, y_train).predict(X_test)\nr2_score_enet = r2_score(y_test, y_pred_enet)\nprint(enet)\nprint(\"r^2 on test data : %f\" % r2_score_enet)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Plot\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "m, s, _ = plt.stem(\n    np.where(enet.coef_)[0],\n    enet.coef_[enet.coef_ != 0],\n    markerfmt=\"x\",\n    label=\"Elastic net coefficients\",\n    use_line_collection=True,\n)\nplt.setp([m, s], color=\"#2ca02c\")\nm, s, _ = plt.stem(\n    np.where(lasso.coef_)[0],\n    lasso.coef_[lasso.coef_ != 0],\n    markerfmt=\"x\",\n    label=\"Lasso coefficients\",\n    use_line_collection=True,\n)\nplt.setp([m, s], color=\"#ff7f0e\")\nplt.stem(\n    np.where(coef)[0],\n    coef[coef != 0],\n    label=\"true coefficients\",\n    markerfmt=\"bx\",\n    use_line_collection=True,\n)\n\nplt.legend(loc=\"best\")\nplt.title(\n    \"Lasso $R^2$: %.3f, Elastic Net $R^2$: %.3f\" % (r2_score_lasso, r2_score_enet)\n)\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.9.13"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}