3 min read

学习笔记:Support Vector Machines SVM 学习笔记

学习笔记 系列导航

1 Hive/Impala 学习笔记 2017-12-04
2 KS 学习笔记 2017-12-06
3 datacamp pandas DataFrames 学习笔记 2017-12-11
4 datacamp pandas Merging DataFrames 学习笔记 2017-12-13
5 Databases in Python 学习笔记 2017-12-14
6 Data Visualization with Python 学习笔记 2017-12-16
7 Exploratory data analysis in Python 学习笔记 2017-12-17
8 Statistical Thinking in Python (Part-2) 学习笔记 2017-12-18
9 list comprehensions in Python 学习笔记 2017-12-19
10 无监督学习:Unsupervised Learning in Python 学习笔记 2017-12-20
11 学习笔记:Deep Learning in Python 学习笔记 2017-12-22
12 学习笔记:Python 学习的流水笔记 2017-12-25
13 学习笔记:Network Analysis in Python Part 1 学习笔记 2017-12-27
14 学习笔记:XGBoost using Python 学习笔记 2017-12-28
15 学习笔记:Supervised Learning with scikit-learn 学习笔记 2017-12-30
16 学习笔记:Boosting理论部分 学习笔记 2018-01-02
17 学习笔记:Machine Learning with the Experts School Budgets 学习笔记 2018-01-02
18 学习笔记:犯罪心理解析 2018-01-02
19 学习笔记:决策树理论部分 学习笔记 2018-01-03
20 学习笔记:Shell 学习笔记 2018-01-04
21 学习笔记:客户价值定价 学习笔记 2018-01-04
22 学习笔记:Introduction to Git for Data Science 学习笔记 2018-01-06
23 学习笔记:线性代数 整理笔记 2018-01-08
24 学习笔记:退火算法 学习笔记 2018-01-09
25 学习笔记:Fahrenheit 911 视频笔记 2018-01-18
26 学习笔记:pandas debugging 学习笔记 2018-01-19
27 学习笔记:brilliant.org概率论导论 学习笔记 2018-01-22
28 学习笔记:Machine Learning with Tree-Based Models in R 学习笔记 2018-01-22
29 学习笔记:Building Web Applications in R with Shiny 学习笔记 2018-01-25
30 学习笔记:Inference for Numerical Data 学习笔记 2018-01-26
31 学习笔记:Introduction to DataCamp Projects 学习笔记 2018-01-28
32 学习笔记:Working with Web Data in R 学习笔记 2018-01-28
33 学习笔记:三种平均数使用的方式 学习笔记 2018-01-29
34 学习笔记:戒律的复活 每周六更新 2018-01-29
35 学习笔记:Communicating with Data in the Tidyverse 学习笔记 2018-01-31
36 学习笔记:Kaggle R Tutorial on Machine Learning 学习笔记 2018-02-01
37 学习笔记:Kaggle Python Tutorial on Machine Learning 学习笔记 2018-02-02
38 技术:ggridges 山峦图 学习笔记 2018-02-02
39 技术:XGBoost 学习笔记 2018-02-02
40 学习笔记:圆桌派 第三季 视频笔记 2018-02-05
41 学习笔记:基础与技巧整理 2018-02-25
42 集成学习R SuperLearner包学习笔记 2018-03-04
43 学习笔记:英语学习积累:词汇、表达与语法整理 2018-04-09
44 技术:原理与应用学习笔记 2018-04-29
45 学习笔记:魏剑峰英语学习笔记:表达与语法整理 2018-05-02
46 技术:方法与实践学习笔记 2018-05-12
47 无监督学习:主成分分析(PCA)原理与实现学习笔记 2018-05-17
48 技术:特征筛选学习笔记 2018-05-29
49 学习笔记:Planet Money播客学习笔记:经济学话题解析 2018-06-05
50 基础算法系列梯度下降算法详解:原理与优化学习笔记 2018-07-11
51 技术:统计建模学习笔记 2018-07-24
52 技术:指标设计学习笔记 2018-09-20
53 技术:安装与使用基础学习笔记 2018-11-07
54 📈 ggplot 设计思路 学习笔记 2019-12-26
55 🧩 Python 函数编写学习笔记 2019-12-31
56 技术:特征工程之目标编码学习笔记 2020-01-20
57 📚 词向量 学习笔记 2020-07-04
58 主题模型:学习笔记 2020-07-04
59 学习笔记:WSJ 学习笔记 2020-10-19
60 健身:学习笔记 2025-08-19

