Tree (on)

Benpian essay is content data scientists study the seventh week, the main reference is:

Time Geeks - data analysis at 45 combat the stresses of the decision tree

 

  The above figure is a typical decision tree. We made the decision tree in time, will go through two stages:

    • structure
    • Pruning

  Construction : choose what attributes as a process node. Constructing a decision tree needs to address the following issues:
    • Choose which attributes / characteristics as the root node?
    • What attributes / characteristics selected as the internal nodes?
    • When to stop and get the target state, that is, leaf nodes - the result of the final decision
  Over-fitting and underfitting
    • Over-fitting, you have to understand this concept, it refers to the results of the training model of "good," and that in the actual application, there will be the case, "rigid", leading to misclassification.
    • Underfitting, and over-fitting is like the first and third cases, the results of this training the following figure "very good", but in the actual application process can lead to misclassification.

  One of the reasons is because of over-fitting the training set small sample size. If the tree selected attribute too much, constructed decision tree will be able to "perfect" to classify the sample in the training set, but this will put some of the features in the training set of data as a feature of all the data, but this feature is not necessarily It features all the data, which makes this decision tree errors in real data classification, which is the model of "generalization" poor.

  Generalization refers to the ability of the classifier is abstracted through the training set classification capabilities can also be understood is giving top priority to capacity. If we are too dependent on the training data set, then the resulting tree will be relatively low fault rate, poor generalization ability. Because the training set but all the data sampling, and can not reflect the characteristics of all the data.

      Pruning : In order to ensure adequate model generalization, classification trees need to be pruned. Pruning is divided into pre-pruning and pruning.

  • After pruning:
后剪枝就是在生成决策树之后再进行剪枝,通常会从决策树的叶节点开始,逐层向上对每个节点进行评估。如果剪掉这个节点子树,与保留该节点子树在分类准确性上差别不大,或者剪掉该节点子树,能在验证集中带来准确性的提升,那么就可以把该节点子树进行剪枝(如何衡量准确性的差异?)。方法是:用这个节点子树的叶子节点来替代该节点,类标记为这个节点子树中最频繁的那个类。

  从上图这个数据集看,我们需要确定将哪一个条件作为根节点,哪一个作为中间节点。哪个条件作为根节点。所以需要通过2个指标度量: 

    • 纯度-让目标变量的分歧最小
    • 信息熵(entropy)-信息的不确定性

 

  假设有2个集合,集合1:5次去打篮球,1次不去打篮球;集合2:3次去打篮球,3次不去打篮球。假设类别1为“打篮球”,类别2为“不打篮球”(注意:这里度量的信息熵是按照是否打篮球来度量),则信息熵为:
    • 集合1: Entropy(t) = -1/6log2 1/6 - -5/6log2 5/6 = 0.65
    • 集合2: Entropy(t) = -3/6log2 3/6 - -3/6log2 3/6 = 1

  信息熵越大,纯度越低;当集合中样本均匀混合时,信息熵最大,纯度最低。上述信息熵公式是下面各分类算法的基础。

  基于上述纯度和信息熵的定义,我们会使用到如下3种指标:

    • 信息增益— ID3算法
    • 信息增益率 — C4.5算法
    • 基尼指数 — Cart算法
  • 信息增益— ID3算法

  公式D是父亲节点,Di是子节点。Gain(D,a)中的a作为D节点的属性选择。
 
  步骤:
    • 计算根节点的信息熵(注意根节点是“是否打篮球”的结果,因为我们还不知道用哪个属性作为根节点— 也是我们要求解的过程)
    • 计算某一个节点(某一列)下每个属性的信息熵
    • 计算这个节点归一化信息熵
    • 计算Gain(D,a)
    • 按上述步骤,遍历所有的节点(所有的列/所有的属性)后,得到每个列的Gain,取最大值的节点为当前节点。
    • 按上述步骤,计算出所有的节点。
  以上述是否打篮球举例:
    • 计算根节点的信息熵(注意根节点是“是否打篮球”的结果,因为我们还不知道用哪个属性作为根节点— 也是我们要求解的过程)
    我们基于 ID3 的算法规则,完整地计算我们的训练集,训练集中一共有 7 条数据,3 个打篮球,4 个不打篮球,所以根节点(是否打篮球)的信息熵是:
