Spec-Zone.ru › scikit-learn

Примечание

Перейти к концу, чтобы загрузить полный пример кода. или запустить этот пример в браузере через JupyterLite или Binder

SGD: Штрафы

Контуры, где штраф равен 1 для трёх штрафов L1, L2 и elastic-net.

Все вышеперечисленные поддерживаются SGDClassifier и SGDRegressor.

plot sgd penalties
# Authors: The scikit-learn developers
# SPDX-License-Identifier: BSD-3-Clause

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()

Общее время выполнения скрипта: (0 минут 0,266 секунд)

Launch binder
Launch JupyterLite

Download Jupyter notebook: plot_sgd_penalties.ipynb

Download Python source code: plot_sgd_penalties.py

Download zipped: plot_sgd_penalties.zip

Связанные примеры

Пример отступов SVM

Штраф L1 и разреженность в логистической регрессии

SGD: Взвешенные образцы

Иллюстрация классификации гауссова процесса (GPC) на наборе данных XOR

© 2007–2025 The scikit-learn developers
Licensed under the 3-clause BSD License.
https://scikit-learn.org/1.6/auto_examples/linear_model/plot_sgd_penalties.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API