目录

前言

本文只做复习使用,只给出关键算法描述和证明。

MLE/MAP

给定NN个样本对{(X(i),y(i)),i=1,,N}\{(X^{(i)}, y^{(i)}), i = 1, \cdots, N\},其中y{Ck,k=1,,K}y \in \{C_k, k = 1, \cdots, K\},要求估计参数模型P(Xθ)P(X | \theta)的参数θ\theta,使之最能描述给定数据分布。

最大似然估计(MLE)

优化目标:θ^=argmaxP(Dθ)定义:L(Dθ)=P(Dθ)=iP(X(i)θ)取对数:logL(Dθ)=ilogP(X(i)θ)求取极值:θlogL(Dθ)=0θ^\begin{aligned} 优化目标:& \hat{\theta} = \arg \max P(D | \theta) \\ 定义:& L(D | \theta) = P(D | \theta) = \prod_i P(X^{(i)} | \theta) \\ 取对数:& \log L(D | \theta) = \sum_i \log P(X^{(i)} | \theta) \\ 求取极值:& \frac{\partial}{\partial \theta} \log L(D | \theta) = 0 \Rightarrow \hat{\theta} \end{aligned}

最大后验概率估计(MAP)

优化目标:θ^=argmaxP(θD)其中:P(θD)=P(Dθ)P(θ)P(D)P(θ)为给定的参数先验概率分布定义:L(θD)=P(Dθ)P(θ)=iP(X(i)θ)P(θ)取对数:logL(θD)=ilogP(X(i)θ)+logP(θ)求取极值:θlogL(θD)=0θ^\begin{aligned} 优化目标:& \hat{\theta} = \arg \max P(\theta | D) \\ 其中:& P(\theta | D) = \frac{P(D | \theta) P(\theta)}{P(D)} \\ & P(\theta)为给定的参数先验概率分布 \\ 定义:& L(\theta | D) = P(D | \theta) P(\theta) = \prod_i P(X^{(i)} | \theta) \cdot P(\theta) \\ 取对数:& \log L(\theta | D) = \sum_i \log P(X^{(i)} | \theta) + \log P(\theta) \\ 求取极值:& \frac{\partial}{\partial \theta} \log L(\theta | D) = 0 \Rightarrow \hat{\theta} \end{aligned}

线性回归/逻辑斯蒂回归

给定NN个样本对{(X(i),y(i)),i=1,,N}\{(X^{(i)}, y^{(i)}), i = 1, \cdots, N\},记样本矩阵XN×nX_{N \times n}

线性回归

标签信息:yR1,定义模型:y^1×1=wn×1Txn×1+b增广后:y^1×1=wn×1Txn×1{w1=bx1=1MSE作为损失,则总体损失:L(y^,y)=1Ni=1N12(y^(i)y(i))2求取梯度:Lwj=1Ni=1N(y^(i)y(i))y^(i)wj=1Ni=1N(y^(i)y(i))xj(i)梯度下降:wj:=wjαLwj\begin{aligned} 标签信息:& y \in \mathcal{R}^1, 定义模型:\hat{y}_{1\times 1} = w_{n \times 1}^T x_{n \times 1} + b \\ 增广后:& \hat{y}_{1\times 1} = w_{n \times 1}^T x_{n \times 1} \begin{cases} w_1 = b \\ x_1 = 1 \end{cases} \\ MSE作为损失,则总体损失:& L(\hat{y}, y) = \frac{1}{N} \sum_{i=1}^N \frac{1}{2} (\hat{y}^{(i)} - y^{(i)})^2 \\ 求取梯度:& \frac{\partial L}{\partial w_j} = \frac{1}{N} \sum_{i=1}^N (\hat{y}^{(i)} - y^{(i)}) \frac{\partial \hat{y}^{(i)}}{\partial w_j} = \frac{1}{N} \sum_{i=1}^N (\hat{y}^{(i)} - y^{(i)}) x^{(i)}_j \Rightarrow \\ 梯度下降:& w_j := w_j - \alpha \frac{\partial L}{\partial w_j} \end{aligned}

若描述为矩阵

标签信息YRN定义模型:Y^N×1=XN×(n+1)w(n+1)×1总体损失:L(Y^,Y)=1N12Y^Y22=1N12(Y^Y)T(Y^Y)}L(Y^,Y)=12N(wTXTXw2YTXw+YTY)求取梯度:Lw=12N(2XTXw2XTY)=0{梯度下降:w:=wαLw解析解:w^=(XTX+λI)1XTX+Y\begin{aligned} \left.\begin{aligned} & 标签信息 Y \in R^{N} \\ 定义模型:& \hat{Y}_{N \times 1} = X_{N \times (n + 1)} w_{(n + 1) \times 1} \\ 总体损失:& L(\hat{Y}, Y) = \frac{1}{N} \cdot \frac{1}{2} || \hat{Y} - Y ||_2^2 = \frac{1}{N} \cdot \frac{1}{2} (\hat{Y} - Y)^T(\hat{Y} - Y) \end{aligned}\right\} \Rightarrow \\ L(\hat{Y}, Y) = \frac{1}{2 N} (w^T X^T X w - 2 Y^T X w + Y^T Y) \\ 求取梯度: \frac{\partial L}{\partial w} = \frac{1}{\cancel{2} N} (\cancel{2} X^T X w - \cancel{2} X^T Y) = 0 \Rightarrow \\ \begin{cases} 梯度下降:& w := w - \alpha \frac{\partial L}{\partial w} \\ 解析解:& \hat{w}^* = \underbrace{(X^T X + \lambda I)^{-1} X^T}_{X^+} Y \end{cases} \end{aligned}

逻辑斯蒂回归(LR)