本文于2026-03-31更新。 如发现问题或者有建议,欢迎提交 Issue

比较主要参考 Ng (2018),相关PPT详见课程PPT

1 Large Margin Classification

Ng (2018, pt. 1)主要介绍三方面内容:

  • SVM的代数定义,ReLU函数的类似。
  • SVM的几何定义,
  • SVM代数和几何定义的联系。

1.1 SVM的代数定义

SVM的代数定义,是逻辑回归的改进。 我们说理解一个公式了一回事,范化一个公式是另一回事。

Abstract is the price of generation.

这里因此尽可能的举特例快速弄懂。 我们知道逻辑回归, \[z=h_{\theta}(x)=\theta^{T}x\] \[y=\frac{1}{1+e^{-z}}\]

1.1.1 几个函数的直观理解

tibble(x = c(-10,10)) %>%
    ggplot(aes(x=x)) +
        stat_function(fun = function(x){exp(-x)}) +
        annotate("text",
                 x = 0, y = 10000, parse = TRUE,
                 label = "y ==  e ^ {-x}")

tibble(x = c(-10,10)) %>%
    ggplot(aes(x=x)) +
        stat_function(fun = function(x){1/(1+exp(-x))}) +
        annotate("text",
                 x = 0, y = 0.5, parse = TRUE,
                 label = "y ==  frac(1, 1 + e ^ {-x})")

我们尽可能的希望,

  • \(y\to1\)时,那么\(\theta^{T}x\geq0\),且\(\theta^{T}x \to + \infty\)
  • \(y\to0\)时,那么\(\theta^{T}x\leq0\),且\(\theta^{T}x \to - \infty\)

1.1.2 损失函数

上面这是我们的假设,我们要用损失函数实现它。

比如 \[\begin{alignat}{2} \text{Cost} & = -[\overbrace{y\log (h_{\theta}(x))}^{\text{y = 1}}+\overbrace{(1-y)\log(1-h_{\theta}(x))}^{\text{y = 0}}] \\ & = -y\log\frac{1}{1+e^{-\theta^Tx}}-(1-y)\log(1-\frac{1}{1+e^{-\theta^Tx}}) \end{alignat}\]

\(y=1\)时,

\[\text{Cost}=-\log\frac{1}{1+e^{-\theta^Tx}}=\log(1+e^{-\theta^Tx})\]

tibble(x = c(-10,10)) %>%
    ggplot(aes(x=x)) +
        stat_function(fun = function(x){log(1+exp(-x))}) +
        annotate("text",
                 x = 0, y = 5, parse = TRUE,
                 label = "y ==  log(1 + e ^ {-x})")

\(y=0\)时,

\[\text{Cost}=-\log(1-\frac{1}{1+e^{-\theta^Tx}})=\log(1+e^{\theta^Tx})\]

tibble(x = c(-10,10)) %>%
    ggplot(aes(x=x)) +
        stat_function(fun = function(x){log(1+exp(x))}) +
        annotate("text",
                 x = 0, y = 5, parse = TRUE,
                 label = "y ==  log(1 + e ^ {x})")

  • \(y=1\)时,只要\(\theta^Tx\geq0\),那么\(Cost=0\)
  • \(y=0\)时,只要\(\theta^Tx\leq0\),那么\(Cost=0\)

这个就是ReLu函数。

注意这里我们可以用更严格的条件,

  • \(y=1\)时,只要\(\theta^Tx\geq1\),那么\(Cost=0\)
  • \(y=0\)时,只要\(\theta^Tx\leq-1\),那么\(Cost=0\)

这里就把逻辑回归,中间这种0两边突变的情况考虑了,因此对cutoff不那么灵敏了,因此效果好、稳定。

\(C\)参数调整正则化强度。

1.2 SVM的几何定义

显然C很大的时候,分割线旋转,导致将outlier进行了切分。 旋转的原因见 1.3

1.3 SVM代数和几何定义的联系

这是代数定义里面我们要完成的任务,

  • \(y=1\)时,只要\(\theta^Tx\geq1\),那么\(Cost=0\)
  • \(y=0\)时,只要\(\theta^Tx\leq-1\),那么\(Cost=0\)
  • 也就是说,\(|\theta^Tx|\geq1\) \(\to\) 点积\(x_i|\theta|\geq1\) \(\to\) 因此\(x_i\)要足够大。 \(\to\) \(x\)\(\theta\)上的投影要足够大。 。

