0% found this document useful (0 votes)
171 views6 pages

ID3 Algorithm Decision Tree Example

The document explains the process of building a decision tree using the ID3 algorithm based on a training dataset related to buying computers. It details the calculation of information gain for various attributes (age, income, student status, and credit rating) to determine the best attribute for splitting the data. Ultimately, it predicts that for a new example with specific attributes, the class label is 'yes' for buying a computer.

Uploaded by

sandriyap.ci23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
171 views6 pages

ID3 Algorithm Decision Tree Example

The document explains the process of building a decision tree using the ID3 algorithm based on a training dataset related to buying computers. It details the calculation of information gain for various attributes (age, income, student status, and credit rating) to determine the best attribute for splitting the data. Ultimately, it predicts that for a new example with specific attributes, the class label is 'yes' for buying a computer.

Uploaded by

sandriyap.ci23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Machine Learning -ID3 Algorithm Decision Tree – Solved Example

Build a decision tree using ID3 algorithm for the given training data in the table (Buy
Computer data), and predict the class of the following new example: age<=30,
income=medium, student=yes, credit-rating=fair

Solution:
Information gain is a measure used to determine which feature should be used to split the
data at each internal node of the decision tree.

First, check which attribute provides the highest Information Gain in order
to split the training set based on that attribute. We need to calculate the
expected information to classify the set and the entropy of each attribute.

The information gain is this mutual information minus the entropy:


The mutual information of the two classes,
Entropy(S)= E(9,5)= -9/14 log2(9/14) – 5/14 log2(5/14)=0.94

Now Consider the Age attribute


For Age, we have three values age<=30 (2 yes and 3 no), age31..40 (4 yes and
0 no), and age>40 (3 yes and 2 no)
Entropy(age) = 5/14 (-2/5 log2(2/5)-3/5log2(3/5)) + 4/14 (0) + 5/14 (-
3/5log2(3/5)-2/5log2(2/5))
= 5/14(0.9709) + 0 + 5/14(0.9709) = 0.6935

Gain(age) = 0.94 – 0.6935 = 0.2465

Next, consider Income Attribute


For Income, we have three values incomehigh (2 yes and 2 no),
incomemedium (4 yes and 2 no), and incomelow (3 yes 1 no)
Entropy(income) = 4/14(-2/4log2(2/4)-2/4log2(2/4)) + 6/14 (-4/6log2(4/6)-
2/6log2(2/6)) + 4/14 (-3/4log2(3/4)-1/4log2(1/4))
= 4/14 (1) + 6/14 (0.918) + 4/14 (0.811)

= 0.285714 + 0.393428 + 0.231714 = 0.9108

Gain(income) = 0.94 – 0.9108 = 0.0292

Next, consider Student Attribute