标签信息:y{0,1}定义模型:{y^=σ(z)z=wTX+b其中σ(z)=11+exp(z)样本X服从01分布:P(X)=(1y^)1y(y^)y(y^(i)为直接待估参数)MLEL(Dw)=iP(X(i))logL(Dw)=ilogP(X(i))优化目标:w^=argmaxL(Dw)=argmaxlogL(Dw)求取极值:Lwj=wjilogP(X(i))=wjilog(1y^(i))1y(i)(y^(i))y(i)=wji(1y(i))log(1y^(i))+wjiy(i)logy^(i)=i(1y(i))11y^(i)(y(i)wj)+iy(i)1y^(i)(y(i)wj)其中:y(i)wj=σ(z(i))z(i)wj=σ(z(i))(1σ(z(i)))xj(i)Lwj=i(1y(i))11y^(i)σ(z(i))(1σ(z(i)))xj(i)+iy(i)1y^(i)σ(z(i))(1σ(z(i)))xj(i)=i(y(i)y^(i))xj(i)梯度下降:wj:=wjαLwj\begin{aligned} 标签信息: y \in \{0, 1\} \\ 定义模型:& \begin{cases} \hat{y} = \sigma(z) \\ z = w^T X + b \end{cases} \\ & 其中 \sigma(z) = \frac{1}{1 + \exp(-z)} \\ 样本X服从0-1分布:& P(X) = (1 - \hat{y})^{1 - y} (\hat{y})^{y} (\hat{y}^{(i)}为直接待估参数) \\ MLE:& L(D | w) = \prod_i P(X^{(i)}) \Rightarrow \log L(D | w) = \sum_i \log P(X^{(i)}) \\ 优化目标:& \hat{w} = \arg \max L(D | w) = \arg \max \log L(D | w) \\ 求取极值:& \begin{aligned} \frac{\partial L}{\partial w_j} & = \frac{\partial}{\partial w_j} \sum_i \log P(X^{(i)}) \\ & = \frac{\partial}{\partial w_j} \sum_i \log (1 - \hat{y}^{(i)})^{1 - y^{(i)}} (\hat{y}^{(i)})^{y^{(i)}} \\ & = \frac{\partial}{\partial w_j} \sum_i (1 - y^{(i)}) \log (1 - \hat{y}^{(i)}) + \frac{\partial}{\partial w_j} \sum_i y^{(i)} \log \hat{y}^{(i)} \\ & = \sum_i (1 - y^{(i)}) \frac{1}{1 - \hat{y}^{(i)}} (- \frac{\partial y^{(i)}}{\partial w_j}) + \sum_i y^{(i)} \frac{1}{\hat{y}^{(i)}} (\frac{\partial y^{(i)}}{\partial w_j}) \end{aligned} \\ 其中:& \frac{\partial y^{(i)}}{\partial w_j} = \sigma'(z^{(i)}) \frac{\partial z^{(i)}}{\partial w_j} = \sigma(z^{(i)}) (1 - \sigma(z^{(i)})) x^{(i)}_j \Rightarrow \\ & \frac{\partial L}{\partial w_j} = \sum_i - (1 - \bcancel{y^{(i)}}) \frac{1}{\cancel{1 - \hat{y}^{(i)}}} \sigma(z^{(i)}) \cancel{(1 - \sigma(z^{(i)}))} x^{(i)}_j + \\ & \sum_i y^{(i)} \frac{1}{\cancel{\hat{y}^{(i)}}} \cancel{\sigma(z^{(i)})} (1 - \bcancel{\sigma(z^{(i)})}) x^{(i)}_j = \sum_i (y^{(i)} - \hat{y}^{(i)}) x^{(i)}_j \Rightarrow \\ 梯度下降:& w_j := w_j - \alpha \frac{\partial L}{\partial w_j} \end{aligned}

朴素贝叶斯

给定NN个样本对{(X(i),y(i)),i=1,,N}\{(X^{(i)}, y^{(i)}), i = 1, \cdots, N\},其中y{Ck,k=1,,K}y \in \{C_k, k = 1, \cdots, K\}

定义模型为条件概率分布:P(YX)由贝叶斯公式:P(YX)=P(XY)P(Y)P(X)称:{后验概率:P(YX)似然函数:P(XY)=j=1nP(XjY)(朴素贝叶斯)先验概率:P(Y)证据因子:P(X)=kP(XY=Ck)P(Y=Ck)y^=maxkP(XY=Ck)P(Y=Ck)=maxkj=1nP(XjY=Ck)P(Y=Ck)\begin{aligned} 定义模型为条件概率分布:& P(Y | X) \\ 由贝叶斯公式:& P(Y | X) = \frac{P(X | Y) P(Y)}{P(X)} \\ 称:& \begin{cases} 后验概率:& P(Y | X) \\ 似然函数:& P(X | Y) = \prod_{j=1}^n P(X_j | Y) (朴素贝叶斯)\\ 先验概率:& P(Y) \\ 证据因子:& P(X) = \sum_k P(X | Y = C_k) P(Y = C_k) \end{cases} \\ \hat{y} & = \max_k P(X | Y = C_k) P(Y = C_k) \\ & = \max_k \prod_{j=1}^n P(X_j | Y = C_k) P(Y = C_k) \end{aligned}

PCA/LDA

PCA

给定包含MM个样本的NN维数据集{XN×1(i),i=1,,M}\{X_{N \times 1}^{(i)}, i = 1, \cdots, M\}构成样本矩阵XN×M=[X(1)X(2)X(M)]X_{N \times M} = \begin{bmatrix}X^{(1)} & X^{(2)} & \cdots X^{(M)}\end{bmatrix},现希望求取主分量βk,k=1,,K\beta_k, k = 1, \cdots, K使得数据投影在各主分量上的散布最大/方差最大

计算步骤

  1. 计算维度间的协方差矩阵ΣN×N=1MX~X~T\Sigma_{N \times N} = \frac{1}{M} \tilde{X} \tilde{X}^T,其中X~(i)=X(i)X,X=1Mi=1MX(i)\tilde{X}^{(i)} = X^{(i)} - \overline{X}, \overline{X} = \frac{1}{M} \sum_{i=1}^{M} X^{(i)}
  2. 求矩阵Σ\Sigma特征值分解,即Σβk=λkβk\Sigma \beta_k = \lambda_k \beta_k
  3. 将特征对(λk,βk)(\lambda_k, \beta_k)按特征值λk\lambda_k降序排序后,选取前KK主分量作为投影轴构成投影矩阵BN×KB_{N \times K}
  4. 投影SK×M=BN×KTXN×MS_{K \times M} = B_{N \times K}^T X_{N \times M}重建X^=BN×KSK×M\hat{X} = B_{N \times K} S_{K \times M}

证明

  1. 11主成分
    优化目标为

    β1=argmaxS122s.t.β122=1\begin{aligned} \beta_1 & = \arg \max ||S_1||_2^2 \\ s.t. & \quad ||\beta_1||_2^2 = 1 \end{aligned}

    那么

    S122=S1TS1S1=XTβ1}S122=β1TXXTCβ1C=XXT=WΛWT}S122=β1TWΛWTβ1α1=i=1Nλiα1iλ1i=1Nα1iβ1Tβ1=α1TWTWα=α1Tα=i=1Nα1i=1(单位约束)}S122λ1为使S122极大化,取{α11=1α1i=0,i=2,3,,Nβ1=Wα1=w1\begin{aligned} \left. \begin{aligned} \left. \begin{aligned} ||S_1||_2^2 & = S_1^T S_1 \\ S_1 & = X^T \beta_1 \end{aligned} \right\} \Rightarrow ||S_1||_2^2 = \beta_1^T \underbrace{X X^T}_C \beta_1 \\ C = X X^T = W \Lambda W^T \end{aligned} \right\} \Rightarrow \\ \left. \begin{aligned} ||S_1||_2^2 = \beta_1^T W \Lambda \underbrace{W^T \beta_1}_{\alpha_1} = \sum_{i=1}^N \lambda_i \alpha_{1i} \leq \lambda_1 \sum_{i=1}^N \alpha_{1i} \\ \beta_1^T \beta_1 = \alpha_1^T W^T W \alpha = \alpha_1^T \alpha = \sum_{i=1}^N \alpha_{1i} = 1(单位约束) \end{aligned} \right\} \Rightarrow \\ ||S_1||_2^2 \leq \lambda_1 \quad 为使||S_1||_2^2极大化,取 \\ \begin{cases} \alpha_{11} = 1\\ \alpha_{1i} = 0, i = 2, 3, \cdots, N \end{cases} \Rightarrow \beta_1 = W \alpha_1 = w_1 \end{aligned}

  2. r(r>1)r(r>1)主成分
    优化目标为

    βr=argmaxSr22s.t.βrTβi=0,i=1,,r1βr22=1\begin{aligned} \beta_r & = \arg \max ||S_r||_2^2 \\ s.t. & \quad \beta_r^T \beta_i = 0, i = 1, \cdots, r - 1 \\ & ||\beta_r||_2^2 = 1 \end{aligned}

    那么

    Sr22=SrTSrSr=XTβr}Sr22=βrTXXTCβrC=XXT=WΛWT}Sr22=βrTWΛWTβrαr=i=1NλiαriβrTβi=(Wαr)T(wi)=αri=0,ir(正交约束)βrTβr=αrTWTWα=αrTα=i=1Nα1i=1(单位约束)}Sr22=λrαrr为使Sr22极大化,取{αrr=1αri=0,i=rβr=Wαr=wr\begin{aligned} \left. \begin{aligned} \left. \begin{aligned} ||S_r||_2^2 = S_r^T S_r \\ S_r = X^T \beta_r \end{aligned} \right\} \Rightarrow ||S_r||_2^2 = \beta_r^T \underbrace{X X^T}_C \beta_r \\ C = X X^T = W \Lambda W^T \end{aligned} \right\} \Rightarrow \\ \left. \begin{aligned} ||S_r||_2^2 = \beta_r^T W \Lambda \underbrace{W^T \beta_r}_{\alpha_r} = \sum_{i=1}^N \lambda_i \alpha_{ri} \\ \beta_r^T \beta_i =(W \alpha_r)^T (w_i) = \alpha_{ri} = 0, i \neq r (正交约束) \\ \beta_r^T \beta_r = \alpha_r^T W^T W \alpha = \alpha_r^T \alpha = \sum_{i=1}^N \alpha_{1i} = 1(单位约束) \end{aligned} \right\} \Rightarrow \\ ||S_r||_2^2 = \lambda_r \alpha_{rr} \quad 为使||S_r||_2^2极大化,取 \\ \begin{cases} \alpha_{rr} = 1 \\ \alpha_{ri} = 0, i = \neq r \end{cases} \Rightarrow \beta_r = W \alpha_r = w_r \end{aligned}

