统计与机器学习 / 21

非线性模型:树、随机森林与近邻

从假设、目标到模型和评估。 非线性模型:树、随机森林与近邻。

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

1. Decision Tree

1.1 Types

分类树与回归树: 同样沿条件分支进入叶节点;分类叶输出类别概率/类别,回归叶输出数值(平方误差目标下通常取叶样本均值)。

  • Left: Classification Tree
  • Right: Regression Tree

1.2 Terminologies

树术语: 根节点从全部训练样本开始;内部节点按特征条件分裂;边表示条件分支;叶节点给出预测;某节点及其后代组成子树;剪枝删除部分分支以控制复杂度。

  1. Root Node
  2. Splitting
  3. Decision Node
  4. Leaf/Terminal Node
  5. Pruning(剪枝)
  6. Branch/Sub-Tree
  7. 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(熵):
Ie(i)=−log⁡2piI_e(i)=-\log_{2}{p_i}

Entropy of the entire information system:

H=∑i=1npi∗Ie(i)=−∑i=1npilog⁡2piH=\sum_{i=1}^{n}p_i*I_e(i)=-\sum_{i=1}^{n}p_i\log_{2}{p_i}

Example:

熵与分裂: 原类别比例0.7/0.3对应H(Y)=−0.7log⁡20.7−0.3log⁡20.3≈0.8813H(Y)=-0.7\log_2 0.7-0.3\log_2 0.3\approx0.8813。C的三分组权重0.3/0.4/0.3,组内正类比例1、3/4、1/3,可代入下一节求条件熵。0log⁡00\log0按极限取0。

Entropy of the entire information system (系统本身的熵):
P(Y)=0.7,  P(N)=0.3P(Y)=0.7,\; P(N)=0.3

H=∑i=1npi∗Ie(i)=−∑i=1npilog⁡2pi=Yes的熵+No的熵=−(pY∗log⁡2pY+pN∗log⁡2pN)=−(0.7∗log⁡20.7+0.3∗log⁡20.3)=0.8813\begin{aligned} H & = \sum^n_{i=1}p_i*I_e(i) \\ & = -\sum_{i=1}^{n}p_i\log_{2}{p_i} \\ & = \text{Yes的熵}+\text{No的熵} \\ & = -(p_Y*\log_2{p_Y}+p_N*\log_2{p_N}) \\ & = -(0.7*\log_2{0.7}+0.3*\log_2{0.3}) \\ & = 0.8813 \\ \end{aligned}

Information gain = Entropy - Conditional Entropy
Updated conditional entropy after adding feature C into system:
Feature C的conditional entropy:
H(Y∣C)=P(C=L)∗H(Y∣C=L)+P(C=M)∗H(Y∣C=M)+P(C=S)∗H(Y∣C=S)H(Y|C)=P(C=L)*H(Y|C=L)+P(C=M)*H(Y|C=M)+P(C=S)*H(Y|C=S)

  1. 计算P:
    • P(C=L)=0.3P(C=L)=0.3
    • P(C=M)=0.4P(C=M)=0.4
    • P(C=S)=0.3P(C=S)=0.3
  2. 计算H
H(Y∣C=L)=−(P(Y∣C=L)∗log⁡2p(Y∣C=L)+P(N∣C=L)∗log⁡2p(N∣C=L))=−33∗log⁡233−03∗log⁡203\begin{aligned} H(Y|C=L) & = -(P(Y|C=L)*\log_2{p(Y|C=L)}+P(N|C=L)*\log_2{p(N|C=L)}) \\ & = -\frac{3}{3}*\log_2{\frac{3}{3}}-\frac{0}{3}*\log_2{\frac{0}{3}} \\ \end{aligned}

P(Y∣C=L)=3/3,  P(N∣C=L)=0/3P(Y|C=L) = 3/3,\; P(N|C=L)=0/3
P(Y∣C=M)=3/4,  P(N∣C=M)=1/4P(Y|C=M) = 3/4,\; P(N|C=M)=1/4
P(Y∣C=S)=1/3,  P(N∣C=S)=2/3P(Y|C=S) = 1/3,\; P(N|C=S)=2/3
3. 带回总公式

H(Y∣C)=0.3∗(−33∗log⁡233−03∗log⁡203)+0.4∗(−34∗log⁡234−14∗log⁡214)+0.3∗(−13∗log⁡213−23∗log⁡223)=0.6000\begin{aligned} H(Y|C) & = 0.3*(-\frac{3}{3}*\log_2{\frac{3}{3}}-\frac{0}{3}*\log_2{\frac{0}{3}}) \\ & +0.4*(-\frac{3}{4}*\log_2{\frac{3}{4}}-\frac{1}{4}*\log_2{\frac{1}{4}}) \\ & +0.3*(-\frac{1}{3}*\log_2{\frac{1}{3}}-\frac{2}{3}*\log_2{\frac{2}{3}}) \\ & = 0.6000 \\ \end{aligned}
  1. 计算新引入feature对系统造成的影响
    The entropy gain by splitting feature C = 0.8813 - 0.6000 = 0.2813
  2. 同样,分别计算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:

f(X)=∑k=1Kcontribution(xk)f(X)=\sum^K_{k=1}contribution(x_k)

这里contribution(xk)contribution(x_k)表示的是输入数据第k个feature对输出结果产生的作用,它可能是信息熵的减少,也可能是Gini Impurity的减少。上面公式与linear regression公式相比较:

y=f(X)=b+∑k=1Kakxky=f(X)=b+\sum^K_{k=1}a_kx_k

在linear regression中,xkx_k这一项对f(X)的contribution是确定的,只与自己的取值和对应的系数相关。而Decision Tree中contribution(xk)contribution(x_k)的取值,不仅仅与xkx_k有关,也与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

importance(i)=performance(RF)−performance(RF(random i))importance(i)=performance(RF)-performance({RF}^{(random\,i)})

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;复杂度与索引类型、维数及召回目标有关。

整理参考资料