因此我们会发现, \(x\)\(\theta\)上的投影要足够大。 等价于 \(x\)\(\theta\)的法向量距离足够大。 因此这就是代数定义和图形定义的联系。

2 kernel 函数

2.1 损失函数

1.1.2的损失函数是线性模型,采用了类似于Relu函数的操作 这里介绍一个基于正态分布完成了的cost函数,这种函数在SNE也有涉及。 可以处理类别被包围的情况。

我们知道多项式,例如\(x_1^2 +x_2^2\)可以表达圆,通过\(x_1^2 +x_2^2\sim0\)的关系,完成圆内和圆外的关系, 但是除了多项式,还有正态分布这种方式。

如图我们在图中选择这些点,通过将该点作为频率分布图的中心,做高斯分布, 那么周围\(\forall x\)离该点\(l^{(1)}\)的距离判断,两点的相似程度,similarity。 那么,

\[f_1 = \text{similarity}(x,l^{(1)})=\exp(-\frac{||x-l^{(1)}||^2}{2\sigma^2}) \in [0,1]\]

因此可得到,

Gaussian Kernel函数,

\[\begin{alignat}{2} f_1 & = \text{similarity}(x,l^{(1)})=\exp(-\frac{||x-l^{(1)}||^2}{2\sigma^2}) \in [0,1]\\ f_2 & = \text{similarity}(x,l^{(2)})=\exp(-\frac{||x-l^{(2)}||^2}{2\sigma^2}) \in [0,1]\\ \cdots \\ f_m & = \text{similarity}(x,l^{(m)})=\exp(-\frac{||x-l^{(m)}||^2}{2\sigma^2}) \in [0,1]\\ \end{alignat}\]

\[\begin{alignat}{2} \hat y=1\gets & \theta^Tx\geq 1\\ & \theta_0 + \theta_1x_1 + \theta_2x_2 + ...\\ \end{alignat}\]

现在换成,

\[\begin{alignat}{2} \hat y=1\gets & \theta^Tf\geq 1\\ & \theta_0f_1 + \theta_1f_1 + \theta_2f_2 + ...\\ \end{alignat}\]

假设\(f_0 = 1\)

  • 我们认为\(y=1\)在圆外,因此\(\theta^Tf\geq >0\)明显在圆外,
  • 我们认为\(y=0\)在圆内,因此\(\theta^Tf\leq <0\)明显在圆内。

这个时候我们把这个关系整理到cost函数中,就是

\[\min_{\theta}C\sum_{i=1}^my^{(i)}cost_1(\theta^Tf^{(i)})+(1-y^{(i)})cost_0(\theta^Tf^{(i)})+\frac{1}{2}\sum_{j=1}^m\theta_j^2\]

后面的思路就是应用到SVM了。 因此现在SVM我们掌握了两种方法,一种是ReLu函数,一种是Gaussian Kernel函数。

3 实际操作

3.1 SVM线性核函数的推荐

  • Use linear kernel when number of features is larger than number of observations (3.2).
  • Use gaussian kernel when number of observations is larger than number of features.
  • If number of observations is larger than 50,000 speed could be an issue when using gaussian kernel; hence, one might want to use linear kernel. (Temlyakov 2013)

总计一句话就是,都先尝试线性模型,在线性模型效果不好的情况下,再考虑使用非线性模型。

  • polynomial -> 把直线变弯
  • radial -> 变球

3.2 变量多用线性模型

当特征向量的数量远大于样本数量,样本之间会比较稀疏, 因此更高维度的空间,样本之间会比较稀疏,更容易线性可分。 因此用线性模型。 但是,如果数据密集,就不容易线性可分了。

  • 因此当样本量小于特征向量时,特征已经足够,更多体现了线性关系(甚至可能存在共线性关系),这时就没有必要使用Gaussian Kernel函数了。
  • 因此当样本量大于特征向量时,
    • 特征不足够,
    • 且样本不是特别大而影响速度,
    • 需要体现非线性关系,这时就需要使用Gaussian Kernel函数了。见CSDN博客,第6和12点

参考文献

Ng, Andrew. 2018. “Support Vector Machines.” 2018. https://www.coursera.org/learn/machine-learning/home/week/7.
Temlyakov, Kirill. 2013. “Linear Kernel and Non-Linear Kernel for Support Vector Machine?” 2013. https://stats.stackexchange.com/questions/73032/linear-kernel-and-non-linear-kernel-for-support-vector-machine.

