SlideShare a Scribd company logo
1 of 44
Download to read offline
Understanding Random Forests
Marc Garcia
PyData Madrid - April 9th, 2016
1 / 44
Understanding Random Forests - Marc Garcia
Introduction
2 / 44
Understanding Random Forests - Marc Garcia
Introduction
About me: @datapythonista
Python programmer since 2006
Master in AI from UPC
Working for Bank of America
http://datapythonista.github.io
3 / 44
Understanding Random Forests - Marc Garcia
Introduction
Overview: Understanding Random Forests
How decision trees work
Training Random Forests
Practical example
Analyzing model parameters
4 / 44
Understanding Random Forests - Marc Garcia
How decision trees work
5 / 44
Understanding Random Forests - Marc Garcia
How decision trees work
Using a decision tree
Simple example:
Event attendance: binary classification
2 explanatory variables: age and distance
from sklearn.tree import DecisionTreeClassifier
dtree = DecisionTreeClassifier()
dtree.fit(df[[’age’, ’distance’]], df[’attended’])
cart_plot(dtree)
6 / 44
Understanding Random Forests - Marc Garcia
How decision trees work
Data visualization
7 / 44
Understanding Random Forests - Marc Garcia
How decision trees work
Decision boundary visualization
8 / 44
Understanding Random Forests - Marc Garcia
How decision trees work
Decision boundary visualization
9 / 44
Understanding Random Forests - Marc Garcia
How decision trees work
Decision boundary visualization
10 / 44
Understanding Random Forests - Marc Garcia
How decision trees work
Decision boundary visualization
11 / 44
Understanding Random Forests - Marc Garcia
How decision trees work
Decision boundary visualization
12 / 44
Understanding Random Forests - Marc Garcia
How decision trees work
Decision boundary visualization
13 / 44
Understanding Random Forests - Marc Garcia
How decision trees work
How is the model?
def decision_tree_model(age, distance):
if distance >= 2283.11:
if age >= 40.00:
if distance >= 6868.86:
if distance >= 8278.82:
return True
else:
return False
else:
return True
else:
return False
else:
if age >= 54.50:
if age >= 57.00:
return True
else:
return False
else:
return True
14 / 44
Understanding Random Forests - Marc Garcia
How decision trees work
Training: Basic algorithm
def train_decision_tree(x, y):
feature, value = get_best_split(x, y)
x_left, y_left = x[x[feature] < value], y[x[feature] < value]
if len(y_left.unique()) > 1:
left_node = train_decision_tree(x_left, y_left)
else:
left_node = None
x_right, y_right = x[x[feature] >= value], y[x[feature] >= value]
if len(y_right.unique()) > 1:
right_node = train_decision_tree(x_right, y_right)
else:
right_node = None
return Node(feature, value, left_node, right_node)
15 / 44
Understanding Random Forests - Marc Garcia
How decision trees work
Best split
Candidate split 1
age 18 19 21 27 29 34 38 42 49 54 62 64
attended F F T F T T F T F T T T
Split True False
Left 0 1
Right 7 4
Candidate split 2
age 18 19 21 27 29 34 38 42 49 54 62 64
attended F F T F T T F T F T T T
Split True False
Left 0 2
Right 7 3
16 / 44
Understanding Random Forests - Marc Garcia
How decision trees work
Best split algorithm
def get_best_split(x, y):
best_split = None
best_entropy = 1.
for feature in x.columns.values:
column = x[feature]
for value in column.iterrows():
a = y[column < value] == class_a_value
b = y[column < value] == class_b_value
left_weight = (a + b) / len(y.index)
left_entropy = entropy(a, b)
a = y[column >= value] == class_a_value
b = y[column >= value] == class_b_value
right_items = (a + b) / len(y.index)
right_entropy = entropy(a, b)
split_entropy = left_weight * left_etropy + right_weight * right_entropy
if split_entropy < best_entropy:
best_split = (feature, value)
best_entropy = split_entropy
return best_split
17 / 44
Understanding Random Forests - Marc Garcia
How decision trees work
Entropy
For a given subset1:
entropy = −Prattending · log2 Prattending − Pr¬attending · log2 Pr¬attending (1)
1
Note that for pure splits it’s assumed that 0 · log2 0 = 0
18 / 44
Understanding Random Forests - Marc Garcia
How decision trees work
Entropy algorithm
import math
def entropy(a, b):
total = a + b
prob_a = a / total
prob_b = b / total
return - prob_a * math.log(prob_a, 2) 
- prob_b * math.log(prob_b, 2)
19 / 44
Understanding Random Forests - Marc Garcia
How decision trees work
Information gain
For a given split:
information_gain = entropyparent −
itemsleft
itemstotal
· entropyleft +
itemsright
itemstotal
· entropyright (2)
20 / 44
Understanding Random Forests - Marc Garcia
Practical example
21 / 44
Understanding Random Forests - Marc Garcia
Practical example
Etsy dataset
Features:
Feedback (number of reviews)
Age (days since shop creation)
Admirers (likes)
Items (number of products)
Response variable:
Sales (number)
Samples:
58,092
Source:
www.bigml.com
22 / 44
Understanding Random Forests - Marc Garcia
Practical example
Features distribution
23 / 44
Understanding Random Forests - Marc Garcia
Practical example
Sales visualization
24 / 44
Understanding Random Forests - Marc Garcia
Practical example
Feature correlation
sales admirers age feedback items
sales 1.000000 0.458261 0.181045 0.955949 0.502423
admirers 0.458261 1.000000 0.340939 0.401995 0.268985
age 0.181045 0.340939 1.000000 0.184238 0.167535
feedback 0.955949 0.401995 0.184238 1.000000 0.458955
items 0.502423 0.268985 0.167535 0.458955 1.000000
25 / 44
Understanding Random Forests - Marc Garcia
Practical example
Correlation to sales
26 / 44
Understanding Random Forests - Marc Garcia
Practical example
Decision tree visualization
27 / 44
Understanding Random Forests - Marc Garcia
Practical example
Decision tree visualization (top)
28 / 44
Understanding Random Forests - Marc Garcia
Practical example
Decision tree visualization (left)
29 / 44
Understanding Random Forests - Marc Garcia
Practical example
Decision tree visualization (right)
30 / 44
Understanding Random Forests - Marc Garcia
Practical example
What is wrong with the tree?
Only one variable used to predict in some cases
Lack of stability:
If feedback = 207 we predict 109 sales
If feedback = 208 we predict 977 sales
Our model can change dramatically if we add few more samples to the dataset
Overfitting: We make predictions based on a single sample
31 / 44
Understanding Random Forests - Marc Garcia
Training Random Forests
32 / 44
Understanding Random Forests - Marc Garcia
Training Random Forests
Ensembles: The board of directors
Why companies have a BoD?
What is best?
The best point of view
A mix of good points of view
How was our best tree?
What about a mix of not so good trees?
33 / 44
Understanding Random Forests - Marc Garcia
Training Random Forests
Ensemble of trees
How do we build optimized trees, that are different?
We need to randomize them
Samples: bagging
Features
Splits
34 / 44
Understanding Random Forests - Marc Garcia
Training Random Forests
Bagging: Uniform sampling with replacement
35 / 44
Understanding Random Forests - Marc Garcia
Training Random Forests
Randomizing features
Even with bagging, trees will usually make the top splits by the same feature
max_features parameter:
Regression: Use all
Classification: Use the squared root
36 / 44
Understanding Random Forests - Marc Garcia
Training Random Forests
Randomizing splits
A split per sample is considered in the original decision tree
We can consider a subset
We obtain randomized trees
Performance is improved
sklearn implements ExtraTreeRegressor and ExtraTreeClassifier
37 / 44
Understanding Random Forests - Marc Garcia
Training Random Forests
What happened with our example problems
Only one variable used to predict in some cases
We can avoid it by not using all features
Lack of stability:
Our model is smoother
Overfitting: We make predictions based on a single sample
Overfitting is mostly avoided
Random forests come with built-in cross validation
38 / 44
Understanding Random Forests - Marc Garcia
Training Random Forests
Smoothing
39 / 44
Understanding Random Forests - Marc Garcia
Training Random Forests
Parameter optimization (Etsy)
40 / 44
Understanding Random Forests - Marc Garcia
Training Random Forests
Parameter optimization (Bank marketing)
Data source: [Moro et al., 2014] S. Moro, P. Cortez and P. Rita.
A Data-Driven Approach to Predict the Success of Bank Telemarketing. Decision Support Systems, Elsevier, 62:22-31, June 2014
41 / 44
Understanding Random Forests - Marc Garcia
Training Random Forests
When should we use Random Forests?
Using a CART is usually good as part of exploratory analysis
Do not use CART or RF when the problem is linear
They are specially good for ordinal (categorical but sorted) variables
Performance
CART is slower than linear models (e.g. logit)
RF trains N trees, and takes N times but it works in multiple cores, seriously
But RF is usually much faster than ANN or SVM, and results can be similar
42 / 44
Understanding Random Forests - Marc Garcia
Training Random Forests
Summary
CART is a simple, but still powerful model
Visualizing them we can better understand our data
Ensembles usually improve the predictive power of models
Random Forests fix CART problems
Better use of features
More stability
Better generalization (RFs avoid overfitting)
Random Forests main parameters
min_samples_leaf (inherited from CART)
num_estimators
43 / 44
Understanding Random Forests - Marc Garcia
Training Random Forests
Thank you
QUESTIONS?
44 / 44
Understanding Random Forests - Marc Garcia

