统计与机器学习 / 22

线性代数与主成分分析

从假设、目标到模型和评估。 线性代数与主成分分析。

阅读说明: 保留原笔记的编号与章节顺序;代码只供查看,没有在本站运行。历史输出不代表当前版本的复现结果。截图/外链图以独立整理的公式、表格、示意或文字替换;纠错与必要补充均作说明。

0. Feature Selection

  • Better understanding of your model
  • Improve model stability(reduce model variance). Multicollinearity()

1. Pearson Correlation

QX1,X2=cov(X1,X2)σX1σX2∈[−1,1]Q_{X_1,X_2}=\frac{cov(X_1,X_2)}{\sigma_{X_1}\sigma_{X_2}}\in{[-1, 1]}
  • cov(X1,X2)cov(X_1,X_2) means covariance(协方差) between X1,X2X_1,X_2, σX\sigma_{X} means standard deviation
  • Covariance:cov(X1,X2)=E[(X1−E(X1))(X2−E(X2))]=E(X1X2)−E(X1)E(X2)cov(X_1,X_2)=E[(X_1-E(X_1))(X_2-E(X_2))]=E(X_1X_2)-E(X_1)E(X_2) where σX12=E(X12)−(E(X1))2\sigma^{2}_{X_1}=E(X^2_1)-(E(X_1))^2

相关矩阵: 第i行j列为特征i、j的Pearson相关系数,矩阵对称、非恒定特征对角线为1。高绝对相关提示线性共变,不证明因果,也不排除零相关下的非线性依赖。

2. Regularized Models

Use regularization methods to do feature selection

  • Regularization review
    A method for adding constraints or penalty term to a model, with the goal of preventing overfitting and improving generalization.
    Loss function: ∑i=1n(yi−hθ(xi))2+λ∣∣θ∣∣\sum^{n}_{i=1}(y_i-h_\theta(x_i))^2+\lambda||\theta||
  • L1:
    Definition: λ∑i=1n∣θi∣\lambda\sum^{n}_{i=1}|\theta_{i}|
    L1 tends to provide sparse solutions
    缺点:计算结果不稳定
    PLAINTEXT · 仅供阅读
     0X1, 5X2, 2X3, 3X4 => Y
     5X1, 0X2, 2X3, 3X4 => Y
    -2X1, 7X2, 2X3, 3X4 => Y
  • L2:
    Definition: λ∑i=1nθi2\lambda\sum^{n}_{i=1}\theta_{i}^{2}
    L2 tends to spread weights more equally among correlated features.
    计算结果更稳定
    PLAINTEXT · 仅供阅读
     2.4X1, 2.6X2, 2X3, 3X4 => Y

3. Other Feature Selection Methods

可以利用某些non-linear model自带的feature importance功能, e.g. random forest - feature importance


问题1:Features selection方法?

  1. pearson corr
  2. L1 (无L2)
  3. Ensemble learning中的feature importance
  4. PCA

问题2:在系统中有high corr的features,怎么提高模型精度?

  1. pearson corr
  2. L1
  3. Ensemble learning
  4. PCA
  5. L2

问题3:在系统中有high corr的features,Model testing的error很高,怎么办?

  1. 扔feature
  2. L2或random forest, reduce var的方法

1和2选谁? 取决于是否需要删除feature


4. Fundamentals of Linear Algebra

4.1 Matrix

  • Matrix can be considered as a linear transformation:

线性变换独立例: A=diag⁡(2,1)A=\operatorname{diag}(2,1)把向量(1,1)变成(2,1),伸长水平方向而保持线性组合关系。

4.2 Eigenvalue and Eigenvector

Av=λvAv=\lambda{v}
λ\lambda is the eigenvalue of eigenvector v.

特征方向: 对上述A,e1=(1,0)e_1=(1,0)满足Ae1=2e1Ae_1=2e_1,e2=(0,1)e_2=(0,1)满足Ae2=e2Ae_2=e_2;特征向量必须非零。负特征值会翻转方向,不能一概称为方向不变。

4.2 Eigendecomposition

特征分解,就是将矩阵分解为由其特征值和特征向量表示的矩阵之积的方法

AQ=QΛAQ=Q\Lambda
  • Suppose A is a square matrix with N linearly independent eigenvectors. Then we can factorize A as
A=QΛQ−1A=Q\Lambda Q^{-1}

其中Q是N∗NN*N方阵,它是方阵A的特征向量组成的矩阵

特征分解: 对称矩阵可选正交特征向量,A=QΛQTA=Q\Lambda Q^T;一般可对角化矩阵为QΛQ−1Q\Lambda Q^{-1}。例如A=[2112]A=\begin{bmatrix}2&1\\1&2\end{bmatrix},单位化特征方向为(1,1)/2(1,1)/\sqrt2、(1,−1)/2(1,-1)/\sqrt2,特征值3、1。

  • (Advanced) We only do eigendecomposition on diagonalizable (对角化) matrices. More general form: singular value decomposition (SVD).

5. Principal Component Analysis (PCA)

5.1 Why PCA?

  • Dimensionality reduction(降维)
    • remove feature correlation
    • “Feature selection” - Reduce model overfitting

5.2 Objective of PCA

Given original data in d-dimensional space, we want to find a low-dimensional projection (k-dimensional space, where k<d) such that it captures the most of the ‘variability’ in the original data and minimizes the reconstruction error.

(Advanced) PCA数学证明

