Lecture 3 - Decision Trees and Random Forest
Lecture 3 - Decision Trees and Random Forest
H Dip in DAB/ AI
CCT College Dublin
3
Decision Trees
• Decision Tree is collection of decision nodes, connected by branches, extending downward from
root node to terminating leaf nodes.
• Beginning with root node, attributes tested at decision nodes, and each possible outcome results
in branch.
• Each branch leads to decision node or leaf node.
• Example
• Predict whether customer is classified “Good” or “Bad” credit risk using
three predictor fields, according to data in Table.
• All records enter root node, and CART evaluates possible binary splits.
3. Choose the attribute test condition that produces the highest gain
Gain = P – M
or equivalently, lowest impurity measure after splitting (M)
9
Measures of Node Impurity
Gini Index
• Gini Index for a given node t:
P(C1) = 0/6 = 0 P(C2) = 6/6 = 1 P(C1) = 1/6 P(C2) = 5/6 P(C1) = 2/6 P(C2) = 4/6 P(C1) = 3/6 P(C2) = 3/6
Gini = 1 – P(C1)2 – P(C2)2 = 1 – 0 – 1 = 0 Gini = 1 – (1/6)2 – (5/6)2 = 0.278 Gini = 1 – (2/6)2 – (4/6)2 = 0.444 Gini = 1 – (3/6)2 – (3/6)2 = 0.5
10
CART Algorithm
Example
• Data Set
• There are 14 instances of golf playing
decisions based on outlook,
temperature, humidity and wind factors.
• Gini index
• Gini index is a metric for classification
tasks in CART. It stores sum of squared
probabilities of each class. We can
formulate it as illustrated below.
• Gini = 1 – Σ (Pi)2 for i = 1 to number of
classes https://dataaspirant.com/how-decision-tree-algorithm-works/ 11
12
https://dataaspirant.com/how-decision-tree-algorithm-works/
Splitting is occurred at minimum gini
index
• So what do you do to find a place that you will like? You can search online, read reviews on travel blogs and
portals, or you can also ask your friends.
• Let’s suppose you have decided to ask your friends, and talked with them about their past travel experience
to various places. You will get some recommendations from every friend. Now you have to make a list of
those recommended places. Then, you ask them to vote (or select one best place for the trip) from the list of
recommended places you made. The place with the highest number of votes will be your final choice for the
trip.
• In the above decision process, there are two parts. First, asking your friends about their individual travel
experience and getting one recommendation out of multiple places they have visited.
• This part is like using the decision tree algorithm. In this scenario, each friend makes a selection of the places
he or she has visited so far.
• The second part, after collecting all the recommendations, is the voting procedure for selecting the best place
in the list of recommendations. This whole process of getting recommendations from friends and voting on 18
How does the algorithm work?
• It works in four steps:
1. Select random samples from a given dataset.
2. Construct a decision tree for each sample and get a prediction
result from each decision tree.
3. Perform a vote for each predicted result.
4. Select the prediction result with the most votes as the final
prediction.
Random Forests vs Decision Trees
• Random forests is a set of multiple decision trees. Working diagram of random forest
• Deep decision trees may suffer from overfitting, but the random forests prevent overfitting by creating trees
on random subsets.
• Random forests is difficult to interpret, while a decision tree is easily interpretable and can be converted to19
Resources/ References
• Introduction to Machine Learning with Python, Andreas C. Müller and
Sarah Guido, O'Reilly Media, Inc. October 2016.
• Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow,
2nd Edition, Aurélien Géron, O'Reilly Media, September 2019, ISBN: Copyright Notice
9781492032649. The following material has been
communicated to you by or on behalf of
• Python Machine Learning - Third Edition, Sebastian Raschka, Vahid CCT College Dublin in accordance with the
Mirjalili, Copyright © 2017 Packt Publishing. Copyright and Related Rights Act 2000 (the
Act).
• Discovering Knowledge In Data: An Introduction To Data Exploration, The material may be subject to copyright
Second Edition, By Daniel Larose And Chantal Larose, John Wiley And under the Act and any further
Sons, Inc., 2014. reproduction, communication or
• UCI Repository: http://www.ics.uci.edu/~mlearn/MLRepository.html distribution of this material must be in
accordance with the Act.
• Understanding Autoencoders. (Part I) | by Jelaleddin Sultanov | AI³ |
Do not remove this notice
Theory, Practice, Business | Medium
• Statlib: http://lib.stat.cmu.edu
• Some images are used from Google search repository
(https://www.google.ie/search) to enhance the level of learning.
20