More Related Content

What's hot

Decision Tree - C4.5&CART
Decision Tree - C4.5&CARTDecision Tree - C4.5&CART
Decision Tree - C4.5&CART
Xueping Peng
 
Machine Learning 3 - Decision Tree Learning
Machine Learning 3 - Decision Tree LearningMachine Learning 3 - Decision Tree Learning
Machine Learning 3 - Decision Tree Learning
butest
 

What's hot (20)

Introduction to Random Forest
Introduction to Random Forest Introduction to Random Forest
Introduction to Random Forest
 
Decision trees for machine learning
Decision trees for machine learningDecision trees for machine learning
Decision trees for machine learning
 
Decision tree
Decision treeDecision tree
Decision tree
 
Data Science - Part V - Decision Trees & Random Forests
Data Science - Part V - Decision Trees & Random Forests Data Science - Part V - Decision Trees & Random Forests
Data Science - Part V - Decision Trees & Random Forests
 
Decision Tree Learning
Decision Tree LearningDecision Tree Learning
Decision Tree Learning
 
Decision tree
Decision treeDecision tree
Decision tree
 
Decision tree
Decision treeDecision tree
Decision tree
 
Decision trees & random forests
Decision trees & random forestsDecision trees & random forests
Decision trees & random forests
 
Random forest and decision tree
Random forest and decision treeRandom forest and decision tree
Random forest and decision tree
 
