Decision Tree
Decision Tree
In this way, the Decision Tree divides into different groups based on some
conditions.
In this dataset, we can see fruits were labelled into either Mango, Grape, or
Lemon based on colour and Diameter.
Here we take the root node as diameter. If the diameter is greater than 3
then we have colour as either Green or yellow. and so if the diameter is
less than 3 then for sure it is grape which is red.
Now we have to check the colour. If it is not yellow then for sure it is
mango. But if it is in yellow then we have two options. it can be either
mango or lemon. So there is a chance for 50% mango and 50% Lemon.
But which question comes as the root node and which question comes
next? Here we need to see which attribute will unmix the label at that
particular point. We can find the amount of uncertainty at a single node with
something known as Gini impurity and also we can find how much a
question reduces that uncertainty with something known as Information
Gain.
Terminologies
Root node: The root node is the base node of the tree where the entire
tree starts from it.
Parent Node/Child Node: Always root node is the parent node and all the
other nodes which are derived from the parent node are known as child
nodes.
CART Algorithm
Here there are attributes like Outlook, temperature, humidity, wind, and a
label Play. All these attributes decide whether to play or not.
So among all of them which one should we pick first? The attribute which
classifies the training data best will be picked first.
Gini Index: Gini Index is the measure of impurity or the purity that is used
in building a decision tree in the CART Algorithm.
Constructing a decision tree is all about finding the attribute that has the
highest information gain.
The first step to do before solving the problem for the decision tree is
entropy which is used to find information gain. As we know splitting will be
done based on information gain. Attribute with highest information gain is
selected first.
Imagine a basket full of cherries and a bowl that contains labels with cherry
written on it. Now if you take 1 fruit from the basket and 1 label from a bowl.
So the probability of matching cherry with cherry is 1 and there is no
impurity here.
Now imagine another situation with different fruits in a basket and different
fruits names labels in the bowl. Now if you pick up 1 random fruit from the
basket and 1 random label from the bowl, the probability of matching cherry
with cherry is certainly not 1. It is less than 1. Here there is impurity.
Entropy=-ΣP(x)logP(x)/
Entropy(s)=-P(yes)logP(yes)-P(no)logP(no)
where,
P(no) = Probability of no
Entropy(s)=-P(yes)logP(yes)
E(s)=0.5(log20.5 – log20.5)
E(s)=1
let’s see second case where it contains either all yes or all no
Entropy(s)=-P(yes)logP(yes)
E(s)=1log1
E(s)=0
Entropy(s)=-P(no)logP(no)
E(s)=1log1
E(s)=0
Let’s calculate the entropy for this dataset. Here total we have 14 data
points in which 9 are yes and 5 are no.
Entropy(s)=-P(yes)logP(yes)-P(no)logP(no)
E(s)=-(9/14)log(9/14) – (5/14)log(5/14)
E(s)=0.41+0.53
E(s)=0.94
Here for sunny, we have 3 yes and 2 no. For Overcast, we have all yes.
And for rainy we have 3 yes and 2 no.
E(outlook=Overcast)= -1log1 =0
IG(Outlook)=E(s) -I(outlook)
=0.94 – 0.693
=0.247
Now lets consider windy as root node and calculate the information gain.
In the case of False, we have 6 yes and 2 no whereas in the case of True
we have 3 yes and 3 no.
E(windy=True)=1
We can observe that if the outlook is overcast then the final output will be
yes. But if it is between sunny or rain, then again we have to calculate
entropy for the remaining attributes and calculate the information gain to
decide the next node. When all these calculations are done the final tree
will look like this.
Source: Author
If Outlook is overcast then you can play without checking any other
condition. If the outlook is sunny, then check for Humidity. If humidity is
high, then don’t play. If humidity is normal then you can play. If the outlook
is Rainy, then check for Windy conditions. If windy is strong then don’t play.
If Windy is weak, then you can play.