当前位置: 首页 > article >正文

使用ID3算法根据信息增益构建决策树

使用ID3算法根据信息增益构建决策树

      • 1. 计算数据集的熵(Entropy of the dataset)
      • 2. 按特征划分数据集并计算条件熵
        • (1)特征 `election`
        • (2)特征 `season`
        • (3)特征 `oil price`
      • 3. 总结信息增益
      • 4 根节点选择
      • 5. 根据 `oil price` 划分数据集
        • (1) 子集 1: `oil price = rise`
        • (2) 子集 2: `oil price = fall`
      • 6. 对 `oil price = rise` 的子集继续划分
        • (1) 特征 `election`
        • (2) 特征 `season`
        • (3) 选择划分特征
      • 7. 根据 `election` 划分子集
        • (1) 子集 1: `election = yes`
        • (2) 子集 2: `election = no`
      • 8. 对 `election = yes` 的子集继续划分
      • 9. 构建完整决策树

根据ID3(信息增益)方法利用以下数据集构建决策树。

electionseasonoil pricestock price
nowinterriserise
nosummerfallfall
nosummerriserise
yeswinterrisefall
nowinterfallfall
yessummerriserise
yessummerfallfall
yeswinterfallfall

1. 计算数据集的熵(Entropy of the dataset)

熵的公式为:

H ( S ) = − ∑ i = 1 n p i log ⁡ 2 ( p i ) H(S) = - \sum_{i=1}^n p_i \log_2(p_i) H(S)=i=1npilog2(pi)

其中 p i p_i pi 是每个类别的概率。

在数据集中,最后一列 stock price 有两种可能的值:risefall。统计每种值的频率:

  • rise 出现了 3 次。
  • fall 出现了 5 次。
  • 数据集总共有 8 条记录。

因此,rise 的概率 p ( r i s e ) = 3 8 p(rise) = \frac{3}{8} p(rise)=83fall 的概率 p ( f a l l ) = 5 8 p(fall) = \frac{5}{8} p(fall)=85

H ( S ) H(S) H(S) 为:

H ( S ) = − ( 3 8 log ⁡ 2 3 8 + 5 8 log ⁡ 2 5 8 ) H(S) = - \left( \frac{3}{8} \log_2 \frac{3}{8} + \frac{5}{8} \log_2 \frac{5}{8} \right) H(S)=(83log283+85log285)

计算:

H ( S ) = − ( 0.375 log ⁡ 2 0.375 + 0.625 log ⁡ 2 0.625 ) H(S) = - \left( 0.375 \log_2 0.375 + 0.625 \log_2 0.625 \right) H(S)=(0.375log20.375+0.625log20.625)

log ⁡ 2 0.375 ≈ − 1.415 , log ⁡ 2 0.625 ≈ − 0.678 \log_2 0.375 \approx -1.415, \quad \log_2 0.625 \approx -0.678 log20.3751.415,log20.6250.678

H ( S ) = − ( 0.375 × − 1.415 + 0.625 × − 0.678 ) H(S) = - \left( 0.375 \times -1.415 + 0.625 \times -0.678 \right) H(S)=(0.375×1.415+0.625×0.678)

H ( S ) = − ( − 0.5306 − 0.42375 ) = 0.95435 H(S) = - \left( -0.5306 - 0.42375 \right) = 0.95435 H(S)=(0.53060.42375)=0.95435

数据集的熵为 H ( S ) ≈ 0.954 H(S) \approx 0.954 H(S)0.954


2. 按特征划分数据集并计算条件熵

我们需要分别计算每个特征(electionseasonoil price)的条件熵。

(1)特征 election

