统计学习方法笔记(李航)———第二章(感知机)

感知机(perceptron)是Rosenblatt在60年代提出的第一个机器学习模型。尽管比较简单,而且有局限性,但它是后续学习“支持向量机”的基础。本章有两个值得注意的地方:一是对偶形式的理解,二是算法收敛性的理解,这些地方都能和“支持向量机”联系起来。

一、模型的数学形式与图像表示

  1. 数学形式
    感知机模型非常简单,输入空间(特征空间) 为 X ⊆ R n , \mathscr{X} \subseteq R^{n}, XRn, 输出空间为 Y = { + 1 , − 1 } \mathscr{Y}=\{+1,-1\} Y={+1,1}
    决策函数为 f ( x ) = sign ⁡ ( w ⋅ x + b ) , f(x)=\operatorname{sign}(w \cdot x+b), f(x)=sign(wx+b), 其中 w ∈ R n , b w \in R^{n}, \quad b wRn,b 为常数
    符号函数 sign ⁡ ( x ) = { + 1 , x ⩾ 0 − 1 , x < 0 \operatorname{sign}(x)=\left\{\begin{array}{ll}+1, & x \geqslant 0 \\ -1, & x<0\end{array}\right. sign(x)={+1,1,x0x<0
  2. 图像表示
    在这里插入图片描述

上图为 X ⊆ R 2 \mathscr{X} \subseteq R^{2} XR2 时的模型图像。图中的直线为 w ⋅ x + b = 0 w \cdot x+b=0 wx+b=0, 即 w 1 x ( 1 ) + w 2 x ( 2 ) + b = 0 w_{1} x^{(1)}+w_{2} x^{(2)}+b=0 w1x(1)+w2x(2)+b=0 。注意不要和直线函数 y = a x + b \mathrm{y}=\mathrm{ax}+\mathrm{b} y=ax+b 混淆, , , , 因为上图是三维图像在二 维平面上的表示,其输出为离散值, 因此用 “圆点” 和 “交叉” 表示。

感知机的作用是在输入空间中找到一个 “分离超平面" (在上图退化为直线 w X + b = 0 wX+b=0 wX+b=0),将 “线性 可分” 数据集一分为二,此超平面又称 “分界面”

下面看看如何表示输入空间中任一点 x 0 x_{0} x0 到超平面 w ⋅ x + b = 0 w \cdot x+b=0 wx+b=0 的距离:

先看输入空间为平面的情况, 其中点 x 0 x_{0} x0 位于直线法向量 w w w 的另一侧(图中红点),设 x 0 x_{0} x0 在 直线上的投影为 x , ∥ x 0 − x ∥ x, \quad\left\|x_{0}-x\right\| x,x0x 记作 D D D

x 0 = x − D w ∥ w ∥ , x_{0}=x-D \frac{w}{\|w\|}, x0=xDww, 两边同时点乘 w w w

w ⋅ x 0 = w ⋅ x − D w ⋅ w ∥ w ∥ = w ⋅ x − D ∥ w ∥ 2 ∥ w ∥ , w \cdot x_{0}=w \cdot x-D \frac{w \cdot w}{\|w\|}=w \cdot x-D \frac{\|w\|^{2}}{\|w\|}, wx0=wxDwww=wxDww2, 由于 w ⋅ x + b = 0 w \cdot x+b=0 wx+b=0

w ⋅ x 0 + b = − D ∥ w ∥ w \cdot x_{0}+b=-D\|w\| \quad wx0+b=Dw D = − w ⋅ x 0 + b ∥ w ∥ D=-\frac{w \cdot x_{0}+b}{\|w\|} D=wwx0+b

因此图中 x = ( 0 , 0 ) T x=(0,0)^{T} x=(0,0)T w ⋅ x + b = 0 w \cdot x+b=0 wx+b=0 的距离为 D 0 = − b ∥ w ∥ D_{0}=-\frac{b}{\|w\|} D0=wb

在不考虑点与法向量位置的情况下, 也可以记作 D = ∣ w ⋅ x 0 + b ∣ ∥ w ∥ D=\frac{\left|w \cdot x_{0}+b\right|}{\|w\|} D=wwx0+b

  1. 线性可分与线性不可分

以上述二维的输入空间为例,线性可分的数据集是十分罕见的,只要稍微加入一个“异常点”,例如图中的红色“圆点”,就无法用线性模型进行分类了。

更有甚者,不同类型的点以椭圆或三次曲线为边界都是有可能的,这样的数据集都是“非线性可分的”。

对于非线性可分数据集的分类问题,我们在第7章“支持向量机(SVM)”进行深入讨论。

二、策略与算法

  1. 风险函数

如何验证样本点 ( x i , y i ) \left(x_{i}, y_{i}\right) (xi,yi) 是否分类正确? 对于某分离超平面 w ⋅ x + b = 0 w \cdot x+b=0 wx+b=0
分类正确: y i ( w ⋅ x i + b ) > 0 \quad y_{i}\left(w \cdot x_{i}+b\right)>0 yi(wxi+b)>0

设超平面S的误分类点集合为M,则误分类点到超平面的总距离为(误分类点需增加负号表示距 离 ) − 1 ∥ w ∥ ∑ x i ∈ M y i ( w ⋅ x i + b ) , )-\frac{1}{\|w\|} \sum_{x_{i} \in M} y_{i}\left(w \cdot x_{i}+b\right), )w1xiMyi(wxi+b), 我们的目标是使得误分类点总距离最小, 由于 ∥ w ∥ \|w\| w 为常 数, 对求最小值没有影响, 因此不考虑 1 ∥ w ∥ \frac{1}{\|w\|} w1

于是将感知机 “经验风险函数" \quad 定义为 L ( w , b ) = − ∑ x i ∈ M y i ( w ⋅ x i + b ) \quad L(w, b)=-\sum_{x_{i} \in M} y_{i}\left(w \cdot x_{i}+b\right) L(w,b)=xiMyi(wxi+b)

  1. 算法 Nabla算子

∇ = ( ∂ ∂ x , ∂ ∂ y , ∂ ∂ z ) T , \nabla=\left(\frac{\partial}{\partial x}, \frac{\partial}{\partial y}, \frac{\partial}{\partial z}\right)^{T}, =(x,y,z)T, 相当于求 “梯度”

∇ w = ∂ ∂ w ∇ b = ∂ ∂ b \nabla_{w}=\frac{\partial}{\partial w} \nabla_{b}=\frac{\partial}{\partial b} w=wb=b

注意: 由于 w ∈ R n , w \in R^{n}, wRn, 因此

∇ w = ∂ ∂ w = ( ∂ ∂ w 1 , ∂ ∂ w 2 , … ∂ ∂ w n ) T \nabla_{w}=\frac{\partial}{\partial w}=\left(\frac{\partial}{\partial w_{1}}, \frac{\partial}{\partial w_{2}}, \ldots \frac{\partial}{\partial w_{n}}\right)^{T} w=w=(w1,w2,wn)T

设误分类点的集合M固定, 经验风险函数的梯度为

∇ w L ( w , b ) = − ∑ x i ∈ M y i x i \nabla_{w} L(w, b)=-\sum_{x_{i} \in M} y_{i} x_{i} wL(w,b)=xiMyixi

∇ b L ( w , b ) = − ∑ x i ∈ M y i \nabla_{b} L(w, b)=-\sum_{x_{i} \in M} y_{i} bL(w,b)=xiMyi

算法的原始形式

对于训练集T, 输入空间 (特征空间) 为 X ⊆ R n , \mathscr{X} \subseteq R^{n}, XRn, 输出空间为 Y = { + 1 , − 1 } \mathscr{Y}=\{+1,-1\} Y={+1,1}, 学习率 为 η ( 0 ≤ η ≤ 1 ) , \eta(0 \leq \eta \leq 1), η(0η1), 感知机模型 f ( x ) = sign ⁡ ( w ⋅ x + b ) f(x)=\operatorname{sign}(w \cdot x+b) f(x)=sign(wx+b)

(1) 选取初值 w 0 , b 0 w_{0}, b_{0} w0,b0
(2) 在训练集中选取一个数据 ( x i , y i ) \left(x_{i}, y_{i}\right) (xi,yi)
(3) 如果 y i ( w ⋅ x i + b ) ≤ 0 , y_{i}\left(w \cdot x_{i}+b\right) \leq 0, yi(wxi+b)0, 即分类错误, 则将参数更新为
w ← w + η y i x i b ← b + η y i \begin{array}{c} w \leftarrow w+\eta y_{i} x_{i} \\ b \leftarrow b+\eta y_{i} \end{array} ww+ηyixibb+ηyi
(4) 转至 (2) 直至训练集中没有误分类点

算法的对偶形式
关于为什么要有对偶形式可以参考:如何理解感知机学习算法的对偶形式?希望能解答大家疑惑。

对偶形式的目的是,将参数 w和b 表示为 x i x_{i} xi y i y_{i} yi 的线性组合, 即
w = ∑ i = 1 N α i y i x i b = ∑ i = 1 N α i y i \begin{aligned} w &=\sum_{i=1}^{N} \alpha_{i} y_{i} x_{i} \\ b &=\sum_{i=1}^{N} \alpha_{i} y_{i} \end{aligned} wb=i=1Nαiyixi=i=1Nαiyi
注意(关于对偶形式的理解):

此时参数 w w w b ( b\left(\right. b( 共n+1个变量) 被 α 1 , α 2 , … α N ( \alpha_{1}, \alpha_{2}, \ldots \alpha_{N} \quad( α1,α2,αN( 共N个变量) 取代。当n+1>N时, 对 偶模型的复杂度变低了,实现了对 “参数空间” 的降维。

另外, 在SVM中 α \alpha α 是参数 w w w b b b 的约束条件, 如果建立起 α \alpha α 与参数 w w w b b b 的线性关系, 则可以将多重极值转化为单重极值求解。这可以说是SVM的关键一步。

感知机的对偶模型: f ( x ) = sign ⁡ ( ∑ j = 1 N α i y i x i ⋅ x + b ) \quad f(x)=\operatorname{sign}\left(\sum_{j=1}^{N} \alpha_{i} y_{i} x_{i} \cdot x+b\right) f(x)=sign(j=1Nαiyixix+b)

步骤:

(1) α ← 0 , b ← 0 \quad \alpha \leftarrow 0, \quad b \leftarrow 0 α0,b0 α 1 , α 2 , … α N \alpha_{1}, \alpha_{2}, \ldots \alpha_{N} α1,α2,αN 均为0

(2) 在训练集中选取数据 ( x i , y i ) \left(x_{i}, y_{i}\right) (xi,yi)

(3) 如果 y i ( ∑ j = 1 N α i y i x i ⋅ x j + b ) ≤ 0 , y_{i}\left(\sum_{j=1}^{N} \alpha_{i} y_{i} x_{i} \cdot x_{j}+b\right) \leq 0, yi(j=1Nαiyixixj+b)0, y i [ ∑ j = 1 N α i y i ( x i ⋅ x j + 1 ) ] ≤ 0 y_{i}\left[\sum_{j=1}^{N} \alpha_{i} y_{i}\left(x_{i} \cdot x_{j}+1\right)\right] \leq 0 yi[j=1Nαiyi(xixj+1)]0
其中 x i ⋅ x j x_{i} \cdot x_{j} xixj 为一个实数, 可预先在Gram矩阵中算好 (对应 G i j G_{i j} Gij ) 。因此判别式至取决于
α 1 , α 2 , … α N \alpha_{1}, \alpha_{2}, \ldots \alpha_{N} α1,α2,αN
通过上述判别式发现训练集中第i个点为 “误分类" ,则更新对应的系数 α i \alpha_{i} αi
α i ← α i + η b ← b + η y i \begin{array}{l} \alpha_{i} \leftarrow \alpha_{i}+\eta \\ b \leftarrow b+\eta y_{i} \end{array} αiαi+ηbb+ηyi
(4) 转至 (2) 直到没有误分类点

(5) 求w 和 b , \mathrm{b}, b, 得到原始模型 f ( x ) = sign ⁡ ( w ⋅ x + b ) f(x)=\operatorname{sign}(w \cdot x+b) f(x)=sign(wx+b)
w = ∑ i = 1 N α i y i x i b = ∑ i = 1 N α i y i \begin{aligned} w &=\sum_{i=1}^{N} \alpha_{i} y_{i} x_{i} \\ b &=\sum_{i=1}^{N} \alpha_{i} y_{i} \end{aligned} wb=i=1Nαiyixi=i=1Nαiyi

三、算法收签性

书中定理2.1(Novikoff) 证明了“线性可分" 数据集在感知机算法作用下的收剑性, 书中已给出 十分详细的推导过程。我们看看如何理解最后的结论:

k ≤ ( R γ ) 2 k \leq\left(\frac{R}{\gamma}\right)^{2} k(γR)2
其中, k k k 为误分类次数, 即感知机算法的迭代次数。上式给出了迭代次数的上界。

γ \gamma γ R R R 表示什么?

γ = min ⁡ i { y i ( w o p t ⋅ x i + b o p t ) } , \gamma=\min _{i}\left\{y_{i}\left(w_{o p t} \cdot x_{i}+b_{o p t}\right)\right\}, γ=mini{yi(woptxi+bopt)}, 由于 w o p t ⋅ x + b o p t = 0 w_{o p t} \cdot x+b_{o p t}=0 woptx+bopt=0 为已知 “分界面” , , ,

因此 y i ( w o p t ⋅ x i + b o p t ) = ∣ w o p t ⋅ x i + b o p t ∣ = ∣ w o p t ⋅ x i + b o p t ∣ ∥ w o p t ∥ , y_{i}\left(w_{o p t} \cdot x_{i}+b_{o p t}\right)=\left|w_{o p t} \cdot x_{i}+b_{o p t}\right|=\frac{\left|w_{o p t} \cdot x_{i}+b_{o p t}\right|}{\left\|w_{o p t}\right\|}, yi(woptxi+bopt)=woptxi+bopt=woptwoptxi+bopt, 根据点到超平面的距
离公式, γ \quad \gamma γ 实际上是数据点 x i x_{i} xi 到 “分界面" 的最短距离。

R = max ⁡ 1 ≤ i ≤ N ∥ x ^ i ∥ , R=\max _{1 \leq i \leq N}\left\|\hat{x}_{i}\right\|, R=max1iNx^i, 表示离原点最远的数据点 x i x_{i} xi 的距离。

R R R 表示数据越分散, 找到 “分界面" 的难度越大, 需要的迭代次数就越多; 而 γ \gamma γ 则表示, 离 “分界面” 最近的点对于迭代次数影响最大, 也就是数据集里最重要的点, 此结论推广后形成了 “支持向量” 的概念。

四、code实例

拿出 iris 数据集中两个分类的数据和[sepal length,sepal width]作为特征

import pandas as pd
import numpy as np
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
%matplotlib inline

# load data
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['label'] = iris.target

df.columns = [
    'sepal length', 'sepal width', 'petal length', 'petal width', 'label'
]
df.label.value_counts()
'''
2    50
1    50
0    50
Name: label, dtype: int64
'''

在这里插入图片描述

plt.scatter(df[:50]['sepal length'], df[:50]['sepal width'], label='0')
plt.scatter(df[50:100]['sepal length'], df[50:100]['sepal width'], label='1')
plt.xlabel('sepal length')
plt.ylabel('sepal width')
plt.legend()

在这里插入图片描述

data = np.array(df.iloc[:100, [0, 1, -1]])

X, y = data[:,:-1], data[:,-1]

y = np.array([1 if i == 1 else -1 for i in y])

感知机


# 数据线性可分,二分类数据
# 此处为一元一次线性方程
class Model:
    def __init__(self):
        self.w = np.ones(len(data[0]) - 1, dtype=np.float32)
        self.b = 0
        self.l_rate = 0.1
        # self.data = data

    def sign(self, x, w, b):
        y = np.dot(x, w) + b
        return y

    # 随机梯度下降法
    def fit(self, X_train, y_train):
        is_wrong = False
        while not is_wrong:
            wrong_count = 0
            for d in range(len(X_train)):
                X = X_train[d]
                y = y_train[d]
                if y * self.sign(X, self.w, self.b) <= 0:
                    self.w = self.w + self.l_rate * np.dot(y, X)
                    self.b = self.b + self.l_rate * y
                    wrong_count += 1
            if wrong_count == 0:
                is_wrong = True
        return 'Perceptron Model!'

    def score(self):
        pass
perceptron = Model()
perceptron.fit(X, y)
#'Perceptron Model!'
x_points = np.linspace(4, 7, 10)
y_ = -(perceptron.w[0] * x_points + perceptron.b) / perceptron.w[1]
plt.plot(x_points, y_)

plt.plot(data[:50, 0], data[:50, 1], 'bo', color='blue', label='0')
plt.plot(data[50:100, 0], data[50:100, 1], 'bo', color='orange', label='1')
plt.xlabel('sepal length')
plt.ylabel('sepal width')
plt.legend()

在这里插入图片描述scikit-learn 实例

from sklearn.linear_model import Perceptron

clf = Perceptron(fit_intercept=False, max_iter=1000, shuffle=False)
clf.fit(X, y)
'''
Perceptron(alpha=0.0001, class_weight=None, early_stopping=False, eta0=1.0,
           fit_intercept=False, max_iter=1000, n_iter_no_change=5, n_jobs=None,
           penalty=None, random_state=0, shuffle=False, tol=0.001,
           validation_fraction=0.1, verbose=0, warm_start=False)
'''
# Weights assigned to the features.
print(clf.coef_)
#[[ 16.3 -24.2]]

# 截距 Constants in decision function.
print(clf.intercept_)
#[0.]
x_ponits = np.arange(4, 8)
y_ = -(clf.coef_[0][0]*x_ponits + clf.intercept_)/clf.coef_[0][1]
plt.plot(x_ponits, y_)

plt.plot(data[:50, 0], data[:50, 1], 'bo', color='blue', label='0')
plt.plot(data[50:100, 0], data[50:100, 1], 'bo', color='orange', label='1')
plt.xlabel('sepal length')
plt.ylabel('sepal width')
plt.legend()

在这里插入图片描述

习题

习题2.1
 Minsky 与 Papert 指出:感知机因为是线性模型,所以不能表示复杂的函数,如异或 (XOR)。验证感知机为什么不能表示异或。

解答:

对于异或函数XOR,全部的输入与对应的输出如下:

x ( 1 ) x ( 2 ) y 1 1 − 1 1 − 1 1 − 1 1 1 − 1 − 1 − 1 \begin{array}{|c|c|c|} \hline \mathrm{x}^{(1)} & \mathrm{x}^{(2)} & \mathrm{y} \\ \hline 1 & 1 & -1 \\ \hline 1 & -1 & 1 \\ \hline-1 & 1 & 1 \\ \hline-1 & -1 & -1 \\ \hline \end{array} x(1)1111x(2)1111y1111

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

x1 = [1, 1, -1, -1]
x2 = [1, -1, 1, -1]
y = [-1, 1, 1, -1]
x1 = np.array(x1)
x2 = np.array(x2)
y = np.array(y)
data = np.c_[x1, x2, y]
data = pd.DataFrame(data, index=None, columns=['x1', 'x2', 'y'])
data.head()

positive = data.loc[data['y'] == 1]
negative = data.loc[data['y'] == -1]

plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.xticks([-2, -1, 0, 1, 2])
plt.yticks([-2, -1, 0, 1, 2])
plt.xlabel("x1")
plt.ylabel("x2")
plt.plot(positive['x1'], positive['x2'], "ro")
plt.plot(negative['x1'], negative['x2'], "gx")
plt.show()

在这里插入图片描述
显然感知机无法使用一条直线将两类样本划分,异或问题是线性不可分的。

习题2.2
  模仿例题 2.1,构建从训练数据求解感知机模型的例子

from sklearn.linear_model import Perceptron
import numpy as np

X_train = np.array([[3, 3], [4, 3], [1, 1]])
y = np.array([1, 1, -1])

perceptron_model = Perceptron()
perceptron_model.fit(X_train, y)
print("w:", perceptron_model.coef_, "\nb:", perceptron_model.intercept_, "\n")

result = perceptron_model.predict(X_train)
print(result)
'''
w: [[1. 0.]] 
b: [-2.] 

[ 1  1 -1]
'''

习题2.3

证明以下定理:样本集线性可分的充分必要条件是正实例点所构成的凸壳与负实例点所构成的凸壳互不相交。

解答:

  • 第1步:首先给出凸壳与线性可分的定义,定义如下:

凸壳

定义1:设集合S ⊂ R n , \subset \mathrm{R}^{\mathrm{n}}, Rn, 是由R n ^{\mathrm{n}} n 中的k个点所组成的集合, 即S = { x 1 , x 2 , ⋯   , x k } ∘ =\left\{\mathrm{x}_{1}, \mathrm{x}_{2}, \cdots, \mathrm{x}_{\mathrm{k}}\right\}_{\circ} ={x1,x2,,xk} 定义S的凸壳conv ( S ) (\mathrm{S}) (S) 为 :
conv ⁡ ( S ) = { x = ∑ i = 1 k λ i x i ∣ ∑ i = 1 k λ i = 1 , λ i ⩾ 0 , i = 1 , 2 , ⋯   , k } \operatorname{conv}(\mathrm{S})=\left\{\mathrm{x}=\sum_{\mathrm{i}=1}^{\mathrm{k}} \lambda_{\mathrm{i}} \mathrm{x}_{\mathrm{i}} \mid \sum_{\mathrm{i}=1}^{\mathrm{k}} \lambda_{\mathrm{i}}=1, \lambda_{\mathrm{i}} \geqslant 0, \mathrm{i}=1,2, \cdots, \mathrm{k}\right\} conv(S)={x=i=1kλixii=1kλi=1,λi0,i=1,2,,k}
说明:凸壳是一个集合, 对于所有可能的 λ i , i = 1 , 2 , ⋯   , k \lambda_{\mathrm{i}}, \mathrm{i}=1,2, \cdots, \mathrm{k} λi,i=1,2,,k 只要满足 ∑ i = 1 k λ i = 1 , \sum_{i=1}^{\mathrm{k}} \lambda_{\mathrm{i}}=1, i=1kλi=1, 那么 x = ∑ i = 1 k \mathrm{x}=\sum_{\mathrm{i}=1}^{\mathrm{k}} x=i=1k 即为凸壳中的元素, \quad 凸壳可以用二维的图形表
示如下:
在这里插入图片描述
线性可分
定义2:给定一个数据集
T = { ( x 1 , y 1 ) , ( x 2 , y 2 ) , ⋯   , ( x n , y n ) } \mathrm{T}=\left\{\left(\mathrm{x}_{1}, \mathrm{y}_{1}\right),\left(\mathrm{x}_{2}, \mathrm{y}_{2}\right), \cdots,\left(\mathrm{x}_{\mathrm{n}}, \mathrm{y}_{\mathrm{n}}\right)\right\} T={(x1,y1),(x2,y2),,(xn,yn)}
其中 x i ∈ X = R n , y i ∈ Y = { + 1 , − 1 } , i = 1 , 2 , ⋯   , n , \mathrm{x}_{\mathrm{i}} \in \mathcal{X}=\mathrm{R}_{\mathrm{n}}, \mathrm{y}_{\mathrm{i}} \in \mathcal{Y}=\{+1,-1\}, \mathrm{i}=1,2, \cdots, \mathrm{n}, xiX=Rn,yiY={+1,1},i=1,2,,n, 如果存在某个超平面S : w ⋅ x + b = 0 , : \quad \mathrm{w} \cdot \mathrm{x}+\mathrm{b}=0, :wx+b=0, 能够将数据集的正实例点和负实例点完全正确划分到超平面的两侧,即对所有的正实例点即 y i = + 1 y_i=+1 yi=+1的实例i,有 w ⋅ x i + b > 0 w \cdot x_i + b > 0 wxi+b>0 ,对所有的负实例点即 y i = − 1 y y_i = -1y yi=1y的实例i,有 w ⋅ x i + b < 0 w \cdot x_i + b < 0 wxi+b<0,则称数据集T线性可分, 否则称数据集T 线性不可分。

  • 第2步:证明必要性:线性可分书=>凸壳不相交

假设数据集T 中的正例点集为 S + S_{+} S+, S + S_{+} S+的凸壳为conv ( S + ) , \left(\mathrm{S}_{+}\right), (S+), 负实例点集为 S − S_{-} S, S − S_{-} S 的凸壳为conv ( S − ) , \left(\mathrm{S}_{-}\right), (S), 若T 是线性可分的, 则存在一个超平 面:
w ⋅ x + b = 0 \mathrm{w} \cdot \mathrm{x}+\mathrm{b}=0 wx+b=0
能够将 S + S_{+} S+ S − S_{-} S完全分离。假设对于所有的正例点 x i x_{i} xi,有:
w ⋅ x i + b = ε i \mathrm{w} \cdot \mathrm{x}_{\mathrm{i}}+\mathrm{b}=\varepsilon_{\mathrm{i}} wxi+b=εi
易知 ε i > 0 , i = 1 , 2 , ⋯   , ∣ S + ∣ ∘ \varepsilon_{\mathrm{i}}>0, \mathrm{i}=1,2, \cdots,\left|\mathrm{S}_{+}\right|_{\circ} εi>0,i=1,2,,S+ 若conv ( S + ) \left(\mathrm{S}_{+}\right) (S+) 和conv ( S − ) \left(\mathrm{S}_{-}\right) (S) 相交, 即存在某个元素s, 同时满足 s ∈ conv ⁡ ( S + ) \mathrm{s} \in \operatorname{conv}\left(\mathrm{S}_{+}\right) sconv(S+) 和s ∈ conv ⁡ ( S − ) ∙ \in \operatorname{conv}\left(\mathrm{S}_{-}\right)_{\bullet} conv(S) 对于 conv ⁡ ( S + ) \operatorname{conv}\left(\mathrm{S}_{+}\right) conv(S+) 中的元素 s + \mathrm{s}^{+} s+
w ⋅ s + = w ⋅ ∑ i = 1 k λ i x i = ∑ i = 1 k λ i ( ε i − b ) = ∑ i = 1 k λ i ε i − b \mathrm{w} \cdot \mathrm{s}^{+}=\mathrm{w} \cdot \sum_{\mathrm{i}=1}^{\mathrm{k}} \lambda_{\mathrm{i}} \mathrm{x}_{\mathrm{i}}=\sum_{\mathrm{i}=1}^{\mathrm{k}} \lambda_{\mathrm{i}}\left(\varepsilon_{\mathrm{i}}-\mathrm{b}\right)=\sum_{\mathrm{i}=1}^{\mathrm{k}} \lambda_{\mathrm{i}} \varepsilon_{\mathrm{i}}-\mathrm{b} ws+=wi=1kλixi=i=1kλi(εib)=i=1kλiεib

因此w・ s + + b = ∑ i = 1 k λ i ε i > 0 , \mathrm{s}^{+}+\mathrm{b}=\sum_{\mathrm{i}=1}^{\mathrm{k}} \lambda_{\mathrm{i}} \varepsilon_{\mathrm{i}}>0, s++b=i=1kλiεi>0, 同理对于S_中的元素 s − \mathrm{s}^{-} s 有w・s − + b = ∑ i = 1 k λ i ε i < 0 ^{-}+\mathrm{b}=\sum_{\mathrm{i}=1}^{\mathrm{k}} \lambda_{\mathrm{i}} \varepsilon_{\mathrm{i}}<0 +b=i=1kλiεi<0

由于s ∈ conv ⁡ ( S + ) \in \operatorname{conv}\left(\mathrm{S}_{+}\right) conv(S+) 且s ∈ conv ⁡ ( S − ) , \in \operatorname{conv}\left(\mathrm{S}_{-}\right), conv(S), 则w ⋅ s + b = ∑ i = 1 k λ i ε i > 0 \cdot \mathrm{s}+\mathrm{b}=\sum_{\mathrm{i}=1}^{\mathrm{k}} \lambda_{\mathrm{i}} \varepsilon_{\mathrm{i}}>0 s+b=i=1kλiεi>0 且w・s + b = ∑ i = 1 k λ i ε i < 0 , +\mathrm{b}=\sum_{\mathrm{i}=1}^{\mathrm{k}} \lambda_{\mathrm{i}} \varepsilon_{\mathrm{i}}<0, +b=i=1kλiεi<0, 可推出矛盾。

因此, conv ⁡ ( S + ) \operatorname{conv}\left(\mathrm{S}_{+}\right) conv(S+) 和conv ( S − ) \left(\mathrm{S}_{-}\right) (S) 必不相交。从而必要性得证。

  • 第3步:证明充分性=>凸壳不相交刃线性可分

假设数据集T 中的正例点集为S + , S + _{+}, \mathrm{S}_{+} +,S+ 的凸壳为conv ( S + ) , \left(\mathrm{S}_{+}\right), (S+), 负实例点集为S − , S − _{-}, \mathrm{S}_{-} ,S 的凸壳为 conv ⁡ ( S − ) , \operatorname{conv}\left(\mathrm{S}_{-}\right), conv(S), 且conv ( S + ) \left(\mathrm{S}_{+}\right) (S+) 与Conv ( S − ) \left(\mathrm{S}_{-}\right) (S) 不相交。 定义两个点x_, x2的距离为
dist ⁡ ( x 1 , x 2 ) = ∥ x 1 − x 2 ∥ 2 = ( x 1 − x 2 ) 2 \operatorname{dist}\left(\mathrm{x}_{1}, \mathrm{x}_{2}\right)=\left\|\mathrm{x}_{1}-\mathrm{x}_{2}\right\|_{2}=\sqrt{\left(\mathrm{x}_{1}-\mathrm{x}_{2}\right)^{2}} dist(x1,x2)=x1x22=(x1x2)2
定义conv ( S + ) \left(\mathrm{S}_{+}\right) (S+) 和conv ( S − ) \left(\mathrm{S}_{-}\right) (S) 距离为
dist ⁡ ( conv ⁡ ( S + ) , conv ⁡ ( S − ) ) = min ⁡ ∥ s + − s − ∥ , s + ∈ conv ⁡ ( S + ) , s − ∈ conv ⁡ ( S − ) \operatorname{dist}\left(\operatorname{conv}\left(\mathrm{S}_{+}\right), \operatorname{conv}\left(\mathrm{S}_{-}\right)\right)=\min \left\|\mathrm{s}_{+}-\mathrm{s}_{-}\right\|, \mathrm{s}_{+} \in \operatorname{conv}\left(\mathrm{S}_{+}\right), \mathrm{s}_{-} \in \operatorname{conv}\left(\mathrm{S}_{-}\right) dist(conv(S+),conv(S))=mins+s,s+conv(S+),sconv(S)
x + ∈ conv ⁡ ( S + ) , x − ∈ conv ⁡ ( S − ) \mathrm{x}_{+} \in \operatorname{conv}\left(\mathrm{S}_{+}\right), \mathrm{x}_{-} \in \operatorname{conv}\left(\mathrm{S}_{-}\right) x+conv(S+),xconv(S) 且dist ( x + , x − ) = dist ⁡ ( conv ⁡ ( S + ) , conv ⁡ ( S − ) ) ∘ \left(\mathrm{x}_{+}, \mathrm{x}_{-}\right)=\operatorname{dist}\left(\operatorname{conv}\left(\mathrm{S}_{+}\right), \operatorname{conv}\left(\mathrm{S}_{-}\right)\right)_{\circ} (x+,x)=dist(conv(S+),conv(S)) 则对于任意正例点x有dist ( x , x − ) ⩾ dist ⁡ ( x + , x − ) \left(\mathrm{x}, \mathrm{x}_{-}\right) \geqslant \operatorname{dist}\left(\mathrm{x}_{+}, \mathrm{x}_{-}\right) (x,x)dist(x+,x)

同理, 对所有的负例点x有dist ( x , x + ) ⩾ dist ⁡ ( x , x ) \left(\mathrm{x}, \mathrm{x}_{+}\right) \geqslant \operatorname{dist}(\mathrm{x}, \mathrm{x}) (x,x+)dist(x,x) 。 存在超平面
w ⋅ x + b = 0 \mathrm{w} \cdot \mathrm{x}+\mathrm{b}=0 wx+b=0
其中
w = x + + x − w = x_{+} +x_{-} w=x++x

b = − x + ⋅ x + − x − ⋅ x − 2 ⋅ x \begin{array}{c} \mathrm{b}=-\frac{\mathrm{x}_{+} \cdot \mathrm{x}_{+} - \mathrm{x}_{-} \cdot \mathrm{x}_{-}}{2} \cdot \mathrm{x} \end{array} b=2x+x+xxx
则对于所有的正例点x (易知 x ・ x + + b > 0 , x・x_{+}+ \mathrm{b}>0, xx++b>0, 因此若 x + \mathrm{x}_{+} x+ 属于正例点, , , , 则令 x + ≠ x ) \left.\mathrm{x}_{+} \neq \mathrm{x}\right) x+=x)
w ⋅ x + b = ( x + − x − ) ⋅ x − x + ⋅ x + − x − − x − 2 = x + ⋅ x − x − ⋅ x − x + ⋅ x + − x − − x − 2 = ∥ x − − x ∥ 2 2 − ∥ x + − x ∥ 2 2 2 = dist ⁡ ( x , x − ) 2 − dist ⁡ ( x , x + ) 2 2 \begin{aligned} \mathrm{w} \cdot \mathrm{x}+\mathrm{b} &=\left(\mathrm{x}_{+}-\mathrm{x}_{-}\right) \cdot \mathrm{x}-\frac{\mathrm{x}_{+} \cdot \mathrm{x}_{+}-\mathrm{x}_{-}-\mathrm{x}_{-}}{2} \\ &=\mathrm{x}_{+} \cdot \mathrm{x}-\mathrm{x}_{-} \cdot \mathrm{x}-\frac{\mathrm{x}_{+} \cdot \mathrm{x}_{+}-\mathrm{x}_{-}-\mathrm{x}_{-}}{2} \\ &=\frac{\|\mathrm{x}_{-}-\mathrm{x}\|_{2}^{2}-\left\|\mathrm{x}_{+}-\mathrm{x}\right\|_{2}^{2}}{2} \\ &=\frac{\operatorname{dist}\left(\mathrm{x}, \mathrm{x}_{-}\right)^{2}-\operatorname{dist}\left(\mathrm{x}, \mathrm{x}_{+}\right)^{2}}{2} \end{aligned} wx+b=(x+x)x2x+x+xx=x+xxx2x+x+xx=2xx22x+x22=2dist(x,x)2dist(x,x+)2
若dist ( x , x − ) ⩽ dist ⁡ ( x , x + ) ( \left(\mathrm{x}, \mathrm{x}_{-}\right) \leqslant \operatorname{dist}\left(\mathrm{x}, \mathrm{x}_{+}\right)( (x,x)dist(x,x+)( 即线性不可分 ) , ), ), 则dist ( x , x − ) ⩽ dist ⁡ ( x , x + ) ⩽ dist ⁡ ( x , x + ) , \left(\mathrm{x}, \mathrm{x}_{-}\right) \leqslant \operatorname{dist}\left(\mathrm{x}, \mathrm{x}_{+}\right) \leqslant \operatorname{dist}\left(\mathrm{x}, \mathrm{x}_{+}\right), (x,x)dist(x,x+)dist(x,x+), 那么dist ( conv ⁡ ( S + ) , conv ⁡ ( S − ) ) < \left(\operatorname{conv}\left(\mathrm{S}_{+}\right), \operatorname{conv}\left(\mathrm{S}_{-}\right)\right)< (conv(S+),conv(S))<
dist ⁡ ( x + , x − ) , \operatorname{dist}\left(\mathrm{x}_{+}, \mathrm{x}_{-}\right), dist(x+,x), 推出矛盾, 因此dist ( x , x − ) > dist ⁡ ( x , x + ) , \left(\mathrm{x}, \mathrm{x}_{-}\right)>\operatorname{dist}\left(\mathrm{x}, \mathrm{x}_{+}\right), (x,x)>dist(x,x+), 即线性可分, 充分性得证。

补充:用反证法证明dist ( x , x ) > dist ⁡ ( x , x + ) (\mathrm{x}, \mathrm{x})>\operatorname{dist}\left(\mathrm{x}, \mathrm{x}_{+}\right) (x,x)>dist(x,x+)

证明:假设dist ( x , x − ) ⩽ dist ⁡ ( x , x + ) (\mathrm{x}, \mathrm{x}_{-}) \leqslant \operatorname{dist}\left(\mathrm{x}, \mathrm{x}_{+}\right) (x,x)dist(x,x+) 则存在
t = ( x − − x + ) ⋅ ( x − x + ) ∥ x − x + ∥ 2 2  令  x ′ = t x + ( 1 − t ) x + , 则  ( x − − x ′ ) ⋅ ( x + − x ) = 0 \begin{array}{c} \mathrm{t}=\frac{\left(\mathrm{x}_{-}-\mathrm{x}_{+}\right) \cdot\left(\mathrm{x}-\mathrm{x}_{+}\right)}{\left\|\mathrm{x}-\mathrm{x}_{+}\right\|_{2}^{2}} \\ \text { 令 } \mathrm{x}^{\prime}=\mathrm{tx}+(1-\mathrm{t}) \mathrm{x}_{+}, \text {则 }\left(\mathrm{x}_{-}-\mathrm{x}^{\prime}\right) \cdot\left(\mathrm{x}_{+}-\mathrm{x}\right)=0 \end{array} t=xx+22(xx+)(xx+)  x=tx+(1t)x+, (xx)(x+x)=0

易知t ⩽ 1 , \leqslant 1, 1, 先证明t > 0 : >0: >0: 可以将x, x + , x − \mathrm{x}_{+}, \mathrm{x}_{-} x+,x 看做为空间中的三个不同的点, 三条边的长度分别为dist ( x , x + ) , dist ⁡ ( x , x − ) , dist ⁡ ( x + , x − ) \left(\mathrm{x}, \mathrm{x}_{+}\right), \operatorname{dist}\left(\mathrm{x}, \mathrm{x}_{-}\right), \operatorname{dist}\left(\mathrm{x}_{+}, \mathrm{x}_{-}\right) (x,x+),dist(x,x),dist(x+,x)

如上面可知dist ( x , x + ) ⩾ dist ⁡ ( x , x − ) ⩾ dist ⁡ ( x − , x + ) \left(\mathrm{x}, \mathrm{x}_{+}\right) \geqslant \operatorname{dist}\left(\mathrm{x}, \mathrm{x}_{-}\right) \geqslant \operatorname{dist}\left(\mathrm{x}_{-}, \mathrm{x}_{+}\right) (x,x+)dist(x,x)dist(x,x+)

根据三角形的大边对应大角这一特性, 很容易可以看出x_ − x -\mathrm{x} x x + − x − \mathrm{x}_{+}-\mathrm{x}_{-} x+x 之间的夹角小于90度, 因此t > 0 >0 >0 。 那么 dist ⁡ ( x ′ , x − ) < \operatorname{dist}\left(\mathrm{x}^{\prime}, \mathrm{x}_{-}\right)< dist(x,x)< dist ⁡ ( x + , x − ) , \operatorname{dist}\left(\mathrm{x}_{+}, \mathrm{x}_{-}\right), dist(x+,x), 又因为x ′ ^{\prime} 必在conv ( S + ) \left(\mathrm{S}_{+}\right) (S+) 内部, 故推出矛盾, 所以dist ( x , x − ) > dist ⁡ ( x , x + ) \left(\mathrm{x}, \mathrm{x}_{-}\right)>\operatorname{dist}\left(\mathrm{x}, \mathrm{x}_{+}\right) (x,x)>dist(x,x+)

参考自:
黄海广博士
datawhale

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浩波的笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值