Decision Tree in Machine Learning
Decision Tree in Machine Learning
A decision tree in machine learning is a versatile, interpretable algorithm used for predictive
modelling. It structures decisions based on input data, making it suitable for both
classification and regression tasks. This article delves into the components, terminologies,
construction, and advantages of decision trees, exploring their applications and learning
algorithms.
There are specialized terms associated with decision trees that denote various components
and facets of the tree structure and decision-making procedure. :
Root Node: A decision tree’s root node, which represents the original choice or feature
from which the tree branches, is the highest node.
Internal Nodes (Decision Nodes): Nodes in the tree whose choices are determined by the
values of particular attributes. There are branches on these nodes that go to other nodes.
Leaf Nodes ( Terminal Nodes): The branches’ termini, when choices or forecasts are
decided upon. There are no more branches on leaf nodes.
Branches (Edges): Links between nodes that show how decisions are made in response to
particular circumstances.
Splitt ing: The process of dividing a node into two or more sub-nodes based on a decision
criterion. It involves selecting a feature and a threshold to create subsets of data.
https://www.geeksforgeeks.org/decision-tree-introduction-example/ 1/16
2/10/24, 1:27 AM Decision Tree in Machine Learning - GeeksforGeeks
Parent Node: A node that is split into child nodes. The original node from which a split
originates.
90%Child
RefundNode: Nodes
@Courses created
Machine as a Tutorial
Learning result of Data
a split fromTutorial
Analysis a parentPython
node.- Data visualization tutorial Nu
Decision Criterion: The rule or condition used to determine how the data should be split at
a decision node. It involves comparing feature values against a threshold.
Pruning: The process of removing branches or nodes from a decision tree to improve its
generalization and prevent overfitting.
Understanding these terminologies is crucial for interpreting and working with decision trees
in machine learning applications.
The process of forming a decision tree involves recursively partitioning the data based on the
values of different attributes. The algorithm selects the best attribute to split the data at each
internal node, based on certain criteria such as information gain or Gini impurity. This splitting
process continues until a stopping criterion is met, such as reaching a maximum depth or
having a minimum number of instances in a leaf node.
Decision trees are widely used in machine learning for a number of reasons:
https://www.geeksforgeeks.org/decision-tree-introduction-example/ 2/16
2/10/24, 1:27 AM Decision Tree in Machine Learning - GeeksforGeeks
They provide comprehensible insights into the decision logic, decision trees are especially
helpful for tasks involving categorization and regression.
They are proficient with both numerical and categorical data, and they can easily adapt to a
variety of datasets thanks to their autonomous feature selection capability.
Decision trees also provide simple visualization, which helps to comprehend and elucidate
the underlying decision processes in a model.
Decision tree uses the tree representation to solve the problem in which each leaf node
corresponds to a class label and attributes are represented on the internal node of the tree.
We can represent any boolean function on discrete attributes using the decision tree.
Below are some assumptions that we made while using the decision tree:
At the beginning, we consider the whole training set as the root.
Feature values are preferred to be categorical. If the values are continuous then they are
discretized prior to building the model.
On the basis of attribute values, records are distributed recursively.
We use statistical methods for ordering attributes as root or the internal node.
https://www.geeksforgeeks.org/decision-tree-introduction-example/ 3/16
2/10/24, 1:27 AM Decision Tree in Machine Learning - GeeksforGeeks
As you can see from the above image the Decision Tree works on the Sum of Product form
which is also known as Disjunctive Normal Form. In the above image, we are predicting the
use of computer in the daily life of people. In the Decision Tree, the major challenge is the
identification of the attribute for the root node at each level. This process is known as
attribute selection. We have two popular attribute selection measures:
1. Information Gain
2. Gini Index
When we use a node in a decision tree to partition the training instances into smaller subsets
the entropy changes. Information gain is a measure of this change in entropy.
https://www.geeksforgeeks.org/decision-tree-introduction-example/ 4/16
2/10/24, 1:27 AM Decision Tree in Machine Learning - GeeksforGeeks
Example:
Start with all training instances associated with the root node
Use info gain to choose which attribute to label each node with
Note: No root-to-leaf path should contain the same discrete attribute twice
Recursively construct each subtree on the subset of training instances that would be
classified down that path in the tree.
If all positive or all negative training instances remain, the label that node “yes” or “no”
accordingly
If no attributes remain, label with a majority vote of training instances left at that node
If no instances remain, label with a majority vote of the parent’s training instances.
Example: Now, let us draw a Decision Tree for the following data using Information gain.
Training set: 3 features and 2 classes
X Y Z C
1 1 1 I
1 1 0 I
0 0 1 II
https://www.geeksforgeeks.org/decision-tree-introduction-example/ 5/16
2/10/24, 1:27 AM Decision Tree in Machine Learning - GeeksforGeeks
X Y Z C
1 0 0 II
Here, we have 3 features and 2 output classes. To build a decision tree using Information gain.
We will take each of the features and calculate the information for each feature.
Split on feature X
Split on feature Y
https://www.geeksforgeeks.org/decision-tree-introduction-example/ 6/16
2/10/24, 1:27 AM Decision Tree in Machine Learning - GeeksforGeeks
Split on feature Z
From the above images, we can see that the information gain is maximum when we make a
split on feature Y. So, for the root node best-suited feature is feature Y. Now we can see that
while splitting the dataset by feature Y, the child contains a pure subset of the target variable.
So we don’t need to further split the dataset. The final tree for the above dataset would look
like this:
2. Gini Index
Gini Index is a metric to measure how often a randomly chosen element would be
incorrectly identified.
It means an attribute with a lower Gini index should be preferred.
Sklearn supports “Gini” criteria for Gini Index and by default, it takes “gini” value.
The Formula for the calculation of the Gini Index is given below.
https://www.geeksforgeeks.org/decision-tree-introduction-example/ 7/16
2/10/24, 1:27 AM Decision Tree in Machine Learning - GeeksforGeeks
The Gini Index is a measure of the inequality or impurity of a distribution, commonly used in
decision trees and other machine learning algorithms. It ranges from 0 to 1, where 0
represents perfect equality (all values are the same) and 1 represents perfect inequality (all
values are different ).
Forecast ing Act ivit ies Using Weat her Informat ion
Determine the best attribute to split the dataset based on information gain, which is
calculated by the formula: Information gain = Entropy(parent ) – [Weighted average] *
Entropy(children), where entropy is a measure of impurity or disorder of a set of examples,
and the weighted average is based on the number of examples in each child node.
Create a new internal node that corresponds to the best attribute and connects it to the
root node. For example, if the best attribute is “outlook” (which can have values “sunny”,
https://www.geeksforgeeks.org/decision-tree-introduction-example/ 8/16
2/10/24, 1:27 AM Decision Tree in Machine Learning - GeeksforGeeks
“overcast”, or “rainy”), we create a new node labeled “outlook” and connect it to the root
node.
Partition the dataset into subsets based on the values of the best attribute. For example,
we create three subsets: one for instances where the outlook is “sunny”, one for instances
where the outlook is “overcast”, and one for instances where the outlook is “rainy”.
Recursively repeat steps 1-4 for each subset until all instances in a given subset belong to
the same class or no further splitting is possible. For example, if the subset of instances
where the outlook is “overcast” contains only instances where the activity is “hiking”, we
assign a leaf node labeled “hiking” to this subset. If the subset of instances where the
outlook is “sunny” is further split based on the humidity attribute, we repeat steps 2-4 for
this subset.
Assign a leaf node to each subset that contains instances that belong to the same class.
For example, if the subset of instances where the outlook is “rainy” contains only instances
where the activity is “stay inside”, we assign a leaf node labeled “stay inside” to this subset.
Make predictions based on the decision tree by traversing it from the root node to a leaf
node that corresponds to the instance being classified. For example, if the outlook is
“sunny” and the humidity is “high”, we traverse the decision tree by following the “sunny”
branch and then the “high humidity” branch, and we end up at a leaf node labeled
“swimming”, which is our predicted activity.
Conclusion
https://www.geeksforgeeks.org/decision-tree-introduction-example/ 9/16
2/10/24, 1:27 AM Decision Tree in Machine Learning - GeeksforGeeks
Decision trees, a key tool in machine learning, model and predict outcomes based on input
data through a tree-like structure. They offer interpretability, versatility, and simple
visualization, making them valuable for both categorization and regression tasks. While
decision trees have advantages like ease of understanding, they may face challenges such as
overfitting. Understanding their terminologies and formation process is essential for effective
application in diverse scenarios.
Major issues in decision tree learning include overfitting, sensitivity to small data
changes, and limited generalization. Ensuring proper pruning, tuning, and handling
imbalanced data can help mitigate these challenges for more robust decision tree
models.
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new
heights by tapping into the power of data. Sharpen your skills and become a part of the
hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science
Program by GeeksforGeeks and stay ahead of the curve.
Three 90 Challenge ending on 29th Feb! Last chance to get 90% refund by completing 90%
course in 90 days. Explore offer now.
Previous Next
https://www.geeksforgeeks.org/decision-tree-introduction-example/ 11/16
2/10/24, 1:27 AM Decision Tree in Machine Learning - GeeksforGeeks
Similar Reads
How to Extract the Decision Rules from scikit- Decision Threshold In Machine Learning
learn Decision-tree?
Support vector machine in Machine Learning Azure Virtual Machine for Machine Learning
Need of Data Structures and Algorithms for Machine Learning - Learning VS Designing
Deep Learning and Machine Learning
Passive and Active learning in Machine Automated Machine Learning for Supervised
Learning Learning using R
Complete Tutorials
Python Crash Course Python API Tutorial: Getting Started with APIs
A Abhishek …
https://www.geeksforgeeks.org/decision-tree-introduction-example/ 12/16
2/10/24, 1:27 AM Decision Tree in Machine Learning - GeeksforGeeks
Additional Information
Company Explore
About Us Job-A-Thon Hiring Challenge
Legal Hack-A-Thon
Careers GfG Weekly Contest
In Media Offline Classes (Delhi/NCR)
Contact Us DSA in JAVA/C++
Advertise with us Master System Design
GFG Corporate Solution Master CP
Placement Training Program GeeksforGeeks Videos
Apply for Mentor Geeks Community
https://www.geeksforgeeks.org/decision-tree-introduction-example/ 13/16
2/10/24, 1:27 AM Decision Tree in Machine Learning - GeeksforGeeks
Languages DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
SQL Top 100 DSA Interview Problems
R Language DSA Roadmap by Sandeep Jain
Android Tutorial All Cheat Sheets
Tutorials Archive
https://www.geeksforgeeks.org/decision-tree-introduction-example/ 14/16
2/10/24, 1:27 AM Decision Tree in Machine Learning - GeeksforGeeks
https://www.geeksforgeeks.org/decision-tree-introduction-example/ 15/16
2/10/24, 1:27 AM Decision Tree in Machine Learning - GeeksforGeeks
https://www.geeksforgeeks.org/decision-tree-introduction-example/ 16/16