SGD:懲罰#

三個懲罰 L1、L2 和彈性網路的懲罰等於 1 的等高線。

以上所有內容都由 SGDClassifierSGDRegressor 支援。

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.289 秒)

相關範例

SVM 邊界範例

SVM 邊界範例

邏輯迴歸中的 L1 懲罰和稀疏性

邏輯迴歸中的 L1 懲罰和稀疏性

XOR 資料集上高斯過程分類 (GPC) 的說明

XOR 資料集上高斯過程分類 (GPC) 的說明

SGD:加權樣本

SGD:加權樣本

由 Sphinx-Gallery 產生的圖庫