LDA

给定NN个样本对{(X(i),y(i)),i=1,,N}\{(X^{(i)}, y^{(i)}), i = 1, \cdots, N\},其中y{Ck,k=1,,K}y \in \{C_k, k = 1, \cdots, K\},记样本矩阵XN×nX_{N \times n}。现利用类别信息求取投影主轴uu使得投影后类内散步小,类间散步大

定义:

{总样本均值:μ=1Ni=1NX(i)类别样本均值:μk=1Nki=1NkX(i),y(i)=Ck类内离差阵:SW,n×n=kNkN[1Nki(X(i)μk)(X(i)μk)T]类内离差阵:SB,n×n=kNkN[(μkμ)(μkμ)T]\begin{cases} 总样本均值: & \mu = \frac{1}{N} \sum_{i=1}^N X^{(i)} \\ 类别样本均值: & \mu_k = \frac{1}{N_k} \sum_{i=1}^{N_k} X^{(i)}, y^{(i)} = C_k \\ 类内离差阵: & S_{W, n \times n} = \sum_k \frac{N_k}{N} \left[ \frac{1}{N_k} \sum_i (X^{(i)} - \mu_k) (X^{(i)} - \mu_k)^T \right] \\ 类内离差阵: & S_{B, n \times n} = \sum_k \frac{N_k}{N} \left[ (\mu_k - \mu) (\mu_k - \mu)^T \right] \\ \end{cases}

计算步骤

  1. 计算类内/类间离差阵SW/SBS_W/S_B
  2. 计算矩阵SW1SBS_W^{-1}S_B的特征对(λi,ui)(\lambda_i, u_i)
  3. 将特征对按特征值降序排序,选取最大的特征值对应特征向量作为投影主轴,构成投影矩阵Un×mU_{n \times m}
  4. 投影到主轴上,X^N×m=XN×nUn×m\hat{X}_{N \times m} = X_{N \times n} U_{n \times m}

证明

将样本点X(i)投影到第一主轴u1上有X~(i)=u1TX(i)在投影空间有X~(i)=u1TX(i),μ~=u1Tμ,μ~k=u1TμkSW~1×1=kNkN[1Nki(X~(i)μ~k)(X~(i)μ~k)T]SB~1×1=kNkN[(μ~kμ~)(μ~kμ~)T]}{SW~=u1TSWu1SB~=u1TSBu1定义优化目标为:u1=argminSW~SB~=argminu1TSWu1u1TSBu1求取极值:u1u1TSWu1u1TSBu1=(u1TSBu1)(2SWu1)(u1TSWu1)(2SBu1)(u1TSBu1)2=0SBu1=u1TSBu1u1TSWu1λ1SWu1,记λ1=u1TSBu1u1TSWu1\begin{aligned} 将样本点X^{(i)}投影到第一主轴u_1上有 \quad \tilde{X}^{(i)} = u_1^T X^{(i)} \quad 在投影空间有 \\ \left.\begin{aligned} \tilde{X}^{(i)} & = u_1^T X^{(i)}, \tilde{\mu} = u_1^T \mu, \tilde{\mu}_k = u_1^T \mu_k \\ \tilde{S_W}_{1 \times 1} & = \sum_k \frac{N_k}{N} \left[ \frac{1}{N_k} \sum_i (\tilde{X}^{(i)} - \tilde{\mu}_k) (\tilde{X}^{(i)} - \tilde{\mu}_k)^T \right] \\ \tilde{S_B}_{1 \times 1} & = \sum_k \frac{N_k}{N} \left[ (\tilde{\mu}_k - \tilde{\mu}) (\tilde{\mu}_k - \tilde{\mu})^T \right] \end{aligned}\right\} \Rightarrow \begin{cases} \tilde{S_W} = u_1^T S_W u_1 \\ \tilde{S_B} = u_1^T S_B u_1 \end{cases} \\ 定义优化目标为:u_1 = \arg \min \frac{\tilde{S_W}}{\tilde{S_B}} = \arg \min \frac{u_1^T S_W u_1}{u_1^T S_B u_1} \\ 求取极值:\frac{\partial}{\partial u_1} \frac{u_1^T S_W u_1}{u_1^T S_B u_1} = \frac{(u_1^T S_B u_1)(2 S_W u_1) - (u_1^T S_W u_1)(2 S_B u_1)}{(u_1^T S_B u_1)^2} = 0 \Rightarrow \\ S_B u_1 = \underbrace{\frac{u_1^T S_B u_1}{u_1^T S_W u_1}}_{\lambda_1} S_W u_1,记\lambda_1 = \frac{u_1^T S_B u_1}{u_1^T S_W u_1} \end{aligned}

