{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Plot Ridge coefficients as a function of the regularization\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" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Authors: The scikit-learn developers\n# SPDX-License-Identifier: BSD-3-Clause\n\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nfrom sklearn import linear_model\n\n# X is the 10x10 Hilbert matrix\nX = 1.0 / (np.arange(1, 11) + np.arange(0, 10)[:, np.newaxis])\ny = np.ones(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compute paths\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "n_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_)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Display results\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ax = 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.9.21" } }, "nbformat": 4, "nbformat_minor": 0 }