Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random...

34
Lecture #15: Regression Trees & Random Forests Data Science 1 CS 109A, STAT 121A, AC 209A, E-109A Pavlos Protopapas Kevin Rader Rahul Dave Margo Levine

Transcript of Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random...

Page 1: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Lecture #15: Regression Trees & RandomForests

Data Science 1CS 109A, STAT 121A, AC 209A, E-109A

Pavlos Protopapas Kevin RaderRahul Dave Margo Levine

Page 2: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Lecture Outline

Review

Decision Trees for Regression

Bagging

Random Forests

2

Page 3: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Review

3

Page 4: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Decision Trees

A decision tree model is an interpretable model in which thefinal output is based on a series of comparisons of the valuesof predictors against threshold values.

Graphically, decision trees can be represented by a flow chart.

Geometrically, the model partitions the feature spacewherein each region is assigned a response variable valuebased on the training points contained in the region.

4

Page 5: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Learning Algorithm

To learn a decision tree model, we take a greedyapproach:

1. Start with an empty decision tree (undividedfeature space)

2. Choose the ‘optimal’ predictor on which to split andchoose the ‘optimal’ threshold value for splitting byapplying a splitting criterion

3. Recurse on on each new node until stoppingcondition is met

For classification, we label each region in the modelwith the label of the class to which the plurality of thepoints within the region belong.

5

Page 6: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Decision Trees for Regression

6

Page 7: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Adaptations for Regression

With just two modifications, we can use a decision tree model forregression:

▶ The three splitting criteria we’ve examined each promotedsplits that were pure - new regions increasingly specialized ina single class.

For classification, purity of the regions is a good indicator theperformance of the model.

For regression, we want to select a splitting criterion thatpromotes splits that improves the predictive accuracy of themodel as measured by, say, the MSE.

▶ For regression with output in R, we want to label each regionin the model with a real number - typically the average of theoutput values of the training points contained in the region.

7

Page 8: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Learning Regression Trees

The learning algorithms for decision trees in regression tasks is:

1. Start with an empty decision tree (undivided feature space)

2. Choose a predictor j on which to split and choose a thresholdvalue tj for splitting such that the weighted average MSE ofthe new regions as smallest possible:

argminj,tj

{N1

NMSE(R1) +

N2

NMSE(R2)

}or equivalently,

argminj,tj

{N1

NVar[y|x ∈ R1] +

N2

NVar[y|x ∈ R2]

}whereNi is the number of training points in Ri andN is thenumber of points in R.

3. Recurse on on each new node until stopping condition is met

8

Page 9: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Regression Trees Prediction

For any data point xi

1. Traverse the tree until we reach a leaf node.

2. Averaged value of the response variable y’s in theleaf (this is from the training set) is the yi

9

Page 10: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Stopping Conditions

Most of the stopping conditions, like maximum depthor minimum number of points in region, we saw lasttime can still be applied.

In the place of purity gain, we can instead computeaccuracy gain for splitting a region R

Gain(R) = ∆(R) = MSE(R)−N1

NMSE(R1)−

N2

NMSE(R2)

and stop the tree when the gain is less than somepre-defined threshold.

10

Page 11: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Expressiveness of Decision Trees

We’ve seen that classification trees approximate boundariesin the feature space that separate classes.

Regression trees, on the other hand, define simple functionsor step functions, functions that are defined on partitions ofthe feature space and are constant over each part.

11

Page 12: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Expressiveness of Decision Trees

For a fine enough partition of the feature space, thesefunctions can approximate complex non-linearfunctions.

11

Page 13: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Bagging

12

Page 14: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Limitations of Decision Tree Models

Decision trees models are highly interpretable and fastto train, using our greedy learning algorithm.

However, in order to capture a complex decisionboundary (or to approximate a complex function), weneed to use a large tree (since each time we can onlymake axis aligned splits).

We’ve seen that large trees have high variance and areprone to overfitting.

For these reasons, in practice, decision tree modelsoften underperforms when compared with otherclassification or regression methods.

13

Page 15: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Bagging

One way to adjust for the high variance of the output of anexperiment is to perform the experiment multiple times andthen average the results.

The same idea can be applied to high variance models:

1. (Bootstrap) we generate multiple samples of trainingdata, via bootstrapping. We train a full decision tree oneach sample of data.

2. (Aggregate) for a given input, we output the averagedoutputs of all the models for that input.

For classification, we return the class that is outputtedby the plurality of the models.

This method is called Bagging (Breiman, 1996), short for, ofcourse, Bootstrap Aggregating.

14

Page 16: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Bagging

Note that bagging enjoys the benefits of

1. High expressiveness - by using full trees eachmodel is able to approximate complex functionsand decision boundaries.