学习笔记 系列导航

1 Hive/Impala 学习笔记 2017-12-04
2 KS 学习笔记 2017-12-06
3 datacamp pandas DataFrames 学习笔记 2017-12-11
4 datacamp pandas Merging DataFrames 学习笔记 2017-12-13
5 Databases in Python 学习笔记 2017-12-14
6 Data Visualization with Python 学习笔记 2017-12-16
7 Exploratory data analysis in Python 学习笔记 2017-12-17
8 Statistical Thinking in Python (Part-2) 学习笔记 2017-12-18
9 list comprehensions in Python 学习笔记 2017-12-19
10 无监督学习:Unsupervised Learning in Python 学习笔记 2017-12-20
11 学习笔记:Deep Learning in Python 学习笔记 2017-12-22
12 学习笔记:Python 学习的流水笔记 2017-12-25
13 学习笔记:Network Analysis in Python Part 1 学习笔记 2017-12-27
14 学习笔记:XGBoost using Python 学习笔记 2017-12-28
15 学习笔记:Supervised Learning with scikit-learn 学习笔记 2017-12-30
16 学习笔记:Boosting理论部分 学习笔记 2018-01-02
17 学习笔记:Machine Learning with the Experts School Budgets 学习笔记 2018-01-02
18 学习笔记:犯罪心理解析 2018-01-02
19 学习笔记:决策树理论部分 学习笔记 2018-01-03
20 学习笔记:Shell 学习笔记 2018-01-04
21 学习笔记:客户价值定价 学习笔记 2018-01-04
22 学习笔记:Introduction to Git for Data Science 学习笔记 2018-01-06
23 学习笔记:线性代数 整理笔记 2018-01-08
24 学习笔记:退火算法 学习笔记 2018-01-09
25 学习笔记:Fahrenheit 911 视频笔记 2018-01-18
26 学习笔记:pandas debugging 学习笔记 2018-01-19
27 学习笔记:brilliant.org概率论导论 学习笔记 2018-01-22
28 学习笔记:Machine Learning with Tree-Based Models in R 学习笔记 2018-01-22
29 学习笔记:Building Web Applications in R with Shiny 学习笔记 2018-01-25
30 学习笔记:Inference for Numerical Data 学习笔记 2018-01-26
31 学习笔记:Introduction to DataCamp Projects 学习笔记 2018-01-28
32 学习笔记:Working with Web Data in R 学习笔记 2018-01-28
33 学习笔记:三种平均数使用的方式 学习笔记 2018-01-29
34 学习笔记:戒律的复活 每周六更新 2018-01-29
35 学习笔记:Communicating with Data in the Tidyverse 学习笔记 2018-01-31
36 学习笔记:Kaggle R Tutorial on Machine Learning 学习笔记 2018-02-01
37 学习笔记:Kaggle Python Tutorial on Machine Learning 学习笔记 2018-02-02
38 技术:ggridges 山峦图 学习笔记 2018-02-02
39 技术:XGBoost 学习笔记 2018-02-02
40 学习笔记:圆桌派 第三季 视频笔记 2018-02-05
41 学习笔记:基础与技巧整理 2018-02-25
42 集成学习R SuperLearner包学习笔记 2018-03-04
43 学习笔记:英语学习积累:词汇、表达与语法整理 2018-04-09
44 技术:原理与应用学习笔记 2018-04-29
45 学习笔记:魏剑峰英语学习笔记:表达与语法整理 2018-05-02
46 技术:方法与实践学习笔记 2018-05-12
47 无监督学习:主成分分析(PCA)原理与实现学习笔记 2018-05-17
48 技术:特征筛选学习笔记 2018-05-29
49 学习笔记:Planet Money播客学习笔记:经济学话题解析 2018-06-05
50 基础算法系列梯度下降算法详解:原理与优化学习笔记 2018-07-11
51 技术:统计建模学习笔记 2018-07-24
52 技术:指标设计学习笔记 2018-09-20
53 技术:安装与使用基础学习笔记 2018-11-07
54 📈 ggplot 设计思路 学习笔记 2019-12-26
55 🧩 Python 函数编写学习笔记 2019-12-31
56 技术:特征工程之目标编码学习笔记 2020-01-20
57 📚 词向量 学习笔记 2020-07-04
58 主题模型:学习笔记 2020-07-04
59 学习笔记:WSJ 学习笔记 2020-10-19
60 健身:学习笔记 2025-08-19