Decision Tree - C4.5&CART
Decision Tree - C4.5&CARTDecision Tree - C4.5&CART
Decision Tree - C4.5&CART
 
Decision tree
Decision treeDecision tree
Decision tree
 
Random forest sgv_ai_talk_oct_2_2018
Random forest sgv_ai_talk_oct_2_2018Random forest sgv_ai_talk_oct_2_2018
Random forest sgv_ai_talk_oct_2_2018
 
Decision Tree Algorithm | Decision Tree in Python | Machine Learning Algorith...
Decision Tree Algorithm | Decision Tree in Python | Machine Learning Algorith...Decision Tree Algorithm | Decision Tree in Python | Machine Learning Algorith...
Decision Tree Algorithm | Decision Tree in Python | Machine Learning Algorith...
 
Classification decision tree
Classification  decision treeClassification  decision tree
Classification decision tree
 
Random Forest Tutorial | Random Forest in R | Machine Learning | Data Science...
Random Forest Tutorial | Random Forest in R | Machine Learning | Data Science...Random Forest Tutorial | Random Forest in R | Machine Learning | Data Science...
Random Forest Tutorial | Random Forest in R | Machine Learning | Data Science...
 
Decision trees in Machine Learning
Decision trees in Machine Learning Decision trees in Machine Learning
Decision trees in Machine Learning
 