PCA优化独立推导: 对已中心化、每行为样本的矩阵X,单位方向v的投影方差为vTXTXv/mv^TX^TXv/m。约束vTv=1v^Tv=1,最大化方差的驻点满足Cv=λvCv=\lambda v,选择最大特征值对应方向。多维取正交前k个方向;正交投影的重构误差为被舍弃方向的平方投影之和。

5.3 Intuition of PCA

  • What do we want to do?
    Select only one or a few ‘important’ ‘feature(s)’ from the original feature space

矩阵尺寸与选轴: 1000×10的中心化数据D形成10×10协方差矩阵C。按特征值降序取前5个特征向量得到10×5的Q,再算Z=DQZ=DQ得到1000×5。两个高度相关的原变量可投影成主轴与次轴;特征值越大代表该轴解释的方差越多。

  • Problems we need to solve:
    Find a new projection to a new set of features where the information loss is minimized (most information is kept)

Assume we only have two features a and b for simplicity, then
X=(a1a2⋯amb1b2⋯bm)X=\begin{pmatrix} a_1 & a_2 & \cdots & a_m \\ b_1 & b_2 & \cdots & b_m \\ \end{pmatrix}
where m is the number of observations. (number of records in your dataset)
1mXXT=(1m∑i=1mai21m∑i=1maibi1m∑i=1maibi1m∑i=1mbi2)\frac{1}{m}XX^T = \begin{pmatrix} \frac{1}{m}\sum^{m}_{i=1}a_i^2 & \frac{1}{m}\sum^{m}_{i=1}a_ib_i \\ \frac{1}{m}\sum^{m}_{i=1}a_ib_i & \frac{1}{m}\sum^{m}_{i=1}b_i^2 \\ \end{pmatrix}
If we can normalize our data first, the mean μ\mu of each feature will become 0. (归一化处理:aia_i->ai−μaσa\frac{a_i-\mu_a}{\sigma_a})
Variance:
Var(a)=1m∑i=1m(ai−μ)2Var(a) = \frac{1}{m}\sum^{m}_{i=1}(a_i-\mu)^2
->
Var(a)=1m∑i=1mai2Var(a) = \frac{1}{m}\sum^{m}_{i=1}a_i^2
Covariance:
Cov(a,b)=1m−1∑i=1m(ai−μa)(bi−μb)Cov(a,b)=\frac{1}{m-1}\sum^m_{i=1}(a_i-\mu_a)(b_i-\mu_b)
->
Cov(a,b)=1m∑i=1maibiCov(a,b)=\frac{1}{m}\sum^m_{i=1}a_ib_i
Then the matrix above will become:
(Cov(a,a)Cov(a,b)Cov(b,a)Cov(b,b))\begin{pmatrix} Cov(a,a) & Cov(a,b) \\ Cov(b,a) & Cov(b,b) \\ \end{pmatrix}


  • Covariance matrix(A就是C)
    A=[E[(X−μ)2]E[(X−μ)(Y−ν)]E[(X−μ)(Y−ν)]E[(Y−ν)2]]A=\begin{bmatrix} E[(X-\mu)^2] & E[(X-\mu)(Y-\nu)] \\ E[(X-\mu)(Y-\nu)] & E[(Y-\nu)^2] \\ \end{bmatrix}

  • Eigendecomposition for Covariance Matrix

    • Convert Covariance matrix to Diagonal matrix
      AQ=QΛAQ=Q\Lambda
      A=QΛQ−1A=Q\Lambda Q^{-1}
  • Select Dimensions

  • Example
    Suppose the Data is D = 1000行(sample)* 10列(feature)

    • Step 1:对data D做feature normalization, i.e. 数据归一化处理
    • Step 2:计算协方差矩阵:A=10*10
    • Step 3:对协方差矩阵做Eigendecomposition,即AQ=QΛAQ=Q\Lambda
      假设我们要取5个features,则特征矩阵Λ\Lambda是一个5*5(即特征值top5组成的)
      Q是特征向量的矩阵,维数=10*5是top 5的特征向量(注意区分:不是全部的特征向量)
      A∗Q=Q∗ΛA*Q=Q*\Lambda
      矩阵维数:(10∗10)∗(10∗5)=(10∗5)∗(5∗5)(10*10)*(10*5)=(10*5)*(5*5)
      至此,我们得到了投影矩阵(projection matrix) Q
    • Step 4:用投影矩阵Q对Data做降维
      D∗Q=new DD*Q=\text{new D}
      矩阵维数:(1000∗10)∗(10∗5)=(1000∗5)(1000*10)*(10*5)=(1000*5)

整理注: PCA首先中心化;是否按标准差缩放取决于特征单位与目标。协方差分母要统一为m或m-1,这只会整体缩放特征值,不改变主方向。PCA选取新轴,不是直接选择原始列。

5.4 Details of PCA

  • Step 1:对样本矩阵做feature normalization, i.e. 数据归一化处理
  • Step 2:计算样本矩阵的协方差矩阵(covariance matrix)
  • Step 3:对协方差矩阵做特征值分解(Eigendecomposition),选取最大的前d个特征值所对应的特征向量,构成投影矩阵(projection matrix)
  • Step 4:利用投影矩阵对样本矩阵进行projection变换,得到降维后的新矩阵

5.5 Important notes of PCA

  • We should perform normalization to the original features prior to doing PCA
  • We can also use PCA to do feature selection

注意,PCA后feature变换了,feature的数量也变化了。所以这里的feature selection=feature变换后再做selection!

整理参考资料