SV打破平局示例#

如果 decision_function_shape='ovr' ,因此默认情况下不启用。这个例子说明了 break_ties 多类分类问题的参数和 decision_function_shape='ovr' .

这两个地块仅在中间的班级并列区域有所不同。如果 break_ties=False ,该区域中的所有输入都将被分类为一个类别,而如果 break_ties=True ,打破平局机制将在该区域创建非凸决策边界。

break_ties = False, break_ties = True
# Authors: The scikit-learn developers
# SPDX-License-Identifier: BSD-3-Clause

import matplotlib.pyplot as plt
import numpy as np

from sklearn.datasets import make_blobs
from sklearn.svm import SVC

X, y = make_blobs(random_state=27)

fig, sub = plt.subplots(2, 1, figsize=(5, 8))
titles = ("break_ties = False", "break_ties = True")

for break_ties, title, ax in zip((False, True), titles, sub.flatten()):
    svm = SVC(
        kernel="linear", C=1, break_ties=break_ties, decision_function_shape="ovr"
    ).fit(X, y)

    xlim = [X[:, 0].min(), X[:, 0].max()]
    ylim = [X[:, 1].min(), X[:, 1].max()]

    xs = np.linspace(xlim[0], xlim[1], 1000)
    ys = np.linspace(ylim[0], ylim[1], 1000)
    xx, yy = np.meshgrid(xs, ys)

    pred = svm.predict(np.c_[xx.ravel(), yy.ravel()])

    colors = [plt.cm.Accent(i) for i in [0, 4, 7]]

    points = ax.scatter(X[:, 0], X[:, 1], c=y, cmap="Accent")
    classes = [(0, 1), (0, 2), (1, 2)]
    line = np.linspace(X[:, 1].min() - 5, X[:, 1].max() + 5)
    ax.imshow(
        -pred.reshape(xx.shape),
        cmap="Accent",
        alpha=0.2,
        extent=(xlim[0], xlim[1], ylim[1], ylim[0]),
    )

    for coef, intercept, col in zip(svm.coef_, svm.intercept_, classes):
        line2 = -(line * coef[1] + intercept) / coef[0]
        ax.plot(line2, line, "-", c=colors[col[0]])
        ax.plot(line2, line, "--", c=colors[col[1]])
    ax.set_xlim(xlim)
    ax.set_ylim(ylim)
    ax.set_title(title)
    ax.set_aspect("equal")

plt.show()

Total running time of the script: (0分0.749秒)

相关实例

支持机边缘示例

SVM Margins Example

多层感知器中的变化规则化

Varying regularization in Multi-layer Perceptron

在虹膜数据集中绘制不同的支持者分类器

Plot different SVM classifiers in the iris dataset

异或数据集上的高斯过程分类(GSK)插图

Illustration of Gaussian process classification (GPC) on the XOR dataset

Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io> _