EM/GMM

EM算法

给定包含NN对样本数据{(X(i),y(i)),i=1,,N}\{(X^{(i)}, y^{(i)}), i = 1, \cdots, N\}。设分类模型为概率模型P(Xθ)P(X | \theta),其中θ\theta待估。该模型包含KK隐藏变量状态{wk,k=1,,K}\{w_k, k = 1, \cdots, K\}。那么证明过程总结如下

MLEL(Dθ)=iP(X(i)θ)logL(Dθ)=ilogP(X(i)θ)优化目标:θ(t+1)=argmaxlogL(Dθ)P(X(i)θ)=kP(X(i),wk(i)θ)(引入隐变量wk)P(wk(i)θ(t))P(wk(i)θ(t))=1(引入迭代变量θ(t))}logL(Dθ)=ilogkP(X(i),wk(i)θ)P(wk(i)θ(t))P(wk(i)θ(t)){φ()下凸iwi=1φ(iwixi)iwiφ(xi)(Jensen不等式)}logL(Dθ)=ikP(wk(i)θ(t))logP(X(i),wk(i)θ)P(wk(i)θ(t))=ikP(wk(i)θ(t))logP(X(i),wk(i)θ)Ew[logP(X(i),wk(i)θ)]ikP(wk(i)θ(t))logP(wk(i)θ(t))H[P(wk(i)θ(t))]Q(θθ(t))=Ew[logP(X(i),wk(i)θ)]优化目标:θ(t+1)=argmaxQ(θθ(t))Q(θθ(t))求极值求解θ(t+1)\begin{aligned} MLE \Rightarrow L(D | \theta) = \prod_i P(X^{(i)} | \theta) \Rightarrow \log L(D | \theta) = \sum_i \log P(X^{(i)} | \theta) \\ \Rightarrow 优化目标:\theta^{(t + 1)} = \arg \max \log L(D | \theta) \\ \\ \left. \begin{aligned} P(X^{(i)} | \theta) = \sum_k P(X^{(i)}, w^{(i)}_k | \theta) (引入隐变量w_k) \\ \frac{P(w^{(i)}_k | \theta^{(t)})}{P(w^{(i)}_k | \theta^{(t)})} = 1 (引入迭代变量\theta^{(t)}) \end{aligned} \right\} \Rightarrow \\ \left. \begin{aligned} \log L(D | \theta) = \sum_i \log \sum_k P(X^{(i)}, w^{(i)}_k | \theta) \frac{P(w^{(i)}_k | \theta^{(t)})}{P(w^{(i)}_k | \theta^{(t)})} \\ \begin{cases} \varphi(\cdot)下凸 \\ \sum_i w_i = 1 \end{cases} \Rightarrow \varphi(\sum_i w_i x_i) \leq \sum_i w_i \varphi(x_i) (Jensen不等式) \end{aligned} \right\} \Rightarrow \\ \log L(D | \theta) = \sum_i \sum_k P(w^{(i)}_k | \theta^{(t)}) \log \frac{P(X^{(i)}, w^{(i)}_k | \theta)}{P(w^{(i)}_k | \theta^{(t)})} \\ = \underbrace{ \sum_i \sum_k P(w^{(i)}_k | \theta^{(t)}) \log P(X^{(i)}, w^{(i)}_k | \theta)}_{E_w\left[ \log P(X^{(i)}, w^{(i)}_k | \theta) \right]} \\ \underbrace{- \sum_i \sum_k P(w^{(i)}_k | \theta^{(t)}) \log P(w^{(i)}_k | \theta^{(t)})}_{H\left[ P(w^{(i)}_k | \theta^{(t)}) \right]} \\ 记 \quad Q(\theta | \theta^{(t)}) = E_w\left[ \log P(X^{(i)}, w^{(i)}_k | \theta) \right] \\ \Rightarrow 优化目标:\theta^{(t + 1)} = \arg \max Q(\theta | \theta^{(t)}) \\ 对Q(\theta | \theta^{(t)})求极值求解\theta^{(t + 1)}。 \end{aligned}

