文章目录
-
- [1. 最大间隔:SVM 的几何直觉](#1. 最大间隔:SVM 的几何直觉)
-
- [1.1 "随便一条线"和"最优的那条线"](#1.1 "随便一条线"和"最优的那条线")
- [1.2 支持向量是什么](#1.2 支持向量是什么)
- [1.3 间隔的数学表达](#1.3 间隔的数学表达)
- [2. 拉格朗日对偶:核技巧的数学基础](#2. 拉格朗日对偶:核技巧的数学基础)
-
- [2.1 为什么要转化为对偶形式](#2.1 为什么要转化为对偶形式)
- [2.2 KKT 条件与稀疏性](#2.2 KKT 条件与稀疏性)
- [3. 核技巧:把低维不可分变成高维可分](#3. 核技巧:把低维不可分变成高维可分)
-
- [3.1 核心思想](#3.1 核心思想)
- [3.2 核函数:跳过显式映射](#3.2 核函数:跳过显式映射)
- [3.3 常用核函数详解](#3.3 常用核函数详解)
- [4. 软间隔与 C 参数:允许"适度犯错"](#4. 软间隔与 C 参数:允许"适度犯错")
-
- [4.1 硬间隔的局限](#4.1 硬间隔的局限)
- [4.2 软间隔:引入 Slack Variable](#4.2 软间隔:引入 Slack Variable)
- [4.3 C 参数的工程含义](#4.3 C 参数的工程含义)
- [5. 核函数选择决策框架](#5. 核函数选择决策框架)
- [6. SVM 多分类策略](#6. SVM 多分类策略)
-
- [6.1 OVR(One-vs-Rest)](#6.1 OVR(One-vs-Rest))
- [6.2 OVO(One-vs-One)](#6.2 OVO(One-vs-One))
- [7. SVM 的工程实践](#7. SVM 的工程实践)
-
- [7.1 必须做特征标准化](#7.1 必须做特征标准化)
- [7.2 训练复杂度与大数据问题](#7.2 训练复杂度与大数据问题)
- [7.3 概率输出:SVM 的局限](#7.3 概率输出:SVM 的局限)
- [7.4 参数搜索代码](#7.4 参数搜索代码)
- [8. SVM vs 其他算法:选型边界](#8. SVM vs 其他算法:选型边界)
- [9. 实战:手写数字分类的核函数对比](#9. 实战:手写数字分类的核函数对比)
-
- [9.1 C 和 γ 的热力图调参](#9.1 C 和 γ 的热力图调参)
- [10. SVM 在深度学习时代的合理定位](#10. SVM 在深度学习时代的合理定位)
- 小结
SVM 是一个容易被误解的算法。入门者觉得它难(满篇拉格朗日乘子和对偶问题),用过的人觉得它简单(fit 一下,调调 C 和 gamma)。这两种理解都不够用。
真正理解 SVM 需要先接受一个核心哲学:分类不只是"画一条线把两类分开",而是"找那条使两侧距离最大的线"。这个看似微小的区别,决定了 SVM 在高维小样本场景下的泛化优势,也决定了它什么时候不如树模型。
本文从几何直觉出发,逐步推进到核技巧的数学本质、参数选择的工程依据,以及 SVM 在深度学习时代的合理定位。
1. 最大间隔:SVM 的几何直觉
1.1 "随便一条线"和"最优的那条线"
考虑一个简单的二分类问题:平面上有红点和蓝点,线性可分。能把它们分开的直线有无数条------任何一条不穿过两类点的线都算"正确分类"。
但这些线并不等价。下图展示了三条都能正确分类的决策边界:
#mermaid-svg-SQSCxBjr16KVmCAv{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-SQSCxBjr16KVmCAv .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-SQSCxBjr16KVmCAv .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-SQSCxBjr16KVmCAv .error-icon{fill:#552222;}#mermaid-svg-SQSCxBjr16KVmCAv .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-SQSCxBjr16KVmCAv .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-SQSCxBjr16KVmCAv .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-SQSCxBjr16KVmCAv .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-SQSCxBjr16KVmCAv .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-SQSCxBjr16KVmCAv .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-SQSCxBjr16KVmCAv .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-SQSCxBjr16KVmCAv .marker{fill:#333333;stroke:#333333;}#mermaid-svg-SQSCxBjr16KVmCAv .marker.cross{stroke:#333333;}#mermaid-svg-SQSCxBjr16KVmCAv svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-SQSCxBjr16KVmCAv p{margin:0;}#mermaid-svg-SQSCxBjr16KVmCAv .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-SQSCxBjr16KVmCAv .cluster-label text{fill:#333;}#mermaid-svg-SQSCxBjr16KVmCAv .cluster-label span{color:#333;}#mermaid-svg-SQSCxBjr16KVmCAv .cluster-label span p{background-color:transparent;}#mermaid-svg-SQSCxBjr16KVmCAv .label text,#mermaid-svg-SQSCxBjr16KVmCAv span{fill:#333;color:#333;}#mermaid-svg-SQSCxBjr16KVmCAv .node rect,#mermaid-svg-SQSCxBjr16KVmCAv .node circle,#mermaid-svg-SQSCxBjr16KVmCAv .node ellipse,#mermaid-svg-SQSCxBjr16KVmCAv .node polygon,#mermaid-svg-SQSCxBjr16KVmCAv .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-SQSCxBjr16KVmCAv .rough-node .label text,#mermaid-svg-SQSCxBjr16KVmCAv .node .label text,#mermaid-svg-SQSCxBjr16KVmCAv .image-shape .label,#mermaid-svg-SQSCxBjr16KVmCAv .icon-shape .label{text-anchor:middle;}#mermaid-svg-SQSCxBjr16KVmCAv .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-SQSCxBjr16KVmCAv .rough-node .label,#mermaid-svg-SQSCxBjr16KVmCAv .node .label,#mermaid-svg-SQSCxBjr16KVmCAv .image-shape .label,#mermaid-svg-SQSCxBjr16KVmCAv .icon-shape .label{text-align:center;}#mermaid-svg-SQSCxBjr16KVmCAv .node.clickable{cursor:pointer;}#mermaid-svg-SQSCxBjr16KVmCAv .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-SQSCxBjr16KVmCAv .arrowheadPath{fill:#333333;}#mermaid-svg-SQSCxBjr16KVmCAv .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-SQSCxBjr16KVmCAv .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-SQSCxBjr16KVmCAv .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-SQSCxBjr16KVmCAv .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-SQSCxBjr16KVmCAv .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-SQSCxBjr16KVmCAv .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-SQSCxBjr16KVmCAv .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-SQSCxBjr16KVmCAv .cluster text{fill:#333;}#mermaid-svg-SQSCxBjr16KVmCAv .cluster span{color:#333;}#mermaid-svg-SQSCxBjr16KVmCAv div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-SQSCxBjr16KVmCAv .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-SQSCxBjr16KVmCAv rect.text{fill:none;stroke-width:0;}#mermaid-svg-SQSCxBjr16KVmCAv .icon-shape,#mermaid-svg-SQSCxBjr16KVmCAv .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-SQSCxBjr16KVmCAv .icon-shape p,#mermaid-svg-SQSCxBjr16KVmCAv .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-SQSCxBjr16KVmCAv .icon-shape .label rect,#mermaid-svg-SQSCxBjr16KVmCAv .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-SQSCxBjr16KVmCAv .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-SQSCxBjr16KVmCAv .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-SQSCxBjr16KVmCAv :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 三种决策边界
新样本稍微偏移
就会误分类
最大化最坏情况下的余量
同样脆弱
边界 ①
紧贴红点
(泛化差)
边界 ②
最大间隔
(SVM选择)
边界 ③
紧贴蓝点
(泛化差)
脆弱
鲁棒
边界 ① 和 ③ 虽然分对了训练数据,但离某一侧点太近------一个微小的噪声就可能导致误分类。边界 ② 在两类点之间找到了"最宽的走廊",两侧都有最大余量。
这就是 SVM 的核心思想:在所有正确分类的超平面中,选那条使两类点距离最大的超平面。
1.2 支持向量是什么
不是所有训练样本都对最终边界有贡献。支持向量是距离超平面最近的那些点------它们"撑住"了这条最宽走廊的两侧边缘。
一个关键性质:删除非支持向量的所有训练样本,决策边界不变。SVM 的分类结果只由这几个边界点决定,其余样本完全无关。
这个性质在小样本场景格外有价值:当训练数据有限时,SVM 只依赖最具信息量的点,不会被大量"普通"样本稀释。
1.3 间隔的数学表达
设超平面为 w T x + b = 0 \mathbf{w}^T \mathbf{x} + b = 0 wTx+b=0,两侧支持向量满足:
w T x + + b = + 1 (正类支持向量) \mathbf{w}^T \mathbf{x}_+ + b = +1 \quad \text{(正类支持向量)} wTx++b=+1(正类支持向量)
w T x − + b = − 1 (负类支持向量) \mathbf{w}^T \mathbf{x}_- + b = -1 \quad \text{(负类支持向量)} wTx−+b=−1(负类支持向量)
两侧边缘超平面之间的距离(即间隔宽度)为:
margin = 2 ∥ w ∥ \text{margin} = \frac{2}{\|\mathbf{w}\|} margin=∥w∥2
最大化间隔等价于:
min w , b 1 2 ∥ w ∥ 2 s.t. y i ( w T x i + b ) ≥ 1 ∀ i \min_{\mathbf{w}, b} \frac{1}{2}\|\mathbf{w}\|^2 \quad \text{s.t.} \quad y_i(\mathbf{w}^T \mathbf{x}_i + b) \geq 1 \; \forall i w,bmin21∥w∥2s.t.yi(wTxi+b)≥1∀i
这是一个凸二次规划问题,有唯一全局最优解。
2. 拉格朗日对偶:核技巧的数学基础
2.1 为什么要转化为对偶形式
原始优化问题(Primal Problem)直接求解 w \mathbf{w} w 和 b b b。但有一个关键限制:后续的核技巧要求优化问题只依赖样本间的内积 x i T x j \mathbf{x}_i^T \mathbf{x}_j xiTxj,而原始形式不满足这一点。
通过引入拉格朗日乘子 α i ≥ 0 \alpha_i \geq 0 αi≥0,构造拉格朗日函数并转化为对偶问题(Dual Problem):
max α ∑ i α i − 1 2 ∑ i , j α i α j y i y j x i T x j \max_{\boldsymbol{\alpha}} \sum_i \alpha_i - \frac{1}{2}\sum_{i,j} \alpha_i \alpha_j y_i y_j \mathbf{x}_i^T \mathbf{x}_j αmaxi∑αi−21i,j∑αiαjyiyjxiTxj
s.t. α i ≥ 0 , ∑ i α i y i = 0 \text{s.t.} \quad \alpha_i \geq 0, \quad \sum_i \alpha_i y_i = 0 s.t.αi≥0,i∑αiyi=0
对偶问题的目标函数只出现内积 x i T x j \mathbf{x}_i^T \mathbf{x}_j xiTxj,这是核技巧可行的根本原因。
2.2 KKT 条件与稀疏性
KKT 互补松弛条件要求:
α i y i ( w T x i + b ) − 1 = 0 \alpha_i \left y_i(\\mathbf{w}\^T \\mathbf{x}_i + b) - 1 \\right = 0 αiyi(wTxi+b)−1=0
这意味着:要么 α i = 0 \alpha_i = 0 αi=0(该样本不是支持向量),要么约束取等(该样本恰好在边界上,是支持向量)。
大多数训练样本的 α i = 0 \alpha_i = 0 αi=0 ,只有支持向量的 α i > 0 \alpha_i > 0 αi>0。这种稀疏性使得 SVM 的预测只需对少数支持向量求和,而非遍历全部训练集。
3. 核技巧:把低维不可分变成高维可分
3.1 核心思想
很多真实数据在原始特征空间不是线性可分的------比如一圈红点围着一圈蓝点,不存在任何直线能分开它们。
解决方法:映射到更高维空间,在高维空间中线性可分。
#mermaid-svg-pzlNrYeYni68GBoe{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-pzlNrYeYni68GBoe .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-pzlNrYeYni68GBoe .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-pzlNrYeYni68GBoe .error-icon{fill:#552222;}#mermaid-svg-pzlNrYeYni68GBoe .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-pzlNrYeYni68GBoe .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-pzlNrYeYni68GBoe .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-pzlNrYeYni68GBoe .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-pzlNrYeYni68GBoe .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-pzlNrYeYni68GBoe .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-pzlNrYeYni68GBoe .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-pzlNrYeYni68GBoe .marker{fill:#333333;stroke:#333333;}#mermaid-svg-pzlNrYeYni68GBoe .marker.cross{stroke:#333333;}#mermaid-svg-pzlNrYeYni68GBoe svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-pzlNrYeYni68GBoe p{margin:0;}#mermaid-svg-pzlNrYeYni68GBoe .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-pzlNrYeYni68GBoe .cluster-label text{fill:#333;}#mermaid-svg-pzlNrYeYni68GBoe .cluster-label span{color:#333;}#mermaid-svg-pzlNrYeYni68GBoe .cluster-label span p{background-color:transparent;}#mermaid-svg-pzlNrYeYni68GBoe .label text,#mermaid-svg-pzlNrYeYni68GBoe span{fill:#333;color:#333;}#mermaid-svg-pzlNrYeYni68GBoe .node rect,#mermaid-svg-pzlNrYeYni68GBoe .node circle,#mermaid-svg-pzlNrYeYni68GBoe .node ellipse,#mermaid-svg-pzlNrYeYni68GBoe .node polygon,#mermaid-svg-pzlNrYeYni68GBoe .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-pzlNrYeYni68GBoe .rough-node .label text,#mermaid-svg-pzlNrYeYni68GBoe .node .label text,#mermaid-svg-pzlNrYeYni68GBoe .image-shape .label,#mermaid-svg-pzlNrYeYni68GBoe .icon-shape .label{text-anchor:middle;}#mermaid-svg-pzlNrYeYni68GBoe .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-pzlNrYeYni68GBoe .rough-node .label,#mermaid-svg-pzlNrYeYni68GBoe .node .label,#mermaid-svg-pzlNrYeYni68GBoe .image-shape .label,#mermaid-svg-pzlNrYeYni68GBoe .icon-shape .label{text-align:center;}#mermaid-svg-pzlNrYeYni68GBoe .node.clickable{cursor:pointer;}#mermaid-svg-pzlNrYeYni68GBoe .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-pzlNrYeYni68GBoe .arrowheadPath{fill:#333333;}#mermaid-svg-pzlNrYeYni68GBoe .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-pzlNrYeYni68GBoe .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-pzlNrYeYni68GBoe .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-pzlNrYeYni68GBoe .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-pzlNrYeYni68GBoe .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-pzlNrYeYni68GBoe .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-pzlNrYeYni68GBoe .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-pzlNrYeYni68GBoe .cluster text{fill:#333;}#mermaid-svg-pzlNrYeYni68GBoe .cluster span{color:#333;}#mermaid-svg-pzlNrYeYni68GBoe div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-pzlNrYeYni68GBoe .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-pzlNrYeYni68GBoe rect.text{fill:none;stroke-width:0;}#mermaid-svg-pzlNrYeYni68GBoe .icon-shape,#mermaid-svg-pzlNrYeYni68GBoe .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-pzlNrYeYni68GBoe .icon-shape p,#mermaid-svg-pzlNrYeYni68GBoe .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-pzlNrYeYni68GBoe .icon-shape .label rect,#mermaid-svg-pzlNrYeYni68GBoe .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-pzlNrYeYni68GBoe .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-pzlNrYeYni68GBoe .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-pzlNrYeYni68GBoe :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 特征映射 φ
找最大间隔超平面
投影回 2D
2D 原始空间
圆形不可分
3D 高维空间
平面可分
3D 决策边界
2D 非线性决策曲线
问题在于:如果映射到很高维(甚至无穷维),显式计算 ϕ ( x i ) T ϕ ( x j ) \phi(\mathbf{x}_i)^T \phi(\mathbf{x}_j) ϕ(xi)Tϕ(xj) 代价极高。
3.2 核函数:跳过显式映射
核函数的定义是:
K ( x i , x j ) = ϕ ( x i ) T ϕ ( x j ) K(\mathbf{x}_i, \mathbf{x}_j) = \phi(\mathbf{x}_i)^T \phi(\mathbf{x}_j) K(xi,xj)=ϕ(xi)Tϕ(xj)
核函数在原始低维空间 直接计算两个样本映射后的内积,完全不需要显式构造 ϕ ( x ) \phi(\mathbf{x}) ϕ(x)。
把对偶问题中的 x i T x j \mathbf{x}_i^T \mathbf{x}_j xiTxj 替换为 K ( x i , x j ) K(\mathbf{x}_i, \mathbf{x}_j) K(xi,xj),就得到了可以处理非线性分类的 SVM------计算代价与显式映射相同(在原始空间),但分类能力等同于高维甚至无穷维的线性 SVM。
3.3 常用核函数详解
| 核函数 | 数学形式 | 隐式映射空间 | 超参数 |
|---|---|---|---|
| 线性核 | x i T x j \mathbf{x}_i^T \mathbf{x}_j xiTxj | 原始空间 | 无 |
| 多项式核 | ( γ x i T x j + r ) d (\gamma \mathbf{x}_i^T \mathbf{x}_j + r)^d (γxiTxj+r)d | d d d 阶多项式特征空间 | γ , r , d \gamma, r, d γ,r,d |
| RBF 核(高斯核) | exp ( − γ ∣ x i − x j ∣ 2 ) \exp(-\gamma |\mathbf{x}_i - \mathbf{x}_j|^2) exp(−γ∣xi−xj∣2) | 无穷维 Hilbert 空间 | γ \gamma γ |
| Sigmoid 核 | tanh ( γ x i T x j + r ) \tanh(\gamma \mathbf{x}_i^T \mathbf{x}_j + r) tanh(γxiTxj+r) | 等效神经网络(满足 Mercer 条件时) | γ , r \gamma, r γ,r |
RBF 核的直观理解 : γ \gamma γ 控制每个样本的"影响范围"。
- γ \gamma γ 小 → 样本影响范围大 → 决策边界光滑 → 欠拟合风险
- γ \gamma γ 大 → 样本影响范围小 → 决策边界复杂 → 过拟合风险
#mermaid-svg-7o1pfGdcT5cmCfhZ{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-7o1pfGdcT5cmCfhZ .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-7o1pfGdcT5cmCfhZ .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-7o1pfGdcT5cmCfhZ .error-icon{fill:#552222;}#mermaid-svg-7o1pfGdcT5cmCfhZ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-7o1pfGdcT5cmCfhZ .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-7o1pfGdcT5cmCfhZ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-7o1pfGdcT5cmCfhZ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-7o1pfGdcT5cmCfhZ .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-7o1pfGdcT5cmCfhZ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-7o1pfGdcT5cmCfhZ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-7o1pfGdcT5cmCfhZ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-7o1pfGdcT5cmCfhZ .marker.cross{stroke:#333333;}#mermaid-svg-7o1pfGdcT5cmCfhZ svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-7o1pfGdcT5cmCfhZ p{margin:0;}#mermaid-svg-7o1pfGdcT5cmCfhZ .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-7o1pfGdcT5cmCfhZ .cluster-label text{fill:#333;}#mermaid-svg-7o1pfGdcT5cmCfhZ .cluster-label span{color:#333;}#mermaid-svg-7o1pfGdcT5cmCfhZ .cluster-label span p{background-color:transparent;}#mermaid-svg-7o1pfGdcT5cmCfhZ .label text,#mermaid-svg-7o1pfGdcT5cmCfhZ span{fill:#333;color:#333;}#mermaid-svg-7o1pfGdcT5cmCfhZ .node rect,#mermaid-svg-7o1pfGdcT5cmCfhZ .node circle,#mermaid-svg-7o1pfGdcT5cmCfhZ .node ellipse,#mermaid-svg-7o1pfGdcT5cmCfhZ .node polygon,#mermaid-svg-7o1pfGdcT5cmCfhZ .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-7o1pfGdcT5cmCfhZ .rough-node .label text,#mermaid-svg-7o1pfGdcT5cmCfhZ .node .label text,#mermaid-svg-7o1pfGdcT5cmCfhZ .image-shape .label,#mermaid-svg-7o1pfGdcT5cmCfhZ .icon-shape .label{text-anchor:middle;}#mermaid-svg-7o1pfGdcT5cmCfhZ .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-7o1pfGdcT5cmCfhZ .rough-node .label,#mermaid-svg-7o1pfGdcT5cmCfhZ .node .label,#mermaid-svg-7o1pfGdcT5cmCfhZ .image-shape .label,#mermaid-svg-7o1pfGdcT5cmCfhZ .icon-shape .label{text-align:center;}#mermaid-svg-7o1pfGdcT5cmCfhZ .node.clickable{cursor:pointer;}#mermaid-svg-7o1pfGdcT5cmCfhZ .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-7o1pfGdcT5cmCfhZ .arrowheadPath{fill:#333333;}#mermaid-svg-7o1pfGdcT5cmCfhZ .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-7o1pfGdcT5cmCfhZ .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-7o1pfGdcT5cmCfhZ .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-7o1pfGdcT5cmCfhZ .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-7o1pfGdcT5cmCfhZ .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-7o1pfGdcT5cmCfhZ .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-7o1pfGdcT5cmCfhZ .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-7o1pfGdcT5cmCfhZ .cluster text{fill:#333;}#mermaid-svg-7o1pfGdcT5cmCfhZ .cluster span{color:#333;}#mermaid-svg-7o1pfGdcT5cmCfhZ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-7o1pfGdcT5cmCfhZ .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-7o1pfGdcT5cmCfhZ rect.text{fill:none;stroke-width:0;}#mermaid-svg-7o1pfGdcT5cmCfhZ .icon-shape,#mermaid-svg-7o1pfGdcT5cmCfhZ .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-7o1pfGdcT5cmCfhZ .icon-shape p,#mermaid-svg-7o1pfGdcT5cmCfhZ .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-7o1pfGdcT5cmCfhZ .icon-shape .label rect,#mermaid-svg-7o1pfGdcT5cmCfhZ .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-7o1pfGdcT5cmCfhZ .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-7o1pfGdcT5cmCfhZ .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-7o1pfGdcT5cmCfhZ :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} γ 大
γ 小
每个样本
影响远处
决策边界
平滑宽泛
每个样本
只影响近处
决策边界
蜿蜒复杂
欠拟合
(bias 高)
过拟合
(variance 高)
4. 软间隔与 C 参数:允许"适度犯错"
4.1 硬间隔的局限
硬间隔 SVM 要求所有训练样本严格在边界正确一侧: y i ( w T x i + b ) ≥ 1 y_i(\mathbf{w}^T \mathbf{x}_i + b) \geq 1 yi(wTxi+b)≥1。
实际数据几乎都存在噪声和重叠------强行要求完美分类会导致:
- 极小的间隔(为了绕过噪声点)
- 边界过于复杂
- 严重过拟合
4.2 软间隔:引入 Slack Variable
软间隔 SVM 允许每个样本"付出代价"越界:
min w , b , ξ 1 2 ∥ w ∥ 2 + C ∑ i ξ i \min_{\mathbf{w}, b, \boldsymbol{\xi}} \frac{1}{2}\|\mathbf{w}\|^2 + C\sum_i \xi_i w,b,ξmin21∥w∥2+Ci∑ξi
s.t. y i ( w T x i + b ) ≥ 1 − ξ i , ξ i ≥ 0 \text{s.t.} \quad y_i(\mathbf{w}^T \mathbf{x}_i + b) \geq 1 - \xi_i, \quad \xi_i \geq 0 s.t.yi(wTxi+b)≥1−ξi,ξi≥0
ξ i \xi_i ξi 是松弛变量(Slack Variable),表示第 i i i 个样本的越界程度:
- ξ i = 0 \xi_i = 0 ξi=0:正确分类且在间隔外
- 0 < ξ i ≤ 1 0 < \xi_i \leq 1 0<ξi≤1:在间隔内但仍正确分类
- ξ i > 1 \xi_i > 1 ξi>1:误分类
4.3 C 参数的工程含义
C C C 控制"容错代价"与"间隔宽度"之间的权衡:
#mermaid-svg-96bPTiBD6TxCA4oB{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-96bPTiBD6TxCA4oB .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-96bPTiBD6TxCA4oB .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-96bPTiBD6TxCA4oB .error-icon{fill:#552222;}#mermaid-svg-96bPTiBD6TxCA4oB .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-96bPTiBD6TxCA4oB .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-96bPTiBD6TxCA4oB .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-96bPTiBD6TxCA4oB .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-96bPTiBD6TxCA4oB .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-96bPTiBD6TxCA4oB .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-96bPTiBD6TxCA4oB .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-96bPTiBD6TxCA4oB .marker{fill:#333333;stroke:#333333;}#mermaid-svg-96bPTiBD6TxCA4oB .marker.cross{stroke:#333333;}#mermaid-svg-96bPTiBD6TxCA4oB svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-96bPTiBD6TxCA4oB p{margin:0;}#mermaid-svg-96bPTiBD6TxCA4oB .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-96bPTiBD6TxCA4oB .cluster-label text{fill:#333;}#mermaid-svg-96bPTiBD6TxCA4oB .cluster-label span{color:#333;}#mermaid-svg-96bPTiBD6TxCA4oB .cluster-label span p{background-color:transparent;}#mermaid-svg-96bPTiBD6TxCA4oB .label text,#mermaid-svg-96bPTiBD6TxCA4oB span{fill:#333;color:#333;}#mermaid-svg-96bPTiBD6TxCA4oB .node rect,#mermaid-svg-96bPTiBD6TxCA4oB .node circle,#mermaid-svg-96bPTiBD6TxCA4oB .node ellipse,#mermaid-svg-96bPTiBD6TxCA4oB .node polygon,#mermaid-svg-96bPTiBD6TxCA4oB .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-96bPTiBD6TxCA4oB .rough-node .label text,#mermaid-svg-96bPTiBD6TxCA4oB .node .label text,#mermaid-svg-96bPTiBD6TxCA4oB .image-shape .label,#mermaid-svg-96bPTiBD6TxCA4oB .icon-shape .label{text-anchor:middle;}#mermaid-svg-96bPTiBD6TxCA4oB .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-96bPTiBD6TxCA4oB .rough-node .label,#mermaid-svg-96bPTiBD6TxCA4oB .node .label,#mermaid-svg-96bPTiBD6TxCA4oB .image-shape .label,#mermaid-svg-96bPTiBD6TxCA4oB .icon-shape .label{text-align:center;}#mermaid-svg-96bPTiBD6TxCA4oB .node.clickable{cursor:pointer;}#mermaid-svg-96bPTiBD6TxCA4oB .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-96bPTiBD6TxCA4oB .arrowheadPath{fill:#333333;}#mermaid-svg-96bPTiBD6TxCA4oB .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-96bPTiBD6TxCA4oB .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-96bPTiBD6TxCA4oB .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-96bPTiBD6TxCA4oB .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-96bPTiBD6TxCA4oB .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-96bPTiBD6TxCA4oB .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-96bPTiBD6TxCA4oB .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-96bPTiBD6TxCA4oB .cluster text{fill:#333;}#mermaid-svg-96bPTiBD6TxCA4oB .cluster span{color:#333;}#mermaid-svg-96bPTiBD6TxCA4oB div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-96bPTiBD6TxCA4oB .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-96bPTiBD6TxCA4oB rect.text{fill:none;stroke-width:0;}#mermaid-svg-96bPTiBD6TxCA4oB .icon-shape,#mermaid-svg-96bPTiBD6TxCA4oB .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-96bPTiBD6TxCA4oB .icon-shape p,#mermaid-svg-96bPTiBD6TxCA4oB .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-96bPTiBD6TxCA4oB .icon-shape .label rect,#mermaid-svg-96bPTiBD6TxCA4oB .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-96bPTiBD6TxCA4oB .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-96bPTiBD6TxCA4oB .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-96bPTiBD6TxCA4oB :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} C 参数
C 大
每个错误代价高
→ 容忍更少错误
→ 间隔变小
→ 过拟合风险↑
C 小
每个错误代价低
→ 容忍更多错误
→ 间隔变大
→ 欠拟合风险↑
适合:噪声少、线性可分的数据
适合:有较多噪声和重叠的数据
调参经验:
- 先用
C=1.0(默认值)建立基线 - 训练误差高 → 增大 C(让模型更努力拟合)
- 过拟合(训练好/验证差)→ 减小 C(增加正则化)
- 结合
GridSearchCV在 0.01 , 0.1 , 1 , 10 , 100 0.01, 0.1, 1, 10, 100 0.01,0.1,1,10,100 中搜索
5. 核函数选择决策框架
核函数选择是 SVM 调优中最关键的一步,不同核函数适用于本质不同的场景:
#mermaid-svg-LdOKDrN5XW7puthg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-LdOKDrN5XW7puthg .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-LdOKDrN5XW7puthg .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-LdOKDrN5XW7puthg .error-icon{fill:#552222;}#mermaid-svg-LdOKDrN5XW7puthg .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-LdOKDrN5XW7puthg .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-LdOKDrN5XW7puthg .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-LdOKDrN5XW7puthg .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-LdOKDrN5XW7puthg .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-LdOKDrN5XW7puthg .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-LdOKDrN5XW7puthg .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-LdOKDrN5XW7puthg .marker{fill:#333333;stroke:#333333;}#mermaid-svg-LdOKDrN5XW7puthg .marker.cross{stroke:#333333;}#mermaid-svg-LdOKDrN5XW7puthg svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-LdOKDrN5XW7puthg p{margin:0;}#mermaid-svg-LdOKDrN5XW7puthg .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-LdOKDrN5XW7puthg .cluster-label text{fill:#333;}#mermaid-svg-LdOKDrN5XW7puthg .cluster-label span{color:#333;}#mermaid-svg-LdOKDrN5XW7puthg .cluster-label span p{background-color:transparent;}#mermaid-svg-LdOKDrN5XW7puthg .label text,#mermaid-svg-LdOKDrN5XW7puthg span{fill:#333;color:#333;}#mermaid-svg-LdOKDrN5XW7puthg .node rect,#mermaid-svg-LdOKDrN5XW7puthg .node circle,#mermaid-svg-LdOKDrN5XW7puthg .node ellipse,#mermaid-svg-LdOKDrN5XW7puthg .node polygon,#mermaid-svg-LdOKDrN5XW7puthg .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-LdOKDrN5XW7puthg .rough-node .label text,#mermaid-svg-LdOKDrN5XW7puthg .node .label text,#mermaid-svg-LdOKDrN5XW7puthg .image-shape .label,#mermaid-svg-LdOKDrN5XW7puthg .icon-shape .label{text-anchor:middle;}#mermaid-svg-LdOKDrN5XW7puthg .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-LdOKDrN5XW7puthg .rough-node .label,#mermaid-svg-LdOKDrN5XW7puthg .node .label,#mermaid-svg-LdOKDrN5XW7puthg .image-shape .label,#mermaid-svg-LdOKDrN5XW7puthg .icon-shape .label{text-align:center;}#mermaid-svg-LdOKDrN5XW7puthg .node.clickable{cursor:pointer;}#mermaid-svg-LdOKDrN5XW7puthg .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-LdOKDrN5XW7puthg .arrowheadPath{fill:#333333;}#mermaid-svg-LdOKDrN5XW7puthg .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-LdOKDrN5XW7puthg .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-LdOKDrN5XW7puthg .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-LdOKDrN5XW7puthg .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-LdOKDrN5XW7puthg .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-LdOKDrN5XW7puthg .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-LdOKDrN5XW7puthg .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-LdOKDrN5XW7puthg .cluster text{fill:#333;}#mermaid-svg-LdOKDrN5XW7puthg .cluster span{color:#333;}#mermaid-svg-LdOKDrN5XW7puthg div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-LdOKDrN5XW7puthg .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-LdOKDrN5XW7puthg rect.text{fill:none;stroke-width:0;}#mermaid-svg-LdOKDrN5XW7puthg .icon-shape,#mermaid-svg-LdOKDrN5XW7puthg .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-LdOKDrN5XW7puthg .icon-shape p,#mermaid-svg-LdOKDrN5XW7puthg .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-LdOKDrN5XW7puthg .icon-shape .label rect,#mermaid-svg-LdOKDrN5XW7puthg .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-LdOKDrN5XW7puthg .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-LdOKDrN5XW7puthg .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-LdOKDrN5XW7puthg :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 是
文本/基因等高维稀疏
否
不确定 / 通用场景
特征交互有显著物理意义
样本数量大 > 10万
数据特征
特征维度高
(> 样本数量)?
线性核
LinearSVC 更快
数据是否
明显非线性?
RBF 核(默认首选)
调 C + γ
多项式核
调 d + γ + r
线性核
(LinearSVC 更快)
或考虑神经网络
GridSearch:
C ∈ 0.01\~100
γ ∈ 0.001\~10
GridSearch:
C ∈ 0.01\~100
核函数选型速查表:
| 场景 | 推荐核 | 原因 |
|---|---|---|
| 文本分类(TF-IDF 特征) | 线性核 | 高维稀疏,线性可分性好 |
| 图像分类(小样本) | RBF 核 | 非线性决策面,样本少 |
| 基因表达数据 | 线性核 | 特征维度 >> 样本数 |
| 二维/低维数据 | RBF 核 | 原始维度低,需要非线性映射 |
| 样本量 > 10 万 | LinearSVC | 训练速度快,O(n) 复杂度 |
| 有物理意义的多项式特征 | 多项式核 | 显式特征交互 |
6. SVM 多分类策略
SVM 原生只支持二分类,多分类需要额外策略。
6.1 OVR(One-vs-Rest)
训练 N N N 个二分类器,第 k k k 个分类器区分"类别 k k k"和"其余所有类别"。预测时取得分最高的类别。
特点:
- 训练 N N N 个分类器(少)
- 每个分类器的训练集不平衡(1:N-1)
- 适合类别数量多的场景
6.2 OVO(One-vs-One)
训练 ( N 2 ) = N ( N − 1 ) 2 \binom{N}{2} = \frac{N(N-1)}{2} (2N)=2N(N−1) 个二分类器,每对类别训练一个。预测时使用多数投票。
特点:
- 训练分类器数量多(但每个训练集更小)
- 每个分类器的训练集平衡(1:1)
- 通常精度略高于 OVR
#mermaid-svg-ltvwSfTtZwPN9KGP{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-ltvwSfTtZwPN9KGP .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-ltvwSfTtZwPN9KGP .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-ltvwSfTtZwPN9KGP .error-icon{fill:#552222;}#mermaid-svg-ltvwSfTtZwPN9KGP .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ltvwSfTtZwPN9KGP .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-ltvwSfTtZwPN9KGP .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ltvwSfTtZwPN9KGP .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ltvwSfTtZwPN9KGP .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-ltvwSfTtZwPN9KGP .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ltvwSfTtZwPN9KGP .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ltvwSfTtZwPN9KGP .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ltvwSfTtZwPN9KGP .marker.cross{stroke:#333333;}#mermaid-svg-ltvwSfTtZwPN9KGP svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ltvwSfTtZwPN9KGP p{margin:0;}#mermaid-svg-ltvwSfTtZwPN9KGP .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-ltvwSfTtZwPN9KGP .cluster-label text{fill:#333;}#mermaid-svg-ltvwSfTtZwPN9KGP .cluster-label span{color:#333;}#mermaid-svg-ltvwSfTtZwPN9KGP .cluster-label span p{background-color:transparent;}#mermaid-svg-ltvwSfTtZwPN9KGP .label text,#mermaid-svg-ltvwSfTtZwPN9KGP span{fill:#333;color:#333;}#mermaid-svg-ltvwSfTtZwPN9KGP .node rect,#mermaid-svg-ltvwSfTtZwPN9KGP .node circle,#mermaid-svg-ltvwSfTtZwPN9KGP .node ellipse,#mermaid-svg-ltvwSfTtZwPN9KGP .node polygon,#mermaid-svg-ltvwSfTtZwPN9KGP .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ltvwSfTtZwPN9KGP .rough-node .label text,#mermaid-svg-ltvwSfTtZwPN9KGP .node .label text,#mermaid-svg-ltvwSfTtZwPN9KGP .image-shape .label,#mermaid-svg-ltvwSfTtZwPN9KGP .icon-shape .label{text-anchor:middle;}#mermaid-svg-ltvwSfTtZwPN9KGP .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-ltvwSfTtZwPN9KGP .rough-node .label,#mermaid-svg-ltvwSfTtZwPN9KGP .node .label,#mermaid-svg-ltvwSfTtZwPN9KGP .image-shape .label,#mermaid-svg-ltvwSfTtZwPN9KGP .icon-shape .label{text-align:center;}#mermaid-svg-ltvwSfTtZwPN9KGP .node.clickable{cursor:pointer;}#mermaid-svg-ltvwSfTtZwPN9KGP .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-ltvwSfTtZwPN9KGP .arrowheadPath{fill:#333333;}#mermaid-svg-ltvwSfTtZwPN9KGP .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-ltvwSfTtZwPN9KGP .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-ltvwSfTtZwPN9KGP .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ltvwSfTtZwPN9KGP .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-ltvwSfTtZwPN9KGP .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ltvwSfTtZwPN9KGP .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-ltvwSfTtZwPN9KGP .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-ltvwSfTtZwPN9KGP .cluster text{fill:#333;}#mermaid-svg-ltvwSfTtZwPN9KGP .cluster span{color:#333;}#mermaid-svg-ltvwSfTtZwPN9KGP div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-ltvwSfTtZwPN9KGP .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-ltvwSfTtZwPN9KGP rect.text{fill:none;stroke-width:0;}#mermaid-svg-ltvwSfTtZwPN9KGP .icon-shape,#mermaid-svg-ltvwSfTtZwPN9KGP .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ltvwSfTtZwPN9KGP .icon-shape p,#mermaid-svg-ltvwSfTtZwPN9KGP .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-ltvwSfTtZwPN9KGP .icon-shape .label rect,#mermaid-svg-ltvwSfTtZwPN9KGP .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ltvwSfTtZwPN9KGP .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-ltvwSfTtZwPN9KGP .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-ltvwSfTtZwPN9KGP :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} OVO策略
OVR策略
N 个分类器
各区分一类 vs 其余
取最高置信度
N(N-1)/2 个分类器
各区分两类
多数投票
训练快
但不平衡
精度高
但分类器多
sklearn 中 SVC 默认使用 OVO,LinearSVC 默认使用 OVR。
7. SVM 的工程实践
7.1 必须做特征标准化
SVM 是对特征尺度极度敏感 的算法。直觉:间隔的计算依赖欧氏距离,如果某个特征取值范围是 0 , 1000 0, 1000 0,1000 而另一个是 0 , 1 0, 1 0,1,前者会主导距离计算。
python
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.pipeline import Pipeline
# 必须在 Pipeline 中先做标准化
model = Pipeline([
('scaler', StandardScaler()),
('svm', SVC(kernel='rbf', C=1.0, gamma='scale'))
])
不做标准化的后果:模型收敛慢,C 和 γ 的搜索范围严重失真,调出来的参数毫无意义。
7.2 训练复杂度与大数据问题
标准 SVM(SMO 算法)的训练复杂度为 O ( n 2 ) ∼ O ( n 3 ) O(n^2) \sim O(n^3) O(n2)∼O(n3),当样本量超过 10 万时训练时间不可接受。
应对策略:
| 样本量级 | 推荐方案 | 原因 |
|---|---|---|
| < 1 万 | SVC(任意核) | 标准 SMO,质量最好 |
| 1 万 ~ 10 万 | SVC + 小数据集策略或 LinearSVC | 权衡速度与精度 |
| > 10 万 | LinearSVC 或梯度提升树 | O(n) 线性复杂度 |
| > 100 万 | 考虑 SGD + Hinge Loss 或神经网络 | SVM 不再是合理选择 |
LinearSVC 实现的是线性核 SVM,用 LibLinear 优化,复杂度 O ( n ) O(n) O(n),支持大规模数据。
7.3 概率输出:SVM 的局限
SVM 原生输出的是类别标签,不是概率 。通过 probability=True 开启 Platt Scaling 可以获得概率输出,但:
- 训练时间额外增加 5 倍(内部用交叉验证做校准)
- 输出的概率校准效果比 Logistic Regression 差
如果业务需要精确概率(风控、医疗诊断),优先考虑 LR + 校准,而非 SVM + Platt。
7.4 参数搜索代码
python
from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
# 构建 Pipeline(缩放 + SVM)
pipe = Pipeline([
('scaler', StandardScaler()),
('svm', SVC(kernel='rbf'))
])
# 参数网格(注意 Pipeline 参数命名:步骤名__参数名)
param_grid = {
'svm__C': [0.01, 0.1, 1, 10, 100],
'svm__gamma': [0.001, 0.01, 0.1, 1, 'scale', 'auto']
}
grid_search = GridSearchCV(
pipe, param_grid, cv=5,
scoring='f1_weighted', n_jobs=-1, verbose=1
)
grid_search.fit(X_train, y_train)
print(f"Best params: {grid_search.best_params_}")
print(f"Best CV score: {grid_search.best_score_:.4f}")
8. SVM vs 其他算法:选型边界
SVM 的适用场景有清晰的边界,超过这个边界就应该换算法:
#mermaid-svg-2DnWFIRhsUBcUOOi{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-2DnWFIRhsUBcUOOi .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-2DnWFIRhsUBcUOOi .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-2DnWFIRhsUBcUOOi .error-icon{fill:#552222;}#mermaid-svg-2DnWFIRhsUBcUOOi .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-2DnWFIRhsUBcUOOi .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-2DnWFIRhsUBcUOOi .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-2DnWFIRhsUBcUOOi .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-2DnWFIRhsUBcUOOi .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-2DnWFIRhsUBcUOOi .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-2DnWFIRhsUBcUOOi .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-2DnWFIRhsUBcUOOi .marker{fill:#333333;stroke:#333333;}#mermaid-svg-2DnWFIRhsUBcUOOi .marker.cross{stroke:#333333;}#mermaid-svg-2DnWFIRhsUBcUOOi svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-2DnWFIRhsUBcUOOi p{margin:0;}#mermaid-svg-2DnWFIRhsUBcUOOi .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-2DnWFIRhsUBcUOOi .cluster-label text{fill:#333;}#mermaid-svg-2DnWFIRhsUBcUOOi .cluster-label span{color:#333;}#mermaid-svg-2DnWFIRhsUBcUOOi .cluster-label span p{background-color:transparent;}#mermaid-svg-2DnWFIRhsUBcUOOi .label text,#mermaid-svg-2DnWFIRhsUBcUOOi span{fill:#333;color:#333;}#mermaid-svg-2DnWFIRhsUBcUOOi .node rect,#mermaid-svg-2DnWFIRhsUBcUOOi .node circle,#mermaid-svg-2DnWFIRhsUBcUOOi .node ellipse,#mermaid-svg-2DnWFIRhsUBcUOOi .node polygon,#mermaid-svg-2DnWFIRhsUBcUOOi .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-2DnWFIRhsUBcUOOi .rough-node .label text,#mermaid-svg-2DnWFIRhsUBcUOOi .node .label text,#mermaid-svg-2DnWFIRhsUBcUOOi .image-shape .label,#mermaid-svg-2DnWFIRhsUBcUOOi .icon-shape .label{text-anchor:middle;}#mermaid-svg-2DnWFIRhsUBcUOOi .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-2DnWFIRhsUBcUOOi .rough-node .label,#mermaid-svg-2DnWFIRhsUBcUOOi .node .label,#mermaid-svg-2DnWFIRhsUBcUOOi .image-shape .label,#mermaid-svg-2DnWFIRhsUBcUOOi .icon-shape .label{text-align:center;}#mermaid-svg-2DnWFIRhsUBcUOOi .node.clickable{cursor:pointer;}#mermaid-svg-2DnWFIRhsUBcUOOi .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-2DnWFIRhsUBcUOOi .arrowheadPath{fill:#333333;}#mermaid-svg-2DnWFIRhsUBcUOOi .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-2DnWFIRhsUBcUOOi .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-2DnWFIRhsUBcUOOi .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-2DnWFIRhsUBcUOOi .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-2DnWFIRhsUBcUOOi .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-2DnWFIRhsUBcUOOi .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-2DnWFIRhsUBcUOOi .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-2DnWFIRhsUBcUOOi .cluster text{fill:#333;}#mermaid-svg-2DnWFIRhsUBcUOOi .cluster span{color:#333;}#mermaid-svg-2DnWFIRhsUBcUOOi div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-2DnWFIRhsUBcUOOi .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-2DnWFIRhsUBcUOOi rect.text{fill:none;stroke-width:0;}#mermaid-svg-2DnWFIRhsUBcUOOi .icon-shape,#mermaid-svg-2DnWFIRhsUBcUOOi .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-2DnWFIRhsUBcUOOi .icon-shape p,#mermaid-svg-2DnWFIRhsUBcUOOi .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-2DnWFIRhsUBcUOOi .icon-shape .label rect,#mermaid-svg-2DnWFIRhsUBcUOOi .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-2DnWFIRhsUBcUOOi .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-2DnWFIRhsUBcUOOi .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-2DnWFIRhsUBcUOOi :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 是
否
是(文本/基因)
否
是(风控/医疗)
否
是(高维小样本)
否
选型起点
样本量
> 10万?
优先树模型 / 神经网络
SVM 训练太慢
特征维度
>> 样本量?
线性 SVM
(LinearSVC)
需要概率输出?
Logistic Regression
概率校准更好
数据非线性
且样本少?
RBF SVM
SVM 的天然优势场景
随机森林 / XGBoost
通常更简单可靠
SVM 的三个天然优势场景:
- 高维小样本:100 个医疗样本 × 5000 个基因特征,树模型过拟合,SVM 稳定
- 文本分类:TF-IDF 特征高维稀疏,线性 SVM 速度快、精度高
- 小数据核方法:样本不足以训练神经网络,核技巧提供非线性能力
9. 实战:手写数字分类的核函数对比
python
import numpy as np
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.svm import SVC, LinearSVC
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
import time
# 加载数据(1797 个样本,64 维特征,10 类)
digits = load_digits()
X, y = digits.data, digits.target
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
# 各核函数对比
configs = [
('Linear SVM', SVC(kernel='linear', C=1.0)),
('RBF SVM (default)', SVC(kernel='rbf', C=1.0, gamma='scale')),
('RBF SVM (tuned)', SVC(kernel='rbf', C=10.0, gamma=0.01)),
('Poly SVM (d=3)', SVC(kernel='poly', degree=3, C=1.0, gamma='scale')),
('LinearSVC', LinearSVC(C=1.0, max_iter=2000)),
]
results = []
for name, clf in configs:
pipe = Pipeline([('scaler', StandardScaler()), ('clf', clf)])
start = time.time()
cv_scores = cross_val_score(pipe, X_train, y_train, cv=5, scoring='accuracy')
elapsed = time.time() - start
pipe.fit(X_train, y_train)
test_acc = pipe.score(X_test, y_test)
results.append({
'Model': name,
'CV Mean': f"{cv_scores.mean():.4f}",
'CV Std': f"{cv_scores.std():.4f}",
'Test Acc': f"{test_acc:.4f}",
'Time (s)': f"{elapsed:.2f}"
})
print(f"{name}: CV={cv_scores.mean():.4f}±{cv_scores.std():.4f} "
f"Test={test_acc:.4f} Time={elapsed:.2f}s")
典型结果(MNIST Digits 数据集,1797 样本,64 维):
| 模型 | CV 精度 | 测试精度 | 训练时间 |
|---|---|---|---|
| Linear SVM | 0.9776 | 0.9806 | 0.8s |
| RBF SVM (default) | 0.9849 | 0.9861 | 1.2s |
| RBF SVM (tuned C=10, γ=0.01) | 0.9875 | 0.9889 | 2.1s |
| Poly SVM (d=3) | 0.9849 | 0.9833 | 1.5s |
| LinearSVC | 0.9748 | 0.9778 | 0.3s |
关键观察:
- RBF 调参后比默认提升约 0.3%,小数据集中有意义
- LinearSVC 最快,且精度只比 Linear SVM 低一点点
- Poly 核并不比 RBF 更好(数字图像没有多项式交互的先验)
9.1 C 和 γ 的热力图调参
python
from sklearn.model_selection import GridSearchCV
import matplotlib.pyplot as plt
import numpy as np
C_range = np.logspace(-2, 2, 5) # [0.01, 0.1, 1, 10, 100]
gamma_range = np.logspace(-3, 1, 5) # [0.001, 0.01, 0.1, 1, 10]
param_grid = {
'clf__C': C_range,
'clf__gamma': gamma_range
}
pipe_rbf = Pipeline([
('scaler', StandardScaler()),
('clf', SVC(kernel='rbf'))
])
gs = GridSearchCV(pipe_rbf, param_grid, cv=5, scoring='accuracy', n_jobs=-1)
gs.fit(X_train, y_train)
print(f"Best params: C={gs.best_params_['clf__C']}, "
f"gamma={gs.best_params_['clf__gamma']}")
print(f"Best CV accuracy: {gs.best_score_:.4f}")
参数热力图的规律(手写数字场景):
- 最优区域通常在中等 C(1~10)× 中等 γ(0.001~0.1)
- 高 C + 高 γ → 严重过拟合(决策边界极度复杂)
- 低 C + 低 γ → 欠拟合(间隔太宽,分类太粗)
10. SVM 在深度学习时代的合理定位
深度学习并没有"消灭" SVM,只是重新定义了它的使用场景:
SVM 仍然适用的场景:
- 数据量 < 5000,深度学习没有足够数据发挥优势
- 特征工程已经做好,不需要端到端学习特征表示
- 高维稀疏文本分类(LinearSVC 速度优势明显)
- 需要可解释的决策支持(SVM 的分类依据可以追溯到支持向量)
- 计算资源受限,无法训练大模型
SVM 不适用的场景:
- 图像/语音/自然语言的端到端学习(深度学习在特征学习上碾压性优势)
- 大规模数据(> 10 万样本,训练代价不可接受)
- 需要精确概率输出(校准效果有限)
一个务实的建议:SVM 和梯度提升树是"数据量不大时的稳健基线",在没有海量数据的业务场景中,它们往往比精心调优的轻量神经网络表现更好,也更容易维护。
小结
SVM 的全部哲学可以用三个关键词概括:
最大间隔:不是随便找一条分类线,而是找那条使两侧距离最大的线------这给了 SVM 在噪声和分布偏移下的鲁棒性。
核技巧:不需要显式构造高维特征,只需要一个核函数在低维空间间接计算高维内积------这给了 SVM 处理非线性问题的能力,代价几乎为零。
软间隔:通过 C 参数控制对错误的容忍程度,在间隔宽度和分类精度之间找到平衡------这让 SVM 在有噪声的真实数据上可用。
理解了这三点,就能解释 SVM 在什么场景有优势、什么场景该换算法,也能在调参时不再盲目------C 控制容错,γ 控制影响范围,核函数决定问题的结构假设。
前文在数据预处理、线性模型精讲中建立了特征标准化和选型边界的基础认知,SVM 对特征尺度的敏感性在这些文章中也有所体现。如果对树模型系列的调参策略感兴趣,可以参考前文树模型精讲。
如果这篇文章帮助梳理了 SVM 的几何直觉或调参思路,欢迎点赞收藏,这是持续输出最直接的动力。关注专栏,机器学习实战系列还有更多算法精讲和工程实践内容。
有任何疑问或不同见解,欢迎在评论区交流。