Example Decision Tree
Example Decision Tree
Gini Index is a metric to measure how often a randomly chosen element would be incorrectly
identified. It means an attribute with lower gini index should be preferred.
We are going to use same data sample that we used for information gain example. Let’s try to
use gini index as a criterion. Here, we have 5 columns out of which 4 columns have continuous
data and 5th column consists of class labels.
A, B, C, D attributes can be considered as predictors and E column class labels can be considered
as a target variable. For constructing a decision tree from this data, we have to convert
continuous data into categorical data.
A B C D
Var A has value >=5 for 12 records out of 16 and 4 records with value <5 value.
Var B has value >=3 for 12 records out of 16 and 4 records with value <5 value.
Var C has value >=4.2 for 6 records out of 16 and 10 records with value <4.2 value.
Var D has value >=1.4 for 5 records out of 16 and 11 records with value <1.4 value.
For Var D >= 1.4 & class == positive: 0/5
For Var D >= 1.4 & class == negative: 5/5
o gini(0,5) = 1- ( (0/5)2 + (5/5)2 ) = 0
For Var D < 1.4 & class == positive: 8/11
For Var D < 1.4 & class == negative: 3/11
o gin(8,3) = 1- ( (8/11)2 + (3/11)2 ) = 0.397
wTarget Target
>= >=
5 7 8 4
5.0 3.0
A B
<5 3 1 < 3.0 0 4
Target
Target
Positive Negative
Positive Negative
>=
>= 4.2 0 6 0 5
1.4
C D
< 4.2 8 2
< 1.4 8 3
Gini Index of C= 0.2
Gini Index of D= 0.273
Entropy
A, B, C, D attributes can be considered as predictors and E column class labels can be considered
as a target variable. For constructing a decision tree from this data, we have to convert
continuous data into categorical data.
A B C D
The entropy of Target: We have 8 records with negative class and 8 records with positive class.
So, we can directly estimate the entropy of target as 1.
Variable E
Positive Negative
8 8
Var A has value >=5 for 12 records out of 16 and 4 records with value <5 value.
Var B has value >=3 for 12 records out of 16 and 4 records with value <5 value.
Var D has value >=1.4 for 5 records out of 16 and 11 records with value <5 value.
Target Target
>= >=
5 7 8 4
5.0 3.0
A B
<5 3 1 < 3.0 0 4
Target Target
From the above Information Gain calculations, we can build a decision tree. We should place the
attributes on the tree according to their values.
An Attribute with better value than other should position as root and A branch with entropy 0
should be converted to a leaf node. A branch with entropy more than 0 needs further splitting.