GMM模型

高斯混合模型,具有如下概率形式

P(Xμ,Σ)=k=1KπkN(Xμk,Σk)P(X | \mu, \Sigma) = \sum_{k=1}^K \pi_k N(X | \mu_k, \Sigma_k)

其中

{kπk=1N(Xμk,Σk)=1(2π)d/2Σ1/2exp[12(Xμk)TΣk1(Xμk)]\begin{cases} \sum_k \pi_k = 1 \\ N(X | \mu_k, \Sigma_k) = \frac{1}{(2\pi)^{d/2}|\Sigma|^{1/2}} \exp \left[ - \frac{1}{2} (X - \mu_k)^T \Sigma_k^{-1} (X - \mu_k) \right] \end{cases}

EM算法对参数进行估计

Q(θθ(t))=ikP(wk(i)θ(t))logP(x(i)wk(i),θ)P(wk(i)θ)P(x(i),wk(i)θ){P(wk(i)θ(t))=πk(t)N(x(i)μk(t),Σk(t))jπj(t)N(x(i)μj(t),Σj(t))=γk(i)(t)P(x(i)wk(i),θ)=N(x(i)μk,Σk)P(wk(i)θ)=πk}Q(θθ(t))=ikγk(i)(t)logπkN(x(i)μk,Σk)求解Q函数极值{μk(t+1)=iγk(i)(t)x(i)iγk(i)(t)Σk(t+1)=iγk(i)(t)(x(i)μk)(x(i)μk)Tiγk(i)(t)πk(t+1)=iγk(i)(t)N\begin{aligned} \left. \begin{aligned} Q(\theta|\theta^{(t)}) = \sum_i \sum_k P(w_k^{(i)}|\theta^{(t)}) \log \underbrace{P(x^{(i)} | w_k^{(i)}, \theta) P(w_k^{(i)} | \theta)}_{P(x^{(i)}, w_k^{(i)} | \theta)} \\ \begin{cases} P(w_k^{(i)}|\theta^{(t)}) = \frac{\pi_k^{(t)} N(x^{(i)}|\mu_k^{(t)}, \Sigma_k^{(t)})} {\sum_j \pi_j^{(t)} N(x^{(i)}|\mu_j^{(t)}, \Sigma_j^{(t)})} = \gamma^{(i)(t)}_k \\ P(x^{(i)} | w_k^{(i)}, \theta) = N(x^{(i)}|\mu_k, \Sigma_k) \\ P(w_k^{(i)} | \theta) = \pi_k \end{cases} \end{aligned} \right\} \Rightarrow \\ Q(\theta|\theta^{(t)}) = \sum_i \sum_k \gamma^{(i)(t)}_k \log \pi_k N(x^{(i)}|\mu_k, \Sigma_k) \\ 求解Q函数极值 \Rightarrow \begin{cases} \mu_k^{(t+1)} = \frac{\sum_i \gamma^{(i)(t)}_k x^{(i)}}{\sum_i \gamma^{(i)(t)}_k} \\ \Sigma_k^{(t+1)} = \frac{\sum_i \gamma^{(i)(t)}_k (x^{(i)} - \mu_k) (x^{(i)} - \mu_k)^T}{\sum_i \gamma^{(i)(t)}_k} \\ \pi_k^{(t+1)} = \frac{\sum_i \gamma^{(i)(t)}_k}{N} \end{cases} \end{aligned}

SVM

KKT条件

w=argminf(w)s.t.hj(w)=0,j=1,,mgj(w)0,j=1,,p}L(w,λ,μ)=f(w)+jλjhj(w)+jμj(gj(w)+ϵ2){wf(w)+jλjwhj(w)+jμjwgj(w)=0hj(w)=0,j=1,,mμjgj(w)=0μj0}j=1,,p\begin{aligned} \left.\begin{aligned} w = \arg \min f(w) \\ s.t. \quad h_j(w) = 0, j = 1, \cdots, m \\ g_j(w) \leq 0, j = 1, \cdots, p \end{aligned}\right\} \Rightarrow \\ L(w, \lambda, \mu) = f(w) + \sum_j \lambda_j h_j(w) + \sum_j \mu_j \left(g_j(w) + \epsilon^2 \right) \\ \Rightarrow \begin{cases} \frac{\partial}{\partial w} f(w) + \sum_j \lambda_j \frac{\partial}{\partial w} h_j(w) + \sum_j \mu_j \frac{\partial}{\partial w} g_j(w) = 0 \\ h_j(w) = 0, j = 1, \cdots, m \\ \left.\begin{aligned} \mu_j g_j(w) = 0 \\ \mu_j \geq 0 \end{aligned} \right\} j = 1, \cdots, p \end{cases} \end{aligned}

核技巧

设某函数Φ(x)\Phi(x),可将xxnn维空间映射到nn'维空间,定义两个向量的核函数为κ(xi,xj)=Φ(xi)TΦ(xj)\kappa(x_i, x_j) = \Phi(x_i)^T \Phi(x_j),常用和函数有