Machine Learning 3 - Decision Tree Learning
Machine Learning 3 - Decision Tree LearningMachine Learning 3 - Decision Tree Learning
Machine Learning 3 - Decision Tree Learning
 
Introduction to Random Forests by Dr. Adele Cutler
Introduction to Random Forests by Dr. Adele CutlerIntroduction to Random Forests by Dr. Adele Cutler
Introduction to Random Forests by Dr. Adele Cutler
 
Random forest
Random forestRandom forest
Random forest
 
12. Random Forest
12. Random Forest12. Random Forest
12. Random Forest
 

Similar to Understanding random forests

CSA 3702 machine learning module 2
CSA 3702 machine learning module 2CSA 3702 machine learning module 2
CSA 3702 machine learning module 2
Nandhini S
 
Machine Learning Unit-5 Decesion Trees & Random Forest.pdf
Machine Learning Unit-5 Decesion Trees & Random Forest.pdfMachine Learning Unit-5 Decesion Trees & Random Forest.pdf
Machine Learning Unit-5 Decesion Trees & Random Forest.pdf
AdityaSoraut
 
모듈형 패키지를 활용한 나만의 기계학습 모형 만들기 - 회귀나무모형을 중심으로
모듈형 패키지를 활용한 나만의 기계학습 모형 만들기 - 회귀나무모형을 중심으로 모듈형 패키지를 활용한 나만의 기계학습 모형 만들기 - 회귀나무모형을 중심으로
모듈형 패키지를 활용한 나만의 기계학습 모형 만들기 - 회귀나무모형을 중심으로
r-kor
 
Module III - Classification Decision tree (1).pptx
Module III - Classification Decision tree (1).pptxModule III - Classification Decision tree (1).pptx
Module III - Classification Decision tree (1).pptx
Shivakrishnan18
 
Classification (ML).ppt
Classification (ML).pptClassification (ML).ppt
Classification (ML).ppt
rajasamal1999
 

Similar to Understanding random forests (20)

CART: Not only Classification and Regression Trees
CART: Not only Classification and Regression TreesCART: Not only Classification and Regression Trees
CART: Not only Classification and Regression Trees
 
Comparison Study of Decision Tree Ensembles for Regression
Comparison Study of Decision Tree Ensembles for RegressionComparison Study of Decision Tree Ensembles for Regression
Comparison Study of Decision Tree Ensembles for Regression
 
Decision tree
Decision tree Decision tree
Decision tree
 
5.Module_AIML Random Forest.pptx
5.Module_AIML Random Forest.pptx5.Module_AIML Random Forest.pptx
5.Module_AIML Random Forest.pptx
 
Decision Trees
Decision TreesDecision Trees
Decision Trees
 
CSA 3702 machine learning module 2
CSA 3702 machine learning module 2CSA 3702 machine learning module 2
CSA 3702 machine learning module 2
 
Machine Learning Unit-5 Decesion Trees & Random Forest.pdf
Machine Learning Unit-5 Decesion Trees & Random Forest.pdfMachine Learning Unit-5 Decesion Trees & Random Forest.pdf
Machine Learning Unit-5 Decesion Trees & Random Forest.pdf
 