Ent(D) = -(3/7(log2 3/7) + 4/7(log2 4/7)) = 0.985
    • 计算某一个节点(某一列)下每个属性的信息熵(假设计算第一列天气— 以天气作为属性的划分)

    那么会有三个叶子节点 D1、D2 和 D3,分别对应的是晴天、阴天和小雨。我们用 + 代表去打篮球,- 代表不去打篮球。那么第一条记录,晴天不去打篮球,可以记为 1-,于是我们可以用下面的方式来记录 D1,D2,D3

      D1(天气 = 晴天)={1-,2-,6+}

      D2(天气 = 阴天)={3+,7-}

      D3(天气 = 小雨)={4+,5-}
    我们先分别计算三个叶子节点的信息熵:
 
 
    • 计算这个节点归一化信息熵
    因为 D1 有 3 个记录,D2 有 2 个记录,D3 有 2 个记录,所以 D 中的记录一共是 3+2+2=7,即总数为 7。所以 D1 在 D(父节点)中的概率是 3/7,D2 在父节点的概率是 2/7,D3 在父节点的概率是 2/7。那么作为子节点的归一化信息熵 = 3/7*0.918+2/7*1.0+2/7*1.0=0.965。
    • 计算Gain(D,a)
    因为我们用 ID3 中的信息增益来构造决策树,所以要计算每个节点的信息增益。
    天气作为属性节点的信息增益为,Gain(D , 天气)=0.985-0.965=0.020
    • 按上述步骤,遍历所有的节点(所有的列)后,得到每个列的Gain,取最大值的节点为当前节点。
  同理我们可以计算出其他属性作为根节点的信息增益,它们分别为 :
    Gain(D , 温度)=0.128
    Gain(D , 湿度)=0.020
    Gain(D , 刮风)=0.020
  我们能看出来温度作为属性的信息增益最大。因为 ID3 就是要将信息增益最大的节点作为父节点,这样可以得到纯度高的决策树,所以我们将温度作为根节点。其决策树为:

 

    • 然后我们要将上图中第一个叶节点,也就是 D1={1-,2-,3+,4+}进一步进行分裂,往下划分,计算其不同属性(天气、湿度、刮风)作为节点的信息增益,可以得到:
    Gain(D , 湿度)=1
    Gain(D , 天气)=1
    Gain(D , 刮风)=0.3115
    计算过程推导 -- 和上述过程类似:

        1.计算父节点-- 即温度为高的节点的信息熵为:

          Ent(D) = - (2/4log2 2/4 + 2/4log2 2/4) = - (-1) = 1

        2.如果以天气作为接下来的属性划分节点,则天气和数据集分为“晴” - (1-,2-),“阴”(3+),“下雨”(4+)—— 注意只在上述D1的数据集中划分,则天气的归一化信息熵为: 

          2/4(2/2log2 2/2) + 1/4(1/1log2 1/1) + 1/4(1/1log2 1/1) = 2/4(0) + 1/4(0) + 1/4(0) = 0.

        3.接下来以此类推,即:决定了湿度是温度为高的子节点后,需要决定温度为“中”的子节点是什么;然后是温度为低的子节点...我们能看到湿度,或者天气为 D1 的节点都可以得到最大的信息增益,这里我们选取湿度作为节点的属性划分。同理,我们可以按照上面的计算步骤得到完整的决策树,结果如下:

 

 下篇继续介绍C4.5和Cart算法。

Guess you like

Origin www.cnblogs.com/favor-dfn/p/12076601.html