{线性核:κ(xi,xj)=xiTxj多项式核:κ(xi,xj)=(γxiTxj+c)nsigmoid核:κ(xi,xj)=tanh(γxiTxj+c)拉普拉斯核:κ(xi,xj)=exp(γxixjσ)高斯核:κ(xi,xj)=exp(γxixj22σ2)\begin{cases} 线性核:& \kappa(x_i, x_j) = x_i^T x_j \\ 多项式核:& \kappa(x_i, x_j) = (\gamma x_i^T x_j + c)^n \\ sigmoid核:& \kappa(x_i, x_j) = \tanh (\gamma x_i^T x_j + c) \\ 拉普拉斯核:& \kappa(x_i, x_j) = \exp (- \gamma \frac{||x_i - x_j||}{\sigma}) \\ 高斯核:& \kappa(x_i, x_j) = \exp (- \gamma \frac{||x_i - x_j||^2}{2 \sigma^2}) \end{cases}

分类问题

给定NN对样本{(X(i),y(i)),i=1,,N},y{1,1}\{(X^{(i)}, y^{(i)}), i = 1, \cdots, N\}, y \in \{-1, 1\},求取超平面wTΦ(x)+b=0w^T \Phi(x) + b = 0使样本点落在该超平面两侧。

线性可分

r+/为分类平面到支持向量x+/的距离,则r=r++r,且r+/=wTΦ(x+/)+bw=1w/负样本分别满足{wTΦ(x(i))+b>1y(i)>0wTΦ(x(i))+b<1y(i)<0y(i)[wTΦ(x(i))+b]1(包括支持向量)}\begin{aligned} \left.\begin{aligned} 记r_{+/-}为分类平面到支持向量x_{+/-}的距离,则r = r_+ + r_-,且r_{+/-} = \frac{|w^T \Phi(x_{+/-}) + b|}{||w||} = \frac{1}{||w||} \\ 正/负样本分别满足\begin{cases} w^T \Phi(x^{(i)}) + b > 1 & y^{(i)} > 0 \\ w^T \Phi(x^{(i)}) + b < -1 & y^{(i)} < 0 \end{cases} \Rightarrow y^{(i)} [w^T \Phi(x^{(i)}) + b] \geq 1(包括支持向量) \end{aligned}\right\} \Rightarrow \\ \end{aligned}

优化目标:w,b=argmaxrs.t.y(i)[wTΦ(x(i))+b]1即:w,b=argmin12w2s.t.y(i)[wTΦ(x(i))+b]1\begin{aligned} 优化目标:& \begin{aligned} w, b & = \arg \max r \\ s.t. & \quad y^{(i)} [w^T \Phi(x^{(i)}) + b] \geq 1 \end{aligned} \\ 即: & \begin{aligned} w, b & = \arg \min \frac{1}{2} ||w||^2 \\ s.t. & \quad y^{(i)} [w^T \Phi(x^{(i)}) + b] \geq 1 \end{aligned} \end{aligned}

线性不可分

在线性可分支持向量机基础上,对每个样本添加松弛变量ϵ(i)\epsilon^{(i)}

优化目标:w,b=argmin[12w2+Ciϵ(i)]s.t.y(i)[wTΦ(x(i))+b]1ϵ(i)ϵ(i)0\begin{aligned} 优化目标:\begin{aligned} w, b & = \arg \min \left[ \frac{1}{2} ||w||^2 + C \sum_i \epsilon^{(i)} \right] \\ s.t. & \quad y^{(i)} [w^T \Phi(x^{(i)}) + b] \geq 1 - \epsilon^{(i)} \\ & \epsilon^{(i)} \geq 0 \end{aligned} \end{aligned}

回归问题

给定NN对样本{(X(i),y(i)),i=1,,N},yR\{(X^{(i)}, y^{(i)}), i = 1, \cdots, N\}, y \in R,求回归模型y^=wTΦ(x)+b\hat{y} = w^T \Phi(x) + b,使得每个样本尽量拟合到该模型上,定义损失为

L(i)={y(i)wTΦ(x(i))bϵy(i)wTΦ(x(i))b>ϵ0otherwiseL^{(i)} = \begin{cases} |y^{(i)} - w^T \Phi(x^{(i)}) - b| - \epsilon & |y^{(i)} - w^T \Phi(x^{(i)}) - b| > \epsilon \\ 0 & otherwise \end{cases}

求解优化问题

以线性可分支持向量机为例,讲解参数wbw, b的优化方法

优化目标:w,b=argmin12w2s.t.y(i)[wTΦ(x(i))+b]1优化目标:\begin{aligned} w, b & = \arg \min \frac{1}{2} ||w||^2 \\ s.t. & \quad y^{(i)} [w^T \Phi(x^{(i)}) + b] \geq 1 \end{aligned}

拉格朗日函数:L(w,b,μ)=12w2+iμ(i){1y(i)[wTΦ(x(i))+b]}w,b,μ=argminw,bmaxμL(w,b,μ)w,b,μ=argmaxμminw,bL(w,b,μ)(对偶问题)求解极值:{wjL(w,b,μ)=12wjw2+iμ(i){y(i)wjwTΦ(x(i))}=wjiμ(i)y(i)Φ(x(i))jbL(w,b,μ)=iμ(i){y(i)bb}=iμ(i)y(i)K.K.T条件:{iμ(i)y(i)Φ(x(i))j=wjiμ(i)y(i)=0}(极值条件)1y(i)[wTΦ(x(i))+b]0(不等式约束)μ(i){1y(i)[wTΦ(x(i))+b]}=0μ(i)>0}(优化目标=的必要条件)\begin{aligned} 拉格朗日函数:L(w, b, \mu) = \frac{1}{2} ||w||^2 + \sum_i \mu^{(i)} \left\{ 1 - y^{(i)} [w^T \Phi(x^{(i)}) + b] \right\} \\ w, b, \mu = \arg \min_{w, b} \max_{\mu} L(w, b, \mu) \Rightarrow w, b, \mu = \arg \max_{\mu} \min_{w, b} L(w, b, \mu)(对偶问题) \\ 求解极值:\begin{cases} \begin{aligned} \frac{\partial}{\partial w_j} L(w, b, \mu) = \frac{1}{2} \frac{\partial}{\partial w_j} ||w||^2 + \sum_i \mu^{(i)} \left\{ - y^{(i)} \frac{\partial}{\partial w_j} w^T \Phi(x^{(i)}) \right\} = \\ w_j - \sum_i \mu^{(i)} y^{(i)} \Phi(x^{(i)})_j \end{aligned} \\ \begin{aligned} \frac{\partial}{\partial b} L(w, b, \mu) = \sum_i \mu^{(i)} \left\{ -y^{(i)} \frac{\partial}{\partial b} b \right\} = \\ - \sum_i \mu^{(i)} y^{(i)} \end{aligned} \end{cases} \\ 由K.K.T条件:\begin{cases} \left.\begin{aligned} \sum_i \mu^{(i)} y^{(i)} \Phi(x^{(i)})_j & = w_j \\ \sum_i \mu^{(i)} y^{(i)} & = 0 \end{aligned}\right\} (极值条件) \\ 1 - y^{(i)} [w^T \Phi(x^{(i)}) + b] \leq 0 (不等式约束) \\ \left.\begin{aligned} \mu^{(i)} \left\{ 1 - y^{(i)} [w^T \Phi(x^{(i)}) + b] \right\} = 0 \\ \mu^{(i)} > 0 \end{aligned} \right\} (优化目标取'='的必要条件) \end{cases} \end{aligned}

