阅读说明: 保留原笔记的编号与章节顺序;代码只供查看,没有在本站运行。历史输出不代表当前版本的复现结果。截图/外链图以独立整理的公式、表格、示意或文字替换;纠错与必要补充均作说明。
0. Feature Selection
- Better understanding of your model
- Improve model stability(reduce model variance). Multicollinearity()
1. Pearson Correlation
- means covariance(协方差) between , means standard deviation
- Covariance: where
相关矩阵: 第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: - L1:
Definition:
L1 tends to provide sparse solutions
缺点:计算结果不稳定PLAINTEXT · 仅供阅读 0X1, 5X2, 2X3, 3X4 => Y 5X1, 0X2, 2X3, 3X4 => Y -2X1, 7X2, 2X3, 3X4 => Y - L2:
Definition:
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方法?
- pearson corr
- L1 (无L2)
- Ensemble learning中的feature importance
- PCA
问题2:在系统中有high corr的features,怎么提高模型精度?
- pearson corr
- L1
- Ensemble learning
- PCA
- L2
问题3:在系统中有high corr的features,Model testing的error很高,怎么办?
- 扔feature
- L2或random forest, reduce var的方法
1和2选谁? 取决于是否需要删除feature
4. Fundamentals of Linear Algebra
4.1 Matrix
- Matrix can be considered as a linear transformation:
线性变换独立例: 把向量(1,1)变成(2,1),伸长水平方向而保持线性组合关系。
4.2 Eigenvalue and Eigenvector
is the eigenvalue of eigenvector v.
特征方向: 对上述A,满足,满足;特征向量必须非零。负特征值会翻转方向,不能一概称为方向不变。
4.2 Eigendecomposition
特征分解,就是将矩阵分解为由其特征值和特征向量表示的矩阵之积的方法
- Suppose A is a square matrix with N linearly independent eigenvectors. Then we can factorize A as
其中Q是方阵,它是方阵A的特征向量组成的矩阵
特征分解: 对称矩阵可选正交特征向量,;一般可对角化矩阵为。例如,单位化特征方向为、,特征值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的投影方差为。约束,最大化方差的驻点满足,选择最大特征值对应方向。多维取正交前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,再算得到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
where m is the number of observations. (number of records in your dataset)
If we can normalize our data first, the mean of each feature will become 0. (归一化处理:->)
Variance:
->
Covariance:
->
Then the matrix above will become:
-
Covariance matrix(A就是C)
-
Eigendecomposition for Covariance Matrix
- Convert Covariance matrix to Diagonal matrix
- Convert Covariance matrix to Diagonal matrix
-
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,即
假设我们要取5个features,则特征矩阵是一个5*5(即特征值top5组成的)
Q是特征向量的矩阵,维数=10*5是top 5的特征向量(注意区分:不是全部的特征向量)
矩阵维数:
至此,我们得到了投影矩阵(projection matrix) Q - Step 4:用投影矩阵Q对Data做降维
矩阵维数:
整理注: 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!