Lecture4.pptx
Lecture4.pptxLecture4.pptx
Lecture4.pptx
 
모듈형 패키지를 활용한 나만의 기계학습 모형 만들기 - 회귀나무모형을 중심으로
모듈형 패키지를 활용한 나만의 기계학습 모형 만들기 - 회귀나무모형을 중심으로 모듈형 패키지를 활용한 나만의 기계학습 모형 만들기 - 회귀나무모형을 중심으로
모듈형 패키지를 활용한 나만의 기계학습 모형 만들기 - 회귀나무모형을 중심으로
 
Using Tree algorithms on machine learning
Using Tree algorithms on machine learningUsing Tree algorithms on machine learning
Using Tree algorithms on machine learning
 
02.03 Artificial Intelligence: Search by Optimization
02.03 Artificial Intelligence: Search by Optimization02.03 Artificial Intelligence: Search by Optimization
02.03 Artificial Intelligence: Search by Optimization
 
Random Forests: The Vanilla of Machine Learning - Anna Quach
Random Forests: The Vanilla of Machine Learning - Anna QuachRandom Forests: The Vanilla of Machine Learning - Anna Quach
Random Forests: The Vanilla of Machine Learning - Anna Quach
 
13 random forest
13 random forest13 random forest
13 random forest
 
What Is Random Forest_ analytics_ IBM.pdf
What Is Random Forest_ analytics_ IBM.pdfWhat Is Random Forest_ analytics_ IBM.pdf
What Is Random Forest_ analytics_ IBM.pdf
 
Decision Trees.ppt
Decision Trees.pptDecision Trees.ppt
Decision Trees.ppt
 
Module III - Classification Decision tree (1).pptx
Module III - Classification Decision tree (1).pptxModule III - Classification Decision tree (1).pptx
Module III - Classification Decision tree (1).pptx
 
ML unit-1.pptx
ML unit-1.pptxML unit-1.pptx
ML unit-1.pptx
 
2023 Supervised Learning for Orange3 from scratch
2023 Supervised Learning for Orange3 from scratch2023 Supervised Learning for Orange3 from scratch
2023 Supervised Learning for Orange3 from scratch
 
Classification (ML).ppt
Classification (ML).pptClassification (ML).ppt
Classification (ML).ppt
 
Strata 2013: Tutorial-- How to Create Predictive Models in R using Ensembles
Strata 2013: Tutorial-- How to Create Predictive Models in R using EnsemblesStrata 2013: Tutorial-- How to Create Predictive Models in R using Ensembles
Strata 2013: Tutorial-- How to Create Predictive Models in R using Ensembles
 

Recently uploaded

In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi ArabiaIn Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
ahmedjiabur940
 
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
gajnagarg
 
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
nirzagarg
 
Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...
gajnagarg
 
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get CytotecAbortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Riyadh +966572737505 get cytotec
 
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
gajnagarg
 