拉格朗日函数展开后,将极值条件代入,有拉格朗日函数展开后,将极值条件代入,有

L(w,b,μ)=12w2+iμ(i){1y(i)[wTΦ(x(i))+b]}=12wTw+iμ(i)iμ(i)y(i)wTΦ(x(i))iμ(i)y(i)b=12wTw+iμ(i)iμ(i)y(i)(jwjΦ(x(i))j)wTΦ(x(i))iμ(i)y(i)b=12wTw+iμ(i)jwjiμ(i)y(i)Φ(x(i))jwi=12wTw+iμ(i)wTw=(iμ(i)y(i)Φ(x(i)))T(iμ(i)y(i)Φ(x(i)))=ijμ(i)μ(j)y(i)y(j)Φ(x(i))TΦ(x(j))}L(μ)=12ijμ(i)μ(j)y(i)y(j)Φ(x(i))TΦ(x(j))wTw+iμ(i)\begin{aligned} L(w, b, \mu) & = \frac{1}{2} ||w||^2 + \sum_i \mu^{(i)} \left\{ 1 - y^{(i)} [w^T \Phi(x^{(i)}) + b] \right\} \\ & = \frac{1}{2} w^T w + \sum_i \mu^{(i)} - \sum_i \mu^{(i)} y^{(i)} w^T \Phi(x^{(i)}) - \sum_i \mu^{(i)} y^{(i)} b \\ & = \frac{1}{2} w^T w + \sum_i \mu^{(i)} - \sum_i \mu^{(i)} y^{(i)} \underbrace{\left( \sum_j w_j \Phi(x^{(i)})_j \right)}_{w^T \Phi(x^{(i)})} - \cancel{\sum_i \mu^{(i)} y^{(i)} b} \\ & \left.\begin{aligned} = \frac{1}{2} w^T w + \sum_i \mu^{(i)} - \sum_j w_j \cdot \underbrace{\sum_i \mu^{(i)} y^{(i)} \Phi(x^{(i)})_j}_{w_i} = - \frac{1}{2} w^T w + \sum_i \mu^{(i)} \\ w^T w = \left( \sum_i \mu^{(i)} y^{(i)} \Phi(x^{(i)}) \right)^T \left( \sum_i \mu^{(i)} y^{(i)} \Phi(x^{(i)}) \right) = \\ \sum_i \sum_j \mu^{(i)} \mu^{(j)} y^{(i)} y^{(j)} \Phi(x^{(i)})^T \Phi(x^{(j)}) \end{aligned}\right\} \Rightarrow \\ L(\mu) & = - \frac{1}{2} \underbrace{\sum_i \sum_j \mu^{(i)} \mu^{(j)} y^{(i)} y^{(j)} \Phi(x^{(i)})^T \Phi(x^{(j)})}_{w^T w} + \sum_i \mu^{(i)} \end{aligned}

那么现在的优化问题如下,用SMO进行求解那么现在的优化问题如下,用SMO进行求解

μ=argmaxμL(μ)s.t.μ(i)0,iμ(i)y(i)=0μw,b\begin{aligned} \mu & = \arg \max_{\mu} L(\mu) \\ s.t. & \quad \mu^{(i)} \geq 0, \quad \sum_i \mu^{(i)} y^{(i)} = 0 \\ \Rightarrow & \mu^* \Rightarrow w^*, b^* \end{aligned}

聚类

仅介绍部分概念和算法步骤。给定样本集合{X(i),i=1,,N}\{X^{(i)}, i = 1, \cdots, N\},指定划分类别KK,要求利用样本分布,将样本划分为KK个类别。

距离度量

定义两个nn维向量x,yx, y,有如下常用距离定义

曼哈顿距离d=xy1=jxjyj欧氏距离d=xy2=(j(xjyj)2)1/2闵可夫斯基距离d=xyp=(jxjyjp)1/p余弦距离d=xy1=cos<x,y>=xTyxy\begin{aligned} 曼哈顿距离 & d = || x - y ||_1 = \sum_j |x_j - y_j| \\ 欧氏距离 & d = || x - y ||_2 = (\sum_j (x_j - y_j)^2)^{1 / 2} \\ 闵可夫斯基距离 & d = || x - y ||_p = (\sum_j |x_j - y_j|^p)^{1 / p} \\ 余弦距离 & d = || x - y ||_1 = \cos <x, y> = \frac{x^T y}{||x||\cdot||y||} \\ \end{aligned}

KMeans

  1. 随机选取KK个样本点作为初始中心点(初值敏感);
  2. 计算每个样本点到各中心点的距离(N×KN \times K);
  3. 将每个样本划分到距离最近的中心点指代的类别中;
  4. 每个类别重新计算中心点,更新参数;
  5. 重复2~4直至收敛。