election 有两个取值:yesno

  • election = yes 时,有 4 条记录,其中:

    • rise 出现 1 次,fall 出现 3 次。
    • 熵为:
      H ( S ∣ e l e c t i o n = y e s ) = − ( 1 4 log ⁡ 2 1 4 + 3 4 log ⁡ 2 3 4 ) H(S|election=yes) = - \left( \frac{1}{4} \log_2 \frac{1}{4} + \frac{3}{4} \log_2 \frac{3}{4} \right) H(Selection=yes)=(41log241+43log243)
      H ( S ∣ e l e c t i o n = y e s ) = − ( 0.25 × − 2 + 0.75 × − 0.415 ) = 0.811 H(S|election=yes) = - \left( 0.25 \times -2 + 0.75 \times -0.415 \right) = 0.811 H(Selection=yes)=(0.25×2+0.75×0.415)=0.811
  • election = no 时,有 4 条记录,其中:

    • rise 出现 2 次,fall 出现 2 次。
    • 熵为:
      H ( S ∣ e l e c t i o n = n o ) = − ( 2 4 log ⁡ 2 2 4 + 2 4 log ⁡ 2 2 4 ) H(S|election=no) = - \left( \frac{2}{4} \log_2 \frac{2}{4} + \frac{2}{4} \log_2 \frac{2}{4} \right) H(Selection=no)=(42log242+42log242)
      H ( S ∣ e l e c t i o n = n o ) = − ( 0.5 × − 1 + 0.5 × − 1 ) = 1 H(S|election=no) = - \left( 0.5 \times -1 + 0.5 \times -1 \right) = 1 H(Selection=no)=(0.5×1+0.5×1)=1

条件熵 H ( S ∣ e l e c t i o n ) H(S|election) H(Selection) 为:

H ( S ∣ e l e c t i o n ) = 4 8 H ( S ∣ e l e c t i o n = y e s ) + 4 8 H ( S ∣ e l e c t i o n = n o ) H(S|election) = \frac{4}{8} H(S|election=yes) + \frac{4}{8} H(S|election=no) H(Selection)=84H(Selection=yes)+84H(Selection=no)

H ( S ∣ e l e c t i o n ) = 0.5 × 0.811 + 0.5 × 1 = 0.9055 H(S|election) = 0.5 \times 0.811 + 0.5 \times 1 = 0.9055 H(Selection)=0.5×0.811+0.5×1=0.9055

信息增益 I G ( S , e l e c t i o n ) IG(S, election) IG(S,election) 为:

I G ( S , e l e c t i o n ) = H ( S ) − H ( S ∣ e l e c t i o n ) IG(S, election) = H(S) - H(S|election) IG(S,election)=H(S)H(Selection)

I G ( S , e l e c t i o n ) = 0.954 − 0.9055 = 0.0485 IG(S, election) = 0.954 - 0.9055 = 0.0485 IG(S,election)=0.9540.9055=0.0485


(2)特征 season

season 有两个取值:wintersummer

  • season = winter 时,有 4 条记录,其中:

    • rise 出现 1 次,fall 出现 3 次。
    • 熵为:
      H ( S ∣ s e a s o n = w i n t e r ) = − ( 1 4 log ⁡ 2 1 4 + 3 4 log ⁡ 2 3 4 ) = 0.811 H(S|season=winter) = - \left( \frac{1}{4} \log_2 \frac{1}{4} + \frac{3}{4} \log_2 \frac{3}{4} \right) = 0.811 H(Sseason=winter)=(41log241+43log243)=0.811
  • season = summer 时,有 4 条记录,其中:

    • rise 出现 2 次,fall 出现 2 次。
    • 熵为:
      H ( S ∣ s e a s o n = s u m m e r ) = − ( 2 4 log ⁡ 2 2 4 + 2 4 log ⁡ 2 2 4 ) = 1 H(S|season=summer) = - \left( \frac{2}{4} \log_2 \frac{2}{4} + \frac{2}{4} \log_2 \frac{2}{4} \right) = 1 H(Sseason=summer)=(42log242+42log242)=1

条件熵 H ( S ∣ s e a s o n ) H(S|season) H(Sseason) 为:

H ( S ∣ s e a s o n ) = 4 8 H ( S ∣ s e a s o n = w i n t e r ) + 4 8 H ( S ∣ s e a s o n = s u m m e r ) H(S|season) = \frac{4}{8} H(S|season=winter) + \frac{4}{8} H(S|season=summer) H(Sseason)=84H(Sseason=winter)+84H(Sseason=summer)

H ( S ∣ s e a s o n ) = 0.5 × 0.811 + 0.5 × 1 = 0.9055 H(S|season) = 0.5 \times 0.811 + 0.5 \times 1 = 0.9055 H(Sseason)=0.5×0.811+0.5×1=0.9055

信息增益 I G ( S , s e a s o n ) IG(S, season) IG(S,season) 为:

I G ( S , s e a s o n ) = H ( S ) − H ( S ∣ s e a s o n ) IG(S, season) = H(S) - H(S|season) IG(S,season)=H(S)H(Sseason)

I G ( S , s e a s o n ) = 0.954 − 0.9055 = 0.0485 IG(S, season) = 0.954 - 0.9055 = 0.0485 IG(S,season)=0.9540.9055=0.0485


(3)特征 oil price

oil price 有两个取值:risefall

  • oil price = rise 时,有 4 条记录,其中:

    • rise 出现 3 次,fall 出现 1 次。
    • 熵为:
      H ( S ∣ o i l p r i c e = r i s e ) = − ( 3 4 log ⁡ 2 3 4 + 1 4 log ⁡ 2 1 4 ) H(S|oil price=rise) = - \left( \frac{3}{4} \log_2 \frac{3}{4} + \frac{1}{4} \log_2 \frac{1}{4} \right) H(Soilprice=rise)=(43log243+41log241)
      H ( S ∣ o i l p r i c e = r i s e ) = − ( 0.75 × − 0.415 + 0.25 × − 2 ) = 0.811 H(S|oil price=rise) = - \left( 0.75 \times -0.415 + 0.25 \times -2 \right) = 0.811 H(Soilprice=rise)=(0.75×0.415+0.25×2)=0.811
  • oil price = fall 时,有 4 条记录,其中:

    • rise 出现 0 次,fall 出现 4 次。
    • 熵为:
      H ( S ∣ o i l p r i c e = f a l l ) = − ( 0 4 log ⁡ 2 0 4 + 4 4 log ⁡ 2 4 4 ) = 0 H(S|oil price=fall) = - \left( \frac{0}{4} \log_2 \frac{0}{4} + \frac{4}{4} \log_2 \frac{4}{4} \right) = 0 H(Soilprice=fall)=(40log240+44log244)=0

条件熵 H ( S ∣ o i l p r i c e ) H(S|oil price) H(Soilprice) 为:

H ( S ∣ o i l p r i c e ) = 4 8 H ( S ∣ o i l p r i c e = r i s e ) + 4 8 H ( S ∣ o i l p r i c e = f a l l ) H(S|oil price) = \frac{4}{8} H(S|oil price=rise) + \frac{4}{8} H(S|oil price=fall) H(Soilprice)=84H(Soilprice=rise)+84H(Soilprice=fall)

H ( S ∣ o i l p r i c e ) = 0.5 × 0.811 + 0.5 × 0 = 0.4055 H(S|oil price) = 0.5 \times 0.811 + 0.5 \times 0 = 0.4055 H(Soilprice)=0.5×0.811+0.5×0=0.4055

信息增益 I G ( S , o i l p r i c e ) IG(S, oil price) IG(S,oilprice) 为:

I G ( S , o i l p r i c e ) = H ( S ) − H ( S ∣ o i l p r i c e ) IG(S, oil price) = H(S) - H(S|oil price) IG(S,oilprice)=H(S)H(Soilprice)

