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

在虹膜数据集的2D投影上比较不同线性支持者。我们只考虑该数据集的前2个特征:

  • 花瓣长度

  • 花瓣宽度

这个例子展示了如何绘制具有不同内核的四个SVM分类器的决策曲面。

线性模型 LinearSVC()SVC(kernel='linear') 会产生稍微不同的决策边界。这可能是以下差异的结果:

  • LinearSVC 最大限度地减少铰链损失的平方, SVC 最大限度地减少常规铰链损失。

  • LinearSVC 使用One-vs-All(也称为One-vs-Rest)多类简化, SVC 使用一对一多类简化。

两种线性模型都具有线性决策边界(交叉超平面),而非线性核模型(多项或高斯基函数)具有更灵活的非线性决策边界,其形状取决于核的类型及其参数。

备注

虽然绘制玩具2D数据集分类器的决策函数可以帮助直观地理解它们各自的表达能力,但请注意,这些直觉并不总是概括为更现实的多维问题。

SVC with linear kernel, LinearSVC (linear kernel), SVC with RBF kernel, SVC with polynomial (degree 3) kernel
# Authors: The scikit-learn developers
# SPDX-License-Identifier: BSD-3-Clause

import matplotlib.pyplot as plt

from sklearn import datasets, svm
from sklearn.inspection import DecisionBoundaryDisplay

# import some data to play with
iris = datasets.load_iris()
# Take the first two features. We could avoid this by using a two-dim dataset
X = iris.data[:, :2]
y = iris.target

# we create an instance of SVM and fit out data. We do not scale our
# data since we want to plot the support vectors
C = 1.0  # SVM regularization parameter
models = (
    svm.SVC(kernel="linear", C=C),
    svm.LinearSVC(C=C, max_iter=10000),
    svm.SVC(kernel="rbf", gamma=0.7, C=C),
    svm.SVC(kernel="poly", degree=3, gamma="auto", C=C),
)
models = (clf.fit(X, y) for clf in models)

# title for the plots
titles = (
    "SVC with linear kernel",
    "LinearSVC (linear kernel)",
    "SVC with RBF kernel",
    "SVC with polynomial (degree 3) kernel",
)

# Set-up 2x2 grid for plotting.
fig, sub = plt.subplots(2, 2)
plt.subplots_adjust(wspace=0.4, hspace=0.4)

X0, X1 = X[:, 0], X[:, 1]

for clf, title, ax in zip(models, titles, sub.flatten()):
    disp = DecisionBoundaryDisplay.from_estimator(
        clf,
        X,
        response_method="predict",
        cmap=plt.cm.coolwarm,
        alpha=0.8,
        ax=ax,
        xlabel=iris.feature_names[0],
        ylabel=iris.feature_names[1],
    )
    ax.scatter(X0, X1, c=y, cmap=plt.cm.coolwarm, s=20, edgecolors="k")
    ax.set_xticks(())
    ax.set_yticks(())
    ax.set_title(title)

plt.show()

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

相关实例

具有自定义内核的支持者

SVM with custom kernel

Iris数据集上半监督分类器与支持机的决策边界

Decision boundary of semi-supervised classifiers versus SVM on the Iris dataset

在LinearSRC中绘制支持载体

Plot the support vectors in LinearSVC

绘制在虹膜数据集上训练的决策树的决策面

Plot the decision surface of decision trees trained on the iris dataset

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