首页 分享 在鸢尾花数据集上绘制不同的 SVM 分类器 — scikit

在鸢尾花数据集上绘制不同的 SVM 分类器 — scikit

来源:花匠小妙招 时间:2025-01-11 22:46

在 Iris 数据集上绘制不同的 SVM 分类器#

在 Iris 数据集的 2D 投影上比较不同的线性 SVM 分类器。我们只考虑该数据集的前 2 个特征

萼片长度

萼片宽度

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

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

LinearSVC 最小化平方铰链损失,而 SVC 最小化正则铰链损失。

LinearSVC 使用一对多 (也称为一对剩余) 多类归约,而 SVC 使用一对一多类归约。

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

注意

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

SVC with linear kernel, LinearSVC (linear kernel), SVC with RBF kernel, SVC with polynomial (degree 3) kernel

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

脚本总运行时间: (0 分钟 0.204 秒)

相关示例

由 Sphinx-Gallery 生成的图库

相关知识

基于svm的鸢尾花数据集分类
鸢尾花(Iris)数据集入门
用svm进行鸢尾花分类
【机器学习】SVM对Iris鸢尾花数据集实现多分类
作业5:SVM实现鸢尾花分类
python鸢尾花数据集的分类问题 -- 逻辑回归问题研究
鸢尾花的分类(四种方法)
鸢尾花数据SVM分类案列代码讲解
对鸢尾花数据集和月亮数据集,分别采用线性LDA、k
鸢尾花数据集 — scikit

网址: 在鸢尾花数据集上绘制不同的 SVM 分类器 — scikit https://www.huajiangbk.com/newsview1545164.html

所属分类:花卉
上一篇: 机器学习 鸢尾花分类的原理和实现
下一篇: 基于Jupyter 对鸢尾花数据

推荐分享