.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/linear_model/plot_sgd_penalties.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via JupyterLite or Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_linear_model_plot_sgd_penalties.py: ============== SGD: Penalties ============== Contours of where the penalty is equal to 1 for the three penalties L1, L2 and elastic-net. All of the above are supported by :class:`~sklearn.linear_model.SGDClassifier` and :class:`~sklearn.linear_model.SGDRegressor`. .. GENERATED FROM PYTHON SOURCE LINES 13-55 .. image-sg:: /auto_examples/linear_model/images/sphx_glr_plot_sgd_penalties_001.png :alt: plot sgd penalties :srcset: /auto_examples/linear_model/images/sphx_glr_plot_sgd_penalties_001.png :class: sphx-glr-single-img .. code-block:: Python import matplotlib.pyplot as plt import numpy as np l1_color = "navy" l2_color = "c" elastic_net_color = "darkorange" line = np.linspace(-1.5, 1.5, 1001) xx, yy = np.meshgrid(line, line) l2 = xx**2 + yy**2 l1 = np.abs(xx) + np.abs(yy) rho = 0.5 elastic_net = rho * l1 + (1 - rho) * l2 plt.figure(figsize=(10, 10), dpi=100) ax = plt.gca() elastic_net_contour = plt.contour( xx, yy, elastic_net, levels=[1], colors=elastic_net_color ) l2_contour = plt.contour(xx, yy, l2, levels=[1], colors=l2_color) l1_contour = plt.contour(xx, yy, l1, levels=[1], colors=l1_color) ax.set_aspect("equal") ax.spines["left"].set_position("center") ax.spines["right"].set_color("none") ax.spines["bottom"].set_position("center") ax.spines["top"].set_color("none") plt.clabel( elastic_net_contour, inline=1, fontsize=18, fmt={1.0: "elastic-net"}, manual=[(-1, -1)], ) plt.clabel(l2_contour, inline=1, fontsize=18, fmt={1.0: "L2"}, manual=[(-1, -1)]) plt.clabel(l1_contour, inline=1, fontsize=18, fmt={1.0: "L1"}, manual=[(-1, -1)]) plt.tight_layout() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.263 seconds) .. _sphx_glr_download_auto_examples_linear_model_plot_sgd_penalties.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-learn/scikit-learn/1.4.X?urlpath=lab/tree/notebooks/auto_examples/linear_model/plot_sgd_penalties.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/?path=auto_examples/linear_model/plot_sgd_penalties.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_sgd_penalties.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_sgd_penalties.py ` .. include:: plot_sgd_penalties.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_