Spectral

  1. 构建相似矩阵{SN×N=[dij]dij=x(i)x(j)22\begin{cases} S_{N \times N} = \begin{bmatrix} d_{ij} \end{bmatrix} \\ d_{ij} = ||x^{(i)} - x^{(j)}||_2^2 \end{cases}
  2. 计算邻接矩阵

    {ϵ近邻法:wij={ϵdijϵ0otherwiseK近邻法:wij={exp(dij2σ2)x(i)δK(x(j))AND/ORx(j)δK(x(i))0otherwiseδK(x)表示xK邻域全连接法:wij=exp(dij2σ2)\begin{cases} \epsilon近邻法:& w_{ij} = \begin{cases} \epsilon & d_{ij} \leq \epsilon \\ 0 & otherwise \end{cases} \\ K近邻法:& w_{ij} = \begin{cases} \exp(-\frac{d_{ij}}{2 \sigma^2}) & x^{(i)} \in \delta_K(x^{(j)}) \quad AND/OR \quad x^{(j)} \in \delta_K(x^{(i)}) \\ 0 & otherwise \end{cases} \\ & \delta_K(x)表示x的K邻域 \\ 全连接法:& w_{ij} = \exp(-\frac{d_{ij}}{2 \sigma^2}) \end{cases}

  3. 求度矩阵DN×N=diag{jwij,i=1,,N}D_{N \times N} = \text{diag}\{\sum_j w_{ij}, i = 1, \cdots, N\},即WW行和作为对角元素;
  4. 求(正则)拉普拉斯矩阵L=DWL = D - WL=D1(DW)L = D^{-1}(D - W)L=D1/2(DW)D1/2L = D^{-1/2}(D - W)D^{-1/2}
  5. LL的特征分解,选取N(NN)N'(N' \leq N)最小特征值对应的特征向量组成矩阵FN×NF_{N \times N'}
  6. 将矩阵FF每行视作样本f(i)f^{(i)},标准化后执行其他简单的聚类如KMeans,得到聚类结果。

决策树

给定包含D|D|个样本的样本集D={(X(i),y(i)),i=1,,D}D = \{(X^{(i)}, y^{(i)}), i = 1, \cdots, |D|\},属于KK个类别y{Ck,k=1,,K}y \in \{C_k, k = 1, \cdots, K\},设类别CkC_k的样本数目为Dk|D_{k}|,设特征AAA|A|个特征{Aa,a=1,,A}\{A_a, a = 1, \cdots, |A|\},每个特征包含样本数目Da|D_{a}|,记特征为AaA_a的样本中属于类别CkC_k的样本数目为Dak|D_{ak}|

ID3

信息增益作为准则选择当前最优划分属性:信息增益越大表示属性越优

g(D,A)=H(D)H(DA)H(D)=kDkDlogDkD(总样本的类别熵)H(DA)=aDaD(kDakDalogDakDa)H(Da)(特征Aa的类别熵的加权和)}\begin{aligned} g(D, A) = H(D) - H(D | A) \\ \left.\begin{aligned} H(D) & = - \sum_k \frac{|D_k|}{|D|} \log \frac{|D_k|}{|D|}(总样本的类别熵) \\ H(D | A) & = \sum_a \frac{|D_a|}{|D|} \underbrace{\left( - \sum_k \frac{|D_{ak}|}{|D_a|} \log \frac{|D_{ak}|}{|D_a|} \right)}_{H(D_a)} (特征A_a的类别熵的加权和) \end{aligned} \right\} \end{aligned}

C4.5

信息增益比作为准则选择当前最优划分属性:信息增益比越大表示属性越优

  • 以信息增益比(information gain ratio)作为特征选择的准则,克服ID3会优先选择有较多属性值的特征的缺点;
  • 弥补不能处理特征属性值连续的问题。

gR(D,A)=g(D,A)HA(D)HA(D)=aDaDlogDaD(特征A的属性熵)\begin{aligned} g_R(D, A) & = \frac{g(D, A)}{H_A(D)} \\ H_A(D) & = - \sum_a \frac{|D_a|}{|D|} \log \frac{|D_a|}{|D|} (特征A的属性熵) \end{aligned}

CART

信息增益比作为准则选择当前最优划分属性:信息增益比越大表示属性越优

gG(D,A)=Gini(D)Gini(DA)Gini(D)=1k(DkD)2(总样本的类别基尼系数)Gini(DA)=aDaD(1k(DakDa)2)Gini(Da)(特征Aa的类别基尼系数的加权和)}\begin{aligned} g_G(D, A) = \text{Gini}(D) - \text{Gini}(D|A) \\ \left.\begin{aligned} \text{Gini}(D) & = 1 - \sum_k (\frac{|D_k|}{|D|})^2 (总样本的类别基尼系数) \\ \text{Gini}(D|A) & = \sum_a \frac{|D_a|}{|D|} \underbrace{\left( 1 - \sum_k (\frac{|D_{ak}|}{|D_a|})^2 \right)}_{\text{Gini}(D_a)} (特征A_a的类别基尼系数的加权和) \end{aligned}\right\} \end{aligned}

RF

随机森林是用Bagging策略,对包含NN个样本的数据集进行MM次的有放回的采样,每次随机取NmN_m个样本,得到MM个样本数目为NmN_m的样本子集,对每个子集建立分类器。

Bootstrap采样:对于一个样本,它在某一次含mm个样本的训练集的随机采样中,每次被采集到的概率是1/m1/m。不被采集到的概率为11/m1−1/m。如果mm次采样都没有被采集中的概率是(11/m)m(1−1/m)^m。当mm→\infty时,limm(11/m)m0.368\lim_{m \rightarrow \infty} (1−1/m)^m \approx 0.368。也就是说,在bagging的每轮随机采样中,训练集中大约有36.8%的数据没有被采样集采集中。对于这部分大约36.8%36.8\%的没有被采样到的数据,我们常常称之为袋外数据(Out Of Bag, 简称OOB)。这些数据没有参与训练集模型的拟合,因此可以用来检测模型的泛化能力。

随机森林在Bagging策略上进行训练:

  1. 用Bootstrap策略随机采样MM次;
  2. 一棵树的生成时,仅从所有特征(KK个)中选取kk个特征
  3. 生成MM棵树进行投票表决,确定预测结果(分类可取众数、回归可取均值)。