For Student, we have two values studentyes (6 yes and 1 no) and
studentno (3 yes 4 no)
Entropy(student) = 7/14(-6/7log2(6/7)-1/7log2(1/7)) + 7/14(-3/7log2(3/7)-
4/7log2(4/7)
= 7/14(0.5916) + 7/14(0.9852)

= 0.2958 + 0.4926 = 0.7884

Gain (student) = 0.94 – 0.7884 = 0.1516

Finally, consider Credit_Rating Attribute


For Credit_Rating we have two values credit_ratingfair (6 yes and 2 no)
and credit_ratingexcellent (3 yes 3 no)

Entropy(credit_rating) = 8/14(-6/8log2(6/8)-2/8log2(2/8)) + 6/14(-


3/6log2(3/6)-3/6log2(3/6))
= 8/14(0.8112) + 6/14(1)

= 0.4635 + 0.4285 = 0.8920

Gain(credit_rating) = 0.94 – 0.8920 = 0.479

Since Age has the highest Information Gain we start splitting the
dataset using the age attribute.
Decision Tree after step 1

Since all records under the branch age31..40 are all of the class, Yes, we
can replace the leaf with Class=Yes

Decision Tree after step 1_1

Now build the decision tree for the left subtree

The same process of splitting has to happen for the two


remaining branches.

Left sub-branch

For branch age<=30 we still have attributes income, student, and


credit_rating. Which one should be used to split the partition?

The mutual information is E(Sage<=30)= E(2,3)= -2/5 log2(2/5) – 3/5


log2(3/5)=0.97
For Income, we have three values incomehigh (0 yes and 2 no),
incomemedium (1 yes and 1 no) and incomelow (1 yes and 0 no)
Entropy(income) = 2/5(0) + 2/5 (-1/2log2(1/2)-1/2log2(1/2)) + 1/5 (0) = 2/5
(1) = 0.4

Gain(income) = 0.97 – 0.4 = 0.57

For Student, we have two values studentyes (2 yes and 0 no) and
studentno (0 yes 3 no)
Entropy(student) = 2/5(0) + 3/5(0) = 0

Gain (student) = 0.97 – 0 = 0.97

We can then safely split on attribute student without checking the other
attributes since the information gain is maximized.

Decision Tree after step 2

Since these two new branches are from distinct classes, we make them
into leaf nodes with their respective class as label:
Decision Tree after step 2_2

Now build the decision tree for right left subtree

Right sub-branch

The mutual information is Entropy(Sage>40)= I(3,2)= -3/5 log2(3/5) – 2/5


log2(2/5)=0.97
For Income, we have two values incomemedium (2 yes and 1 no) and
incomelow (1 yes and 1 no)
Entropy(income) = 3/5(-2/3log2(2/3)-1/3log2(1/3)) + 2/5 (-1/2log2(1/2)-
1/2log2(1/2))

= 3/5(0.9182)+2/5 (1) = 0.55+0. 4= 0.95

Gain(income) = 0.97 – 0.95 = 0.02

For Student, we have two values studentyes (2 yes and 1 no) and
studentno (1 yes and 1 no)
Entropy(student) = 3/5(-2/3log2(2/3)-1/3log2(1/3)) + 2/5(-1/2log2(1/2)-
1/2log2(1/2)) = 0.95
Gain (student) = 0.97 – 0.95 = 0.02

For Credit_Rating, we have two values credit_ratingfair (3 yes and 0 no)


and credit_ratingexcellent (0 yes and 2 no)
Entropy(credit_rating) = 0

Gain(credit_rating) = 0.97 – 0 = 0.97


We then split based on credit_rating. These splits give partitions each with
records from the same class. We just need to make these into leaf nodes
with their class label attached:

Decision Tree for Buys Computer

New example: age<=30, income=medium, student=yes, credit-


rating=fair

Follow branch(age<=30) then student=yes we predict Class=yes

Buys_computer = yes

Common questions

Powered by AI

The ID3 algorithm uses mutual information to quantify the expected information gained by splitting a node on a particular attribute. Mutual information is the difference between the original entropy of the data set and the weighted entropy of partitions due to the split. While entropy measures the impurity of an arbitrary collection of examples, mutual information measures the reduction in entropy after the dataset has been partitioned according to the attribute. The attribute that results in the highest mutual information is selected for the split, as it contributes to the most significant reduction in uncertainty .

When a dataset includes a new categorical variable with more levels, the information gain calculation must adjust to consider these additional levels. The entropy calculation for each level involves computing the weighted average entropy across the different categories. As the new variable may change the overall distribution of the data, each level's entropy is multiplied by the likelihood of that level occurring in the dataset. This comprehensive treatment ensures that the gain reflects the reduction in disorder when data is partitioned by this variable, thereby properly influencing the decision tree's splits .

Leaf nodes in a decision tree represent the final decision or outcome based on the attributes along the path from the root to that leaf. After splitting based on high-information gain attributes, leaf nodes are significant because they constitute homogenous classes with minimal impurity. This homogeneity results from the careful selection of splits to maximize information gain, ensuring that the attributes used provide clearer distinctions between data classes, thus enhancing the classifier's predictive accuracy .

Attributes with higher information gain are preferred in the ID3 algorithm because they provide the most effective splits that lead to partitions with lower impurity or entropy. A higher information gain signifies a larger reduction in entropy, meaning the data subset is more homogenous with respect to the target variable. This leads to a simpler and more accurate tree by achieving clearer decision boundaries and reducing overfitting, which are crucial goals in constructing decision trees .

The new example with age <= 30, income = medium, student = yes, and credit-rating = fair will be classified as 'Yes' for the 'Buys Computer' dataset. According to the constructed decision tree, the path for age <= 30 leads to a further split on the 'Student' attribute. With student = yes, the decision node classifies the individual as a class 'Yes', indicating they will buy a computer .

The ID3 algorithm uses the information gain criterion to determine which attribute to use for splitting a dataset at each node of the decision tree. Information gain is calculated as the difference between the entropy of the dataset before the split and the weighted sum of the entropies of the partitions after the dataset is split on an attribute. The attribute with the highest information gain is selected for the split .

Challenges when using ID3 with continuous attributes include determining the optimal split points, as continuous data can take on an infinite number of values. To address this, continuous attributes can be discretized into a finite number of intervals or bins, which can then be evaluated for information gain. Alternatively, dynamic split points can be calculated at the time of tree construction by testing various split values and selecting the one that maximizes information gain. These approaches help convert the continuous data into manageable, categorical form for effective decision tree creation .

The 'Age' attribute was chosen as the first split in the decision tree because it provided the highest information gain compared to other attributes. The calculation of information gain for 'Age' was 0.2465, which was greater than the gains from other attributes such as 'Income' (0.0292), 'Student' (0.1516), and 'Credit_Rating' (0.048). The high information gain indicates that splitting the data based on 'Age' would result in the most significant reduction in entropy, thus making the dataset partitions more class-homogeneous .

The 'Student' attribute was selected for a split in the left subtree because it provided the maximum information gain among the remaining attributes. The gain for 'Student' was 0.97, which was higher than for other possible attributes like 'Income'. This high gain means that splitting on 'Student' would lead to the most significant decrease in entropy for that subsection of the data, thereby improving the predictability of the resulting branches .

Entropy is a measure of randomness or disorder in a dataset. In the ID3 algorithm, it is used to quantify the expected information needed to classify a set. Entropy is calculated for the entire dataset and each attribute's possible values. It helps determine the impurity reduction gained when dividing the data on an attribute. The attribute that results in the greatest entropy reduction, or highest information gain, is chosen to create the split in the decision tree .

You might also like