{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Lasso and Elastic Net\n\n\nLasso and elastic net (L1 and L2 penalisation) implemented using a\ncoordinate descent.\n\nThe coefficients can be forced to be positive.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(__doc__)\n\n# Author: Alexandre Gramfort <alexandre.gramfort@inria.fr>\n# License: BSD 3 clause\n\nfrom itertools import cycle\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom sklearn.linear_model import lasso_path, enet_path\nfrom sklearn import datasets\n\ndiabetes = datasets.load_diabetes()\nX = diabetes.data\ny = diabetes.target\n\nX /= X.std(axis=0)  # Standardize data (easier to set the l1_ratio parameter)\n\n# Compute paths\n\neps = 5e-3  # the smaller it is the longer is the path\n\nprint(\"Computing regularization path using the lasso...\")\nalphas_lasso, coefs_lasso, _ = lasso_path(X, y, eps, fit_intercept=False)\n\nprint(\"Computing regularization path using the positive lasso...\")\nalphas_positive_lasso, coefs_positive_lasso, _ = lasso_path(\n    X, y, eps, positive=True, fit_intercept=False)\nprint(\"Computing regularization path using the elastic net...\")\nalphas_enet, coefs_enet, _ = enet_path(\n    X, y, eps=eps, l1_ratio=0.8, fit_intercept=False)\n\nprint(\"Computing regularization path using the positive elastic net...\")\nalphas_positive_enet, coefs_positive_enet, _ = enet_path(\n    X, y, eps=eps, l1_ratio=0.8, positive=True, fit_intercept=False)\n\n# Display results\n\nplt.figure(1)\ncolors = cycle(['b', 'r', 'g', 'c', 'k'])\nneg_log_alphas_lasso = -np.log10(alphas_lasso)\nneg_log_alphas_enet = -np.log10(alphas_enet)\nfor coef_l, coef_e, c in zip(coefs_lasso, coefs_enet, colors):\n    l1 = plt.plot(neg_log_alphas_lasso, coef_l, c=c)\n    l2 = plt.plot(neg_log_alphas_enet, coef_e, linestyle='--', c=c)\n\nplt.xlabel('-Log(alpha)')\nplt.ylabel('coefficients')\nplt.title('Lasso and Elastic-Net Paths')\nplt.legend((l1[-1], l2[-1]), ('Lasso', 'Elastic-Net'), loc='lower left')\nplt.axis('tight')\n\n\nplt.figure(2)\nneg_log_alphas_positive_lasso = -np.log10(alphas_positive_lasso)\nfor coef_l, coef_pl, c in zip(coefs_lasso, coefs_positive_lasso, colors):\n    l1 = plt.plot(neg_log_alphas_lasso, coef_l, c=c)\n    l2 = plt.plot(neg_log_alphas_positive_lasso, coef_pl, linestyle='--', c=c)\n\nplt.xlabel('-Log(alpha)')\nplt.ylabel('coefficients')\nplt.title('Lasso and positive Lasso')\nplt.legend((l1[-1], l2[-1]), ('Lasso', 'positive Lasso'), loc='lower left')\nplt.axis('tight')\n\n\nplt.figure(3)\nneg_log_alphas_positive_enet = -np.log10(alphas_positive_enet)\nfor (coef_e, coef_pe, c) in zip(coefs_enet, coefs_positive_enet, colors):\n    l1 = plt.plot(neg_log_alphas_enet, coef_e, c=c)\n    l2 = plt.plot(neg_log_alphas_positive_enet, coef_pe, linestyle='--', c=c)\n\nplt.xlabel('-Log(alpha)')\nplt.ylabel('coefficients')\nplt.title('Elastic-Net and positive Elastic-Net')\nplt.legend((l1[-1], l2[-1]), ('Elastic-Net', 'positive Elastic-Net'),\n           loc='lower left')\nplt.axis('tight')\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.6.7"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}