{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Plot Ridge coefficients as a function of the regularization\n\n\nShows the effect of collinearity in the coefficients of an estimator.\n\n.. currentmodule:: sklearn.linear_model\n\n:class:`Ridge` Regression is the estimator used in this example.\nEach color represents a different feature of the\ncoefficient vector, and this is displayed as a function of the\nregularization parameter.\n\nThis example also shows the usefulness of applying Ridge regression\nto highly ill-conditioned matrices. For such matrices, a slight\nchange in the target variable can cause huge variances in the\ncalculated weights. In such cases, it is useful to set a certain\nregularization (alpha) to reduce this variation (noise).\n\nWhen alpha is very large, the regularization effect dominates the\nsquared loss function and the coefficients tend to zero.\nAt the end of the path, as alpha tends toward zero\nand the solution tends towards the ordinary least squares, coefficients\nexhibit big oscillations. In practise it is necessary to tune alpha\nin such a way that a balance is maintained between both.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Author: Fabian Pedregosa -- <fabian.pedregosa@inria.fr>\n# License: BSD 3 clause\n\nprint(__doc__)\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn import linear_model\n\n# X is the 10x10 Hilbert matrix\nX = 1. / (np.arange(1, 11) + np.arange(0, 10)[:, np.newaxis])\ny = np.ones(10)\n\n# #############################################################################\n# Compute paths\n\nn_alphas = 200\nalphas = np.logspace(-10, -2, n_alphas)\n\ncoefs = []\nfor a in alphas:\n    ridge = linear_model.Ridge(alpha=a, fit_intercept=False)\n    ridge.fit(X, y)\n    coefs.append(ridge.coef_)\n\n# #############################################################################\n# Display results\n\nax = plt.gca()\n\nax.plot(alphas, coefs)\nax.set_xscale('log')\nax.set_xlim(ax.get_xlim()[::-1])  # reverse axis\nplt.xlabel('alpha')\nplt.ylabel('weights')\nplt.title('Ridge coefficients as a function of the regularization')\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
}