Call Girls in G.T.B. Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in G.T.B. Nagar  (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in G.T.B. Nagar  (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in G.T.B. Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
nirzagarg
 
Belur $ Female Escorts Service in Kolkata (Adult Only) 8005736733 Escort Serv...
Belur $ Female Escorts Service in Kolkata (Adult Only) 8005736733 Escort Serv...Belur $ Female Escorts Service in Kolkata (Adult Only) 8005736733 Escort Serv...
Belur $ Female Escorts Service in Kolkata (Adult Only) 8005736733 Escort Serv...
HyderabadDolls
 
Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...
Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...
Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...
HyderabadDolls
 

Recently uploaded (20)

In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi ArabiaIn Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
 
Digital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareDigital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham Ware
 
👉 Bhilai Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class Call Girl Ser...
👉 Bhilai Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class Call Girl Ser...👉 Bhilai Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class Call Girl Ser...
👉 Bhilai Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class Call Girl Ser...
 
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
 
Gomti Nagar & best call girls in Lucknow | 9548273370 Independent Escorts & D...
Gomti Nagar & best call girls in Lucknow | 9548273370 Independent Escorts & D...Gomti Nagar & best call girls in Lucknow | 9548273370 Independent Escorts & D...
Gomti Nagar & best call girls in Lucknow | 9548273370 Independent Escorts & D...
 
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
 
Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...
 
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get CytotecAbortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get Cytotec
 
Ranking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRanking and Scoring Exercises for Research
Ranking and Scoring Exercises for Research
 
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
 
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptxRESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
 
Call Girls in G.T.B. Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in G.T.B. Nagar  (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in G.T.B. Nagar  (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in G.T.B. Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Introduction to Statistics Presentation.pptx
Introduction to Statistics Presentation.pptxIntroduction to Statistics Presentation.pptx
Introduction to Statistics Presentation.pptx
 
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
 
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
 
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...Identify Customer Segments to Create Customer Offers for Each Segment - Appli...
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...
 
Top Call Girls in Balaghat 9332606886Call Girls Advance Cash On Delivery Ser...
Top Call Girls in Balaghat  9332606886Call Girls Advance Cash On Delivery Ser...Top Call Girls in Balaghat  9332606886Call Girls Advance Cash On Delivery Ser...
Top Call Girls in Balaghat 9332606886Call Girls Advance Cash On Delivery Ser...
 
社内勉強会資料_Object Recognition as Next Token Prediction
社内勉強会資料_Object Recognition as Next Token Prediction社内勉強会資料_Object Recognition as Next Token Prediction
社内勉強会資料_Object Recognition as Next Token Prediction
 
Belur $ Female Escorts Service in Kolkata (Adult Only) 8005736733 Escort Serv...
Belur $ Female Escorts Service in Kolkata (Adult Only) 8005736733 Escort Serv...Belur $ Female Escorts Service in Kolkata (Adult Only) 8005736733 Escort Serv...
Belur $ Female Escorts Service in Kolkata (Adult Only) 8005736733 Escort Serv...
 
Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...
Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...
Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...
 

Understanding random forests

  • 1. Understanding Random Forests Marc Garcia PyData Madrid - April 9th, 2016 1 / 44 Understanding Random Forests - Marc Garcia
  • 2. Introduction 2 / 44 Understanding Random Forests - Marc Garcia
  • 3. Introduction About me: @datapythonista Python programmer since 2006 Master in AI from UPC Working for Bank of America http://datapythonista.github.io 3 / 44 Understanding Random Forests - Marc Garcia
  • 4. Introduction Overview: Understanding Random Forests How decision trees work Training Random Forests Practical example Analyzing model parameters 4 / 44 Understanding Random Forests - Marc Garcia
  • 5. How decision trees work 5 / 44 Understanding Random Forests - Marc Garcia
  • 6. How decision trees work Using a decision tree Simple example: Event attendance: binary classification 2 explanatory variables: age and distance from sklearn.tree import DecisionTreeClassifier dtree = DecisionTreeClassifier() dtree.fit(df[[’age’, ’distance’]], df[’attended’]) cart_plot(dtree) 6 / 44 Understanding Random Forests - Marc Garcia
  • 7. How decision trees work Data visualization 7 / 44 Understanding Random Forests - Marc Garcia
  • 8. How decision trees work Decision boundary visualization 8 / 44 Understanding Random Forests - Marc Garcia
  • 9. How decision trees work Decision boundary visualization 9 / 44 Understanding Random Forests - Marc Garcia
  • 10. How decision trees work Decision boundary visualization 10 / 44 Understanding Random Forests - Marc Garcia
  • 11. How decision trees work Decision boundary visualization 11 / 44 Understanding Random Forests - Marc Garcia
  • 12. How decision trees work Decision boundary visualization 12 / 44 Understanding Random Forests - Marc Garcia
  • 13. How decision trees work Decision boundary visualization 13 / 44 Understanding Random Forests - Marc Garcia
  • 14. How decision trees work How is the model? def decision_tree_model(age, distance): if distance >= 2283.11: if age >= 40.00: if distance >= 6868.86: if distance >= 8278.82: return True else: return False else: return True else: return False else: if age >= 54.50: if age >= 57.00: return True else: return False else: return True 14 / 44 Understanding Random Forests - Marc Garcia
  • 15. How decision trees work Training: Basic algorithm def train_decision_tree(x, y): feature, value = get_best_split(x, y) x_left, y_left = x[x[feature] < value], y[x[feature] < value] if len(y_left.unique()) > 1: left_node = train_decision_tree(x_left, y_left) else: left_node = None x_right, y_right = x[x[feature] >= value], y[x[feature] >= value] if len(y_right.unique()) > 1: right_node = train_decision_tree(x_right, y_right) else: right_node = None return Node(feature, value, left_node, right_node) 15 / 44 Understanding Random Forests - Marc Garcia
  • 16. How decision trees work Best split Candidate split 1 age 18 19 21 27 29 34 38 42 49 54 62 64 attended F F T F T T F T F T T T Split True False Left 0 1 Right 7 4 Candidate split 2 age 18 19 21 27 29 34 38 42 49 54 62 64 attended F F T F T T F T F T T T Split True False Left 0 2 Right 7 3 16 / 44 Understanding Random Forests - Marc Garcia
  • 17. How decision trees work Best split algorithm def get_best_split(x, y): best_split = None best_entropy = 1. for feature in x.columns.values: column = x[feature] for value in column.iterrows(): a = y[column < value] == class_a_value b = y[column < value] == class_b_value left_weight = (a + b) / len(y.index) left_entropy = entropy(a, b) a = y[column >= value] == class_a_value b = y[column >= value] == class_b_value right_items = (a + b) / len(y.index) right_entropy = entropy(a, b) split_entropy = left_weight * left_etropy + right_weight * right_entropy if split_entropy < best_entropy: best_split = (feature, value) best_entropy = split_entropy return best_split 17 / 44 Understanding Random Forests - Marc Garcia
  • 18. How decision trees work Entropy For a given subset1: entropy = −Prattending · log2 Prattending − Pr¬attending · log2 Pr¬attending (1) 1 Note that for pure splits it’s assumed that 0 · log2 0 = 0 18 / 44 Understanding Random Forests - Marc Garcia
  • 19. How decision trees work Entropy algorithm import math def entropy(a, b): total = a + b prob_a = a / total prob_b = b / total return - prob_a * math.log(prob_a, 2) - prob_b * math.log(prob_b, 2) 19 / 44 Understanding Random Forests - Marc Garcia
  • 20. How decision trees work Information gain For a given split: information_gain = entropyparent − itemsleft itemstotal · entropyleft + itemsright itemstotal · entropyright (2) 20 / 44 Understanding Random Forests - Marc Garcia
  • 21. Practical example 21 / 44 Understanding Random Forests - Marc Garcia
  • 22. Practical example Etsy dataset Features: Feedback (number of reviews) Age (days since shop creation) Admirers (likes) Items (number of products) Response variable: Sales (number) Samples: 58,092 Source: www.bigml.com 22 / 44 Understanding Random Forests - Marc Garcia
  • 23. Practical example Features distribution 23 / 44 Understanding Random Forests - Marc Garcia
  • 24. Practical example Sales visualization 24 / 44 Understanding Random Forests - Marc Garcia
  • 25. Practical example Feature correlation sales admirers age feedback items sales 1.000000 0.458261 0.181045 0.955949 0.502423 admirers 0.458261 1.000000 0.340939 0.401995 0.268985 age 0.181045 0.340939 1.000000 0.184238 0.167535 feedback 0.955949 0.401995 0.184238 1.000000 0.458955 items 0.502423 0.268985 0.167535 0.458955 1.000000 25 / 44 Understanding Random Forests - Marc Garcia
  • 26. Practical example Correlation to sales 26 / 44 Understanding Random Forests - Marc Garcia
  • 27. Practical example Decision tree visualization 27 / 44 Understanding Random Forests - Marc Garcia
  • 28. Practical example Decision tree visualization (top) 28 / 44 Understanding Random Forests - Marc Garcia
  • 29. Practical example Decision tree visualization (left) 29 / 44 Understanding Random Forests - Marc Garcia
  • 30. Practical example Decision tree visualization (right) 30 / 44 Understanding Random Forests - Marc Garcia
  • 31. Practical example What is wrong with the tree? Only one variable used to predict in some cases Lack of stability: If feedback = 207 we predict 109 sales If feedback = 208 we predict 977 sales Our model can change dramatically if we add few more samples to the dataset Overfitting: We make predictions based on a single sample 31 / 44 Understanding Random Forests - Marc Garcia
  • 32. Training Random Forests 32 / 44 Understanding Random Forests - Marc Garcia
  • 33. Training Random Forests Ensembles: The board of directors Why companies have a BoD? What is best? The best point of view A mix of good points of view How was our best tree? What about a mix of not so good trees? 33 / 44 Understanding Random Forests - Marc Garcia
  • 34. Training Random Forests Ensemble of trees How do we build optimized trees, that are different? We need to randomize them Samples: bagging Features Splits 34 / 44 Understanding Random Forests - Marc Garcia
  • 35. Training Random Forests Bagging: Uniform sampling with replacement 35 / 44 Understanding Random Forests - Marc Garcia
  • 36. Training Random Forests Randomizing features Even with bagging, trees will usually make the top splits by the same feature max_features parameter: Regression: Use all Classification: Use the squared root 36 / 44 Understanding Random Forests - Marc Garcia
  • 37. Training Random Forests Randomizing splits A split per sample is considered in the original decision tree We can consider a subset We obtain randomized trees Performance is improved sklearn implements ExtraTreeRegressor and ExtraTreeClassifier 37 / 44 Understanding Random Forests - Marc Garcia
  • 38. Training Random Forests What happened with our example problems Only one variable used to predict in some cases We can avoid it by not using all features Lack of stability: Our model is smoother Overfitting: We make predictions based on a single sample Overfitting is mostly avoided Random forests come with built-in cross validation 38 / 44 Understanding Random Forests - Marc Garcia
  • 39. Training Random Forests Smoothing 39 / 44 Understanding Random Forests - Marc Garcia
  • 40. Training Random Forests Parameter optimization (Etsy) 40 / 44 Understanding Random Forests - Marc Garcia
  • 41. Training Random Forests Parameter optimization (Bank marketing) Data source: [Moro et al., 2014] S. Moro, P. Cortez and P. Rita. A Data-Driven Approach to Predict the Success of Bank Telemarketing. Decision Support Systems, Elsevier, 62:22-31, June 2014 41 / 44 Understanding Random Forests - Marc Garcia
  • 42. Training Random Forests When should we use Random Forests? Using a CART is usually good as part of exploratory analysis Do not use CART or RF when the problem is linear They are specially good for ordinal (categorical but sorted) variables Performance CART is slower than linear models (e.g. logit) RF trains N trees, and takes N times but it works in multiple cores, seriously But RF is usually much faster than ANN or SVM, and results can be similar 42 / 44 Understanding Random Forests - Marc Garcia
  • 43. Training Random Forests Summary CART is a simple, but still powerful model Visualizing them we can better understand our data Ensembles usually improve the predictive power of models Random Forests fix CART problems Better use of features More stability Better generalization (RFs avoid overfitting) Random Forests main parameters min_samples_leaf (inherited from CART) num_estimators 43 / 44 Understanding Random Forests - Marc Garcia
  • 44. Training Random Forests Thank you QUESTIONS? 44 / 44 Understanding Random Forests - Marc Garcia