I G ( S , o i l p r i c e ) = 0.954 − 0.4055 = 0.5485 IG(S, oil price) = 0.954 - 0.4055 = 0.5485 IG(S,oilprice)=0.9540.4055=0.5485


3. 总结信息增益

  • I G ( S , e l e c t i o n ) = 0.0485 IG(S, election) = 0.0485 IG(S,election)=0.0485
  • I G ( S , s e a s o n ) = 0.0485 IG(S, season) = 0.0485 IG(S,season)=0.0485
  • I G ( S , o i l p r i c e ) = 0.5485 IG(S, oil price) = 0.5485 IG(S,oilprice)=0.5485

因此,oil price 是信息增益最大的特征。


4 根节点选择

在上一步中,我们计算了各特征的信息增益:

  • I G ( S , e l e c t i o n ) = 0.0485 IG(S, election) = 0.0485 IG(S,election)=0.0485
  • I G ( S , s e a s o n ) = 0.0485 IG(S, season) = 0.0485 IG(S,season)=0.0485
  • I G ( S , o i l p r i c e ) = 0.5485 IG(S, oil price) = 0.5485 IG(S,oilprice)=0.5485

由于 oil price 的信息增益最大,我们选择 oil price 作为根节点。


5. 根据 oil price 划分数据集

oil price 有两个取值:risefall

(1) 子集 1: oil price = rise

oil price = rise 时,数据如下:

electionseasonoil pricestock price
nowinterriserise
nosummerriserise
yeswinterrisefall
yessummerriserise
  • stock price = rise 出现 3 次。
  • stock price = fall 出现 1 次。

熵为:

H ( S ∣ o i l p r i c e = r i s e ) = − ( 3 4 log ⁡ 2 3 4 + 1 4 log ⁡ 2 1 4 ) H(S|oil price=rise) = - \left( \frac{3}{4} \log_2 \frac{3}{4} + \frac{1}{4} \log_2 \frac{1}{4} \right) H(Soilprice=rise)=(43log243+41log241)

H ( S ∣ o i l p r i c e = r i s e ) = − ( 0.75 × − 0.415 + 0.25 × − 2 ) = 0.811 H(S|oil price=rise) = - \left( 0.75 \times -0.415 + 0.25 \times -2 \right) = 0.811 H(Soilprice=rise)=(0.75×0.415+0.25×2)=0.811

由于熵不为 0,oil price = rise 的子集还需要进一步划分。


(2) 子集 2: oil price = fall

oil price = fall 时,数据如下:

electionseasonoil pricestock price
nosummerfallfall
nowinterfallfall
yessummerfallfall
yeswinterfallfall
  • stock price = rise 出现 0 次。
  • stock price = fall 出现 4 次。

熵为:

H ( S ∣ o i l p r i c e = f a l l ) = − ( 0 4 log ⁡ 2 0 4 + 4 4 log ⁡ 2 4 4 ) = 0 H(S|oil price=fall) = - \left( \frac{0}{4} \log_2 \frac{0}{4} + \frac{4}{4} \log_2 \frac{4}{4} \right) = 0 H(Soilprice=fall)=(40log240+44log244)=0

由于熵为 0,oil price = fall 的子集是纯的,不需要进一步划分。此时,stock price = fall 是叶节点。


6. 对 oil price = rise 的子集继续划分

现在我们只需要处理 oil price = rise 的子集:

electionseasonoil pricestock price
nowinterriserise
nosummerriserise
yeswinterrisefall
yessummerriserise

我们再次计算剩余特征的信息增益。


(1) 特征 election