2. Low variance - averaging the prediction of all themodels reduces the variance in the final prediction,assuming that we choose a sufficiently largenumber of trees.

14

Page 17: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Bagging

14

Page 18: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Bagging

14

Page 19: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Bagging

However, the major drawback of bagging (and otherensemble methods that we will study) is that theaveraged model is no longer easily interpretable - i.e.one can no longer trace the ‘logic’ of an output througha series of decisions based on predictor values!

14

Page 20: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Variable Importance for Bagging

Bagging improves prediction accuracy at the expense ofinterpretability.

Calculate the total amount that the RSS (for regression)or Gini index (for classification) is decreased due tosplits over a given predictor, averaged over all B trees.

15

Page 21: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Variable Importance for Bagging

15

Page 22: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Out-of-Bag Error

Bagging is an example of an ensemble method, a method of buildinga single model by training and aggregating multiple models.

With ensemble methods, we get a new metric for assessing thepredictive performance of the model, the out-of-bag error.

Given a training set and an ensemble of modeled each trained on abootstrap sample, we compute the out-of-bag error of the averagedmodel by

1. for each point in the training set, we average the predictedoutput for this point over the models whose bootstraptraining set excludes this point.

We compute the error or squared error of this averagedprediction. Call this the point-wise out-of-bag error.

2. we average the point-wise out-of-bag error over the fulltraining set.

16

Page 23: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Bagging, correlated darta set

[show example]

17

Page 24: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Random Forests

18

Page 25: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Improving on Bagging

In practice, the ensembles of trees in Bagging tend to behighly correlated.

Suppose we have an extremely strong predictor, xj , inthe training set amongst moderate predictors. Then thegreedy learning algorithm ensures that most of themodels in the ensemble will choose to split on xj inearly iterations.

That is, each tree in the ensemble is identicallydistributed, with the expected output of the averagedmodel the same as the expected output of any one ofthe trees.

19

Page 26: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Improving on Bagging

Recall, for B number of identically but notindependently distributed variables with pairwisecorrelation ρ and variance σ2, the variance of their meanis

ρσ2 +1− ρ

Bσ2.

As we increase B, the second term vanishes but thefirst term remains.

Consequently, variance reduction in bagging is limitedby the fact that we are averaging over highly correlatedtrees.

19

Page 27: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Random Forests

Random Forest is a modified form of bagging thatcreates ensembles of independent decision trees.

To de-correlate the trees, we:

1. train each tree on a separate bootstrap sample ofthe full training set (same as in bagging)

2. for each tree, at each split, we randomly select a setof J ′ predictors from the full set of predictors.

From amongst the J ′ predictors, we select theoptimal predictor and the optimal correspondingthreshold for the split.

20

Page 28: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Tuning Random Forests

Random forest models have multiple hyper-parametersto tune:

1. the number of predictors to randomly select ateach split

2. the total number of trees in the ensemble

3. the minimum leaf node size

In theory, each tree in the random forest is full, but inpractice this can be computationally expensive (andadded redundancies in the model), thus, imposing aminimum node size is not unusual.

21

Page 29: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Tuning Random Forests

There are standard (default) values for each of randomforest hyper-parameters recommended by long timepractitioners, but generally these parameters should betuned through cross validation (making them data andproblem dependent).

Using out-of-bag errors, training and cross validationcan be done in a single sequence - we cease trainingonce the out-of-bag error stabilizes

21

Page 30: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Variable Importance for RF

▶ Record the prediction accuracy on the oob samplesfor each tree.

▶ Randomly permute the data for column j in the oobsamples the record the accuracy again.

▶ The decrease in accuracy as a result of thispermuting is averaged over all trees, and is used asa measure of the importance of variable j in therandom forest.

22

Page 31: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Variable Importance for RF

22

Page 32: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Example

[compare RF, Bagging and Tree]

23

Page 33: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Final Thoughts on Random Forests

▶ When the number of predictors is large, but the numberof relevant predictors is small, random forests canperform poorly.

In each split, the chances of selected a relevantpredictor will be low and hence most trees in theensemble will be weak models.

24

Page 34: Lecture#15:RegressionTrees&Random Forests · 2018-11-16 · Lecture#15:RegressionTrees&Random Forests DataScience1 CS109A,STAT121A,AC209A,E-109A PavlosProtopapas KevinRader RahulDave

Final Thoughts on Random Forests

▶ Increasing the number of trees in the ensemblegenerally does not increase the risk of overfitting.

Again, by decomposing the generalization error in termsof bias and variance, we see that increasing the numberof trees produces a model that is at least as robust as asingle tree.

However, if the number of trees is too large, then thetrees in the ensemble may become more correlated,increase the variance.

24