{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# L1 Penalty and Sparsity in Logistic Regression\n\n\nComparison of the sparsity (percentage of zero coefficients) of solutions when\nL1 and L2 penalty are used for different values of C. We can see that large\nvalues of C give more freedom to the model.  Conversely, smaller values of C\nconstrain the model more. In the L1 penalty case, this leads to sparser\nsolutions.\n\nWe classify 8x8 images of digits into two classes: 0-4 against 5-9.\nThe visualization shows coefficients of the models for varying C.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(__doc__)\n\n# Authors: Alexandre Gramfort <alexandre.gramfort@inria.fr>\n#          Mathieu Blondel <mathieu@mblondel.org>\n#          Andreas Mueller <amueller@ais.uni-bonn.de>\n# License: BSD 3 clause\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn import datasets\nfrom sklearn.preprocessing import StandardScaler\n\ndigits = datasets.load_digits()\n\nX, y = digits.data, digits.target\nX = StandardScaler().fit_transform(X)\n\n# classify small against large digits\ny = (y > 4).astype(np.int)\n\n\n# Set regularization parameter\nfor i, C in enumerate((1, 0.1, 0.01)):\n    # turn down tolerance for short training time\n    clf_l1_LR = LogisticRegression(C=C, penalty='l1', tol=0.01, solver='saga')\n    clf_l2_LR = LogisticRegression(C=C, penalty='l2', tol=0.01, solver='saga')\n    clf_l1_LR.fit(X, y)\n    clf_l2_LR.fit(X, y)\n\n    coef_l1_LR = clf_l1_LR.coef_.ravel()\n    coef_l2_LR = clf_l2_LR.coef_.ravel()\n\n    # coef_l1_LR contains zeros due to the\n    # L1 sparsity inducing norm\n\n    sparsity_l1_LR = np.mean(coef_l1_LR == 0) * 100\n    sparsity_l2_LR = np.mean(coef_l2_LR == 0) * 100\n\n    print(\"C=%.2f\" % C)\n    print(\"Sparsity with L1 penalty: %.2f%%\" % sparsity_l1_LR)\n    print(\"score with L1 penalty: %.4f\" % clf_l1_LR.score(X, y))\n    print(\"Sparsity with L2 penalty: %.2f%%\" % sparsity_l2_LR)\n    print(\"score with L2 penalty: %.4f\" % clf_l2_LR.score(X, y))\n\n    l1_plot = plt.subplot(3, 2, 2 * i + 1)\n    l2_plot = plt.subplot(3, 2, 2 * (i + 1))\n    if i == 0:\n        l1_plot.set_title(\"L1 penalty\")\n        l2_plot.set_title(\"L2 penalty\")\n\n    l1_plot.imshow(np.abs(coef_l1_LR.reshape(8, 8)), interpolation='nearest',\n                   cmap='binary', vmax=1, vmin=0)\n    l2_plot.imshow(np.abs(coef_l2_LR.reshape(8, 8)), interpolation='nearest',\n                   cmap='binary', vmax=1, vmin=0)\n    plt.text(-8, 3, \"C = %.2f\" % C)\n\n    l1_plot.set_xticks(())\n    l1_plot.set_yticks(())\n    l2_plot.set_xticks(())\n    l2_plot.set_yticks(())\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.6.7"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}