阅读说明: 保留原笔记的编号与章节顺序;代码只供查看,没有在本站运行。历史输出不代表当前版本的复现结果。截图/外链图以独立整理的公式、表格、示意或文字替换;纠错与必要补充均作说明。
1. Decision Tree
1.1 Types
分类树与回归树: 同样沿条件分支进入叶节点;分类叶输出类别概率/类别,回归叶输出数值(平方误差目标下通常取叶样本均值)。
- Left: Classification Tree
- Right: Regression Tree
1.2 Terminologies
树术语: 根节点从全部训练样本开始;内部节点按特征条件分裂;边表示条件分支;叶节点给出预测;某节点及其后代组成子树;剪枝删除部分分支以控制复杂度。
- Root Node
- Splitting
- Decision Node
- Leaf/Terminal Node
- Pruning(剪枝)
- Branch/Sub-Tree
- Parent and Child Node
1.3 Build a Decision Tree
Two steps:
- Feature Prioritization
- Feature Splitting
随着树深度的增加,节点的熵可以迅速地降低。熵降低的速度越来越快,希望得到一个最矮的高度树。
- 熵(entropy)
表示随机变量不确定性的度量,熵越大代表随机变量的不确定性就越大。 - 条件熵(conditional entropy)
经验条件熵就是在某一条件约束下的经验熵。 - 信息增益(information gain)
信息增益表示得知特征X的信息而使得类Y的信息的不确定性减少的程度。
不同的特征往往具有不同的信息增益,信息增益大的特征具有更强的分类能力。
Algorithm Details - ID3 Algorithm
- At each iteration step, we split the feature which can give us the largest entropy gain.
- Definition of Entropy(熵):
Entropy of the entire information system:
Example:
熵与分裂: 原类别比例0.7/0.3对应。C的三分组权重0.3/0.4/0.3,组内正类比例1、3/4、1/3,可代入下一节求条件熵。按极限取0。
Entropy of the entire information system (系统本身的熵):
Information gain = Entropy - Conditional Entropy
Updated conditional entropy after adding feature C into system:
Feature C的conditional entropy:
- 计算P:
- 计算H
3. 带回总公式
- 计算新引入feature对系统造成的影响
The entropy gain by splitting feature C = 0.8813 - 0.6000 = 0.2813 - 同样,分别计算A引入和B引入造成的影响
The entropy gain by splitting feature A = 0.033
The entropy gain by splitting feature B = 0.553
可知引入B的增益最大,Therefore we choose feature B as the first splitting feature.
之后的其他分支,就在选定第一个的情况下计算
1.4 Compare Decision Tree with Linear Regression
We can formulate decision tree model as the following formula:
这里表示的是输入数据第k个feature对输出结果产生的作用,它可能是信息熵的减少,也可能是Gini Impurity的减少。上面公式与linear regression公式相比较:
在linear regression中,这一项对f(X)的contribution是确定的,只与自己的取值和对应的系数相关。而Decision Tree中的取值,不仅仅与有关,也与X中其他feature所决定的整个Decision Path有关(更具体地说,feature在path上的先后顺序不同,它的contribution就会不同)。很显然,这是一种非线性关系。
Decision Tree:
- 优点:可以用来描述x和y之间更复杂的关系,建模能力强
- 确定:严重overfitting
克服overfitting:Pruning(剪枝)
- Post-pruning
- Pre-pruning
整理注: 信息增益或Gini下降用于选择分裂,不是预测值本身的可加贡献;树预测可写为各叶区域指示函数乘叶输出之和。
2. Ensemble learning
Ensemble learning includes Boosting method and Bagging method:
- Bagging -> focus on reducing variance
- Learn independently from each other in parallel
- Combine with deterministic averaging process
- Boosting -> focus on reducing bias
- Learn sequentially in an adaptative way (a base model depends on the previous ones)
- Combine with deterministic strategy
3. Random Forest
3.1 Bagging
- Bagging == bootstrap aggregating
- Bagging is an ensemble learning method
- Bagging method: sampling with replacement
- Advantage:
Decrease variance by introducing randomness into your model frameword - Random Forest leverages a bagging method to solve overfitting problems in complete decision trees
3.2 Description of Random Forest
假设总体数据中有N个样本,每个样本有m个特征属性(feature的个数是m个)
- 每次从原数据中有放回地随机选取N个观测值(注意这个过程中有的样本可能被选择了多次,有的样本可能没有被包含在取出的N个观测值中)
- 在每个候选分裂节点重新随机选取k(k<m)个特征,利用这k个特征计算出一个最佳分裂方式的decision tree
- 多次重复上面的操作,得到一组互不相同的decision tree,把他们整合起来构成random forest模型,对新数据进行分类或者预测
The recommend k = floor(sqrt(m))
随机森林: 每棵树从原训练集bootstrap抽样;每个节点从随机特征子集选分裂;分类通常投票/平均概率,回归平均预测。分类常以sqrt(m)为特征数起点,具体值需验证,回归不必沿用同一默认。
3.3 Summary of Random Forest
- Sampling idea in Random Forest algorithm:
- Bagging for data
- Sampling for feature
- In the random forest algorithm, we do NOT need to do pruning for each “weak” decision tree.
- Advantage
- Less overfitting
- Parallel implementation
- Random Forest vs. Bagging
Difference at the feature sampling step.
3.4 Feature Importance value in Random Forest
It does not signify positive or negative impact on the class label. It only judges how much class discriminatory information each variable contains.
4. K-nearest-neighbors
4.1 Model introduction
KNN示意: 查询点先按所选距离找到最近K个训练点;分类作投票或距离加权,回归作均值或加权均值。尺度不同的特征可能主导距离,应在训练集拟合缩放。
- K denotes the k labels that are “closest” to your target sample
- The higher K is, the smoother (more robust) your model can be.
- Multiple definition choices for “distance”
- CityBlock Distance, Euclidean Distance
4.2 Implement KNN algorithm
Find the top K shortest distance from your returned unsorted distance array.
见30 相关笔记
4.3 FLANN annoy faiss
OpenCV的最近邻开源库FLANN - locality-sensitive hashing
https://github.com/spotify/annoy
N is total number of training data
m is average data size of split areas
O(N) -> O(m) (m<<N)
整理注: FLANN、Annoy、Faiss 是不同的近邻检索项目,不能全都概括为LSH;复杂度与索引类型、维数及召回目标有关。