Skip to Content
《使用 Scikit-Learn、Keras 和 TensorFlow 进行实践机器学习》第三版
book

《使用 Scikit-Learn、Keras 和 TensorFlow 进行实践机器学习》第三版

by Aurélien Géron
May 2025
Intermediate to advanced
864 pages
12h 32m
Chinese
O'Reilly Media, Inc.
Content preview from 《使用 Scikit-Learn、Keras 和 TensorFlow 进行实践机器学习》第三版

第 5 章 支持向量机 支持向量机

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

支持向量机(SVM)是一种功能强大、用途广泛的机器学习模型,能够进行线性或非线性分类、回归,甚至新颖性检测。SVM 在中小型非线性数据集(即数百到数千个实例)中大显身手,尤其是在分类任务中。但是,它们在处理超大型数据集时并不能很好地扩展,这一点您将会看到。

本章将解释 SVM 的核心概念、使用方法和工作原理。让我们直接进入正题!

线性 SVM 分类

SVM 背后的基本思想最好用一些直观的图片来解释。图 5-1显示了第 4 章末尾介绍的虹膜数据集的一部分。两个类别显然可以用一条直线轻松分开(它们是线性可分的)。左图显示了三种可能的线性分类器的判定边界。决策边界以虚线表示的模型非常糟糕,它甚至不能正确区分两个类别。另外两个模型在这个训练集上工作得很好,但它们的判定边界与实例非常接近,因此这些模型在新实例上的表现可能不会很好。相比之下,右图中的实线代表 SVM 分类器的决策边界;这条线不仅将两个类别分开,还尽可能远离最接近的训练实例。您可以将 SVM 分类器看作是在两个类别之间拟合出一条尽可能宽的街道(由平行虚线表示)。这就是所谓的大余量分类。

mls3 0501
图 5-1. 大边缘分类

请注意,在 "街道外 "添加更多训练实例完全不会影响决策边界:决策边界完全由位于街道边缘的实例决定(或 "支持")。这些实例被称为支持向量(在图 5-1 中被圈起来)。

警告

SVM 对特征尺度很敏感,如图 5-2 所示。在左图中,垂直尺度远大于水平尺度,因此最宽的街道接近水平。经过特征缩放(例如使用 Scikit-Learn 的StandardScaler )后,右图中的决策边界看起来要好得多。

mls3 0502
图 5-2. 对地物尺度的敏感性

软边际分类

如果我们严格规定所有实例都必须不在街道上,而且都在正确的一边,这就叫做硬边距分类。硬边距分类有两个主要问题。首先,它只有在数据可线性分离的情况下才会起作用。其次,它对异常值很敏感。图 5-3显示了仅有一个额外异常值的虹膜数据集:在左侧,不可能找到硬边距;在右侧,决策边界最终与我们在图 5-1中看到的没有异常值的边界截然不同,模型的泛化效果可能也不会太好。

mls3 0503
图 5-3. 硬边际对异常值的敏感性

为了避免这些问题,我们需要使用更灵活的模型。我们的目标是在保持街道尽可能大和限制违反边距(即实例最终位于街道中间甚至错误一侧)之间找到一个良好的平衡点。这就是所谓的软边际分类。

使用 Scikit-Learn 创建 SVM 模型时,您可以指定几个超参数,包括正则化超参数C 。如果将其设置为较低值,就会得到图 5-4 左侧的模型。如果设置为高值,就会得到右边的模型。正如您所看到的,降低C 会使街道变大,但也会导致更多的边际违规。换句话说,降低 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

机器学习实战:基于Scikit-Learn、Keras 和TensorFlow (原书第2 版)

机器学习实战:基于Scikit-Learn、Keras 和TensorFlow (原书第2 版)

Aurélien Géron
学习 API 风格

学习 API 风格

Lukasz Dynowski, Marcin Dulak
《高性能 Python》第二版

《高性能 Python》第二版

Micha Gorelick, Ian Ozsvald

Publisher Resources

ISBN: 9798341656598