election 有两个取值:yesno

  • election = yes 时,有 2 条记录,其中:

    • rise 出现 1 次,fall 出现 1 次。
    • 熵为:
      H ( S ∣ e l e c t i o n = y e s ) = − ( 1 2 log ⁡ 2 1 2 + 1 2 log ⁡ 2 1 2 ) = 1 H(S|election=yes) = - \left( \frac{1}{2} \log_2 \frac{1}{2} + \frac{1}{2} \log_2 \frac{1}{2} \right) = 1 H(Selection=yes)=(21log221+21log221)=1
  • election = no 时,有 2 条记录,其中:

    • rise 出现 2 次,fall 出现 0 次。
    • 熵为:
      H ( S ∣ e l e c t i o n = n o ) = − ( 2 2 log ⁡ 2 2 2 + 0 2 log ⁡ 2 0 2 ) = 0 H(S|election=no) = - \left( \frac{2}{2} \log_2 \frac{2}{2} + \frac{0}{2} \log_2 \frac{0}{2} \right) = 0 H(Selection=no)=(22log222+20log220)=0

条件熵 H ( S ∣ e l e c t i o n ) H(S|election) H(Selection) 为:

H ( S ∣ e l e c t i o n ) = 2 4 H ( S ∣ e l e c t i o n = y e s ) + 2 4 H ( S ∣ e l e c t i o n = n o ) H(S|election) = \frac{2}{4} H(S|election=yes) + \frac{2}{4} H(S|election=no) H(Selection)=42H(Selection=yes)+42H(Selection=no)

H ( S ∣ e l e c t i o n ) = 0.5 × 1 + 0.5 × 0 = 0.5 H(S|election) = 0.5 \times 1 + 0.5 \times 0 = 0.5 H(Selection)=0.5×1+0.5×0=0.5

信息增益 I G ( S , e l e c t i o n ) IG(S, election) IG(S,election) 为:

I G ( S , e l e c t i o n ) = H ( S ∣ o i l p r i c e = r i s e ) − H ( S ∣ e l e c t i o n ) IG(S, election) = H(S|oil price=rise) - H(S|election) IG(S,election)=H(Soilprice=rise)H(Selection)

I G ( S , e l e c t i o n ) = 0.811 − 0.5 = 0.311 IG(S, election) = 0.811 - 0.5 = 0.311 IG(S,election)=0.8110.5=0.311


(2) 特征 season

season 有两个取值:wintersummer

  • season = winter 时,有 2 条记录,其中:

    • rise 出现 1 次,fall 出现 1 次。
    • 熵为:
      H ( S ∣ s e a s o n = w i n t e r ) = − ( 1 2 log ⁡ 2 1 2 + 1 2 log ⁡ 2 1 2 ) = 1 H(S|season=winter) = - \left( \frac{1}{2} \log_2 \frac{1}{2} + \frac{1}{2} \log_2 \frac{1}{2} \right) = 1 H(Sseason=winter)=(21log221+21log221)=1
  • season = summer 时,有 2 条记录,其中:

    • rise 出现 2 次,fall 出现 0 次。
    • 熵为:
      H ( S ∣ s e a s o n = s u m m e r ) = − ( 2 2 log ⁡ 2 2 2 + 0 2 log ⁡ 2 0 2 ) = 0 H(S|season=summer) = - \left( \frac{2}{2} \log_2 \frac{2}{2} + \frac{0}{2} \log_2 \frac{0}{2} \right) = 0 H(Sseason=summer)=(22log222+20log220)=0

条件熵 H ( S ∣ s e a s o n ) H(S|season) H(Sseason) 为:

H ( S ∣ s e a s o n ) = 2 4 H ( S ∣ s e a s o n = w i n t e r ) + 2 4 H ( S ∣ s e a s o n = s u m m e r ) H(S|season) = \frac{2}{4} H(S|season=winter) + \frac{2}{4} H(S|season=summer) H(Sseason)=42H(Sseason=winter)+42H(Sseason=summer)

H ( S ∣ s e a s o n ) = 0.5 × 1 + 0.5 × 0 = 0.5 H(S|season) = 0.5 \times 1 + 0.5 \times 0 = 0.5 H(Sseason)=0.5×1+0.5×0=0.5

