0% found this document useful (0 votes)
4 views25 pages

Understanding Random Forests in R

Uploaded by

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

Understanding Random Forests in R

Uploaded by

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

DataCamp Supervised

Learning in R: Classification

SUPERVISED LEARNING IN R: CLASSIFICATION

Making decisions with


trees

Brett Lantz
Instructor
DataCamp Supervised Learning in R: Classification

A decision tree model


DataCamp Supervised Learning in R: Classification

Decision trees for prediction


DataCamp Supervised Learning in R: Classification

Divide-and-conquer
DataCamp Supervised Learning in R: Classification

Divide-and-conquer
DataCamp Supervised Learning in R: Classification

Divide-and-conquer
DataCamp Supervised Learning in R: Classification

The resulting tree


DataCamp Supervised Learning in R: Classification

Building trees in R
# building a simple rpart classification tree
library(rpart)
m <- rpart(outcome ~ loan_amount + credit_score, data = loans,
method = "class")

# making predictions from an rpart tree


p <- predict(m, test_data, type = "class")
DataCamp Supervised Learning in R: Classification

SUPERVISED LEARNING IN R: CLASSIFICATION

Let's practice!
DataCamp Supervised Learning in R: Classification

SUPERVISED LEARNING IN R: CLASSIFICATION

Growing larger
classification trees

Brett Lantz
Instructor
DataCamp Supervised Learning in R: Classification

Choosing where to split


DataCamp Supervised Learning in R: Classification

Axis-parallel splits
DataCamp Supervised Learning in R: Classification

The problem of overfitting


DataCamp Supervised Learning in R: Classification

Evaluating model performance


DataCamp Supervised Learning in R: Classification

SUPERVISED LEARNING IN R: CLASSIFICATION

Let's practice!
DataCamp Supervised Learning in R: Classification

SUPERVISED LEARNING IN R: CLASSIFICATION

Tending to
classification trees

Brett Lantz
Instructor
DataCamp Supervised Learning in R: Classification

Pre-pruning
DataCamp Supervised Learning in R: Classification

Post-pruning
DataCamp Supervised Learning in R: Classification

Pre- and post-pruning with R


# pre-pruning with rpart
library(rpart)
prune_control <- [Link](maxdepth = 30, minsplit = 20)

m <- rpart(repaid ~ credit_score + request_amt,


data = loans,
method = "class",
control = prune_control)

# post-pruning with rpart


m <- rpart(repaid ~ credit_score + request_amt,
data = loans,
method = "class")

plotcp(m)

m_pruned <- prune(m, cp = 0.20)


DataCamp Supervised Learning in R: Classification

SUPERVISED LEARNING IN R: CLASSIFICATION

Let's practice!
DataCamp Supervised Learning in R: Classification

SUPERVISED LEARNING IN R: CLASSIFICATION

Seeing the forest from


the trees

Brett Lantz
Instructor
DataCamp Supervised Learning in R: Classification

Understanding random forests


DataCamp Supervised Learning in R: Classification

Making decisions as an ensemble


DataCamp Supervised Learning in R: Classification

Random forests in R
# building a simple random forest
library(randomForest)
m <- randomForest(repaid ~ credit_score + request_amt, data = loans,
ntree = 500, # number of trees in the forest
mtry = sqrt(p)) # number of predictors (p) per tree
# making predictions from a random forest
p <- predict(m, test_data)
DataCamp Supervised Learning in R: Classification

SUPERVISED LEARNING IN R: CLASSIFICATION

Let's practice!

You might also like