{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# SGD: Penalties\n\nContours of where the penalty is equal to 1\nfor the three penalties L1, L2 and elastic-net.\n\nAll of the above are supported by :class:`~sklearn.linear_model.SGDClassifier`\nand :class:`~sklearn.linear_model.SGDRegressor`.\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\nl1_color = \"navy\"\nl2_color = \"c\"\nelastic_net_color = \"darkorange\"\n\nline = np.linspace(-1.5, 1.5, 1001)\nxx, yy = np.meshgrid(line, line)\n\nl2 = xx**2 + yy**2\nl1 = np.abs(xx) + np.abs(yy)\nrho = 0.5\nelastic_net = rho * l1 + (1 - rho) * l2\n\nplt.figure(figsize=(10, 10), dpi=100)\nax = plt.gca()\n\nelastic_net_contour = plt.contour(\n xx, yy, elastic_net, levels=[1], colors=elastic_net_color\n)\nl2_contour = plt.contour(xx, yy, l2, levels=[1], colors=l2_color)\nl1_contour = plt.contour(xx, yy, l1, levels=[1], colors=l1_color)\nax.set_aspect(\"equal\")\nax.spines[\"left\"].set_position(\"center\")\nax.spines[\"right\"].set_color(\"none\")\nax.spines[\"bottom\"].set_position(\"center\")\nax.spines[\"top\"].set_color(\"none\")\n\nplt.clabel(\n elastic_net_contour,\n inline=1,\n fontsize=18,\n fmt={1.0: \"elastic-net\"},\n manual=[(-1, -1)],\n)\nplt.clabel(l2_contour, inline=1, fontsize=18, fmt={1.0: \"L2\"}, manual=[(-1, -1)])\nplt.clabel(l1_contour, inline=1, fontsize=18, fmt={1.0: \"L1\"}, manual=[(-1, -1)])\n\nplt.tight_layout()\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 }