信息增益 I G ( S , s e a s o n ) IG(S, season) IG(S,season) 为:

I G ( S , s e a s o n ) = H ( S ∣ o i l p r i c e = r i s e ) − H ( S ∣ s e a s o n ) IG(S, season) = H(S|oil price=rise) - H(S|season) IG(S,season)=H(Soilprice=rise)H(Sseason)

I G ( S , s e a s o n ) = 0.811 − 0.5 = 0.311 IG(S, season) = 0.811 - 0.5 = 0.311 IG(S,season)=0.8110.5=0.311


(3) 选择划分特征

electionseason 的信息增益相同(均为 0.311)。我们可以任选一个作为划分特征。这里选择 election


7. 根据 election 划分子集

(1) 子集 1: election = yes

election = yes 时,数据如下:

electionseasonoil pricestock price
yeswinterrisefall
yessummerriserise
  • stock price = rise 出现 1 次。
  • stock price = fall 出现 1 次。

熵为:

H ( S ∣ e l e c t i o n = y e s ) = − ( 1 2 log ⁡ 2 1 2 + 1 2 log ⁡ 2 1 2 ) = 1 H(S|election=yes) = - \left( \frac{1}{2} \log_2 \frac{1}{2} + \frac{1}{2} \log_2 \frac{1}{2} \right) = 1 H(Selection=yes)=(21log221+21log221)=1

由于熵不为 0,还需要进一步划分。


(2) 子集 2: election = no

election = no 时,数据如下:

electionseasonoil pricestock price
nowinterriserise
nosummerriserise
  • stock price = rise 出现 2 次。
  • stock price = fall 出现 0 次。

熵为:

H ( S ∣ e l e c t i o n = n o ) = − ( 2 2 log ⁡ 2 2 2 + 0 2 log ⁡ 2 0 2 ) = 0 H(S|election=no) = - \left( \frac{2}{2} \log_2 \frac{2}{2} + \frac{0}{2} \log_2 \frac{0}{2} \right) = 0 H(Selection=no)=(22log222+20log220)=0

此时,stock price = rise 是叶节点。


8. 对 election = yes 的子集继续划分

对于 election = yes 的子集:

electionseasonoil pricestock price
yeswinterrisefall
yessummerriserise

我们可以选择 season 作为划分特征。

  • season = winter 时,stock price = fall
  • season = summer 时,stock price = rise

此时,两个子集都是纯的。


9. 构建完整决策树

最终决策树如下:

oil price?
├── fall: stock price = fall
└── rise:
    ├── election?
    │   ├── no: stock price = rise
    │   └── yes:
    │       ├── season?
    │       │   ├── winter: stock price = fall
    │       │   └── summer: stock price = rise


http://www.kler.cn/a/449609.html

相关文章:

  • 中小企业的助力工具:项目管理系统如何优化资源配置?
  • golang 链路追踪
  • 计算机毕业设计Python+Spark知识图谱高考志愿推荐系统 高考数据分析 高考可视化 高考大数据 大数据毕业设计
  • 快速定位sql索引未使用上的问题
  • C语言指针与数组深入剖析及优化示例 指针解读 数组与指针的关系
  • 如何正确计算显示器带宽需求
  • Sequelize ORM sql 语句工具
  • 安装MongoDB,环境配置
  • 对于其他管理的理解(下)
  • C#调用WebService的方法
  • 基于 SSM 架构的 JAVA 网络直播带货查询系统设计与 JSP 实践成果
  • 轻量型 5G DTU 选型
  • 记Fastjson2的一个报ConcurrentModificationException的bug
  • 带通/带阻滤波器
  • 云原生周刊:利用 eBPF 增强 K8s
  • CSDN实现免登录复制-油猴制作全过程
  • 电脑无法开机的解决方案
  • 【Maven】Maven的快照库和发行库
  • Chrome 关闭自动添加https
  • 记录vue+elementUI table的组件