Skip to contents

Random forest for classification. Calls randomForest::randomForest() from randomForest.

Dictionary

This Learner can be instantiated via lrn():

lrn("classif.randomForest")

Meta Information

  • Task type: “classif”

  • Predict Types: “response”, “prob”

  • Feature Types: “logical”, “integer”, “numeric”, “factor”, “ordered”

  • Required Packages: mlr3, mlr3extralearners, randomForest

Parameters

IdTypeDefaultLevelsRange
ntreeinteger500\([1, \infty)\)
mtryinteger-\([1, \infty)\)
replacelogicalTRUETRUE, FALSE-
classwtuntypedNULL-
cutoffuntyped--
stratauntyped--
sampsizeuntyped--
nodesizeinteger1\([1, \infty)\)
maxnodesinteger-\([1, \infty)\)
importancecharacterFALSEaccuracy, gini, none-
localImplogicalFALSETRUE, FALSE-
proximitylogicalFALSETRUE, FALSE-
oob.proxlogical-TRUE, FALSE-
norm.voteslogicalTRUETRUE, FALSE-
do.tracelogicalFALSETRUE, FALSE-
keep.forestlogicalTRUETRUE, FALSE-
keep.inbaglogicalFALSETRUE, FALSE-
predict.alllogicalFALSETRUE, FALSE-
nodeslogicalFALSETRUE, FALSE-

References

Breiman, Leo (2001). “Random Forests.” Machine Learning, 45(1), 5–32. ISSN 1573-0565, doi:10.1023/A:1010933404324 .

See also

Author

pat-s

Super classes

mlr3::Learner -> mlr3::LearnerClassif -> LearnerClassifRandomForest

Methods

Inherited methods


Method new()

Creates a new instance of this R6 class.


Method importance()

The importance scores are extracted from the slot importance. Parameter 'importance' must be set to either "accuracy" or "gini".

Usage

LearnerClassifRandomForest$importance()

Returns

Named numeric().


Method oob_error()

OOB errors are extracted from the model slot err.rate.

Usage

LearnerClassifRandomForest$oob_error()

Returns

numeric(1).


Method clone()

The objects of this class are cloneable with this method.

Usage

LearnerClassifRandomForest$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

# Define the Learner
learner = mlr3::lrn("classif.randomForest", importance = "accuracy")
print(learner)
#> <LearnerClassifRandomForest:classif.randomForest>: Random Forest
#> * Model: -
#> * Parameters: importance=accuracy
#> * Packages: mlr3, mlr3extralearners, randomForest
#> * Predict Types:  [response], prob
#> * Feature Types: logical, integer, numeric, factor, ordered
#> * Properties: importance, multiclass, oob_error, twoclass, weights

# Define a Task
task = mlr3::tsk("sonar")
# Create train and test set
ids = mlr3::partition(task)

# Train the learner on the training ids
learner$train(task, row_ids = ids$train)

print(learner$model)
#> 
#> Call:
#>  randomForest(formula = formula, data = data, classwt = classwt,      cutoff = cutoff, importance = TRUE) 
#>                Type of random forest: classification
#>                      Number of trees: 500
#> No. of variables tried at each split: 7
#> 
#>         OOB estimate of  error rate: 20.86%
#> Confusion matrix:
#>    M  R class.error
#> M 67  8   0.1066667
#> R 21 43   0.3281250
print(learner$importance())
#>           V11           V12            V9           V10           V36 
#>  2.717515e-02  2.395888e-02  1.388048e-02  8.406069e-03  8.273901e-03 
#>           V47           V13            V5           V49           V51 
#>  8.096311e-03  8.048201e-03  7.729568e-03  7.654984e-03  7.590147e-03 
#>           V48           V52           V37           V16           V27 
#>  6.312205e-03  5.972657e-03  5.653645e-03  4.712117e-03  4.364364e-03 
#>           V45           V15            V4           V46           V21 
#>  3.798688e-03  3.748484e-03  3.618038e-03  3.462182e-03  3.343985e-03 
#>           V39           V44           V14           V50           V54 
#>  2.863128e-03  2.737147e-03  2.512185e-03  2.502525e-03  2.379943e-03 
#>           V17           V26            V6           V58           V22 
#>  2.332428e-03  2.131952e-03  2.074112e-03  2.049535e-03  2.022364e-03 
#>            V2           V23           V28           V25           V20 
#>  2.007287e-03  2.000708e-03  1.969641e-03  1.955514e-03  1.923627e-03 
#>           V60           V42           V19           V43            V1 
#>  1.863758e-03  1.827722e-03  1.676401e-03  1.651613e-03  1.586040e-03 
#>           V35           V32           V53           V38           V24 
#>  1.478282e-03  1.450080e-03  1.331703e-03  1.286426e-03  1.230393e-03 
#>           V29           V18           V59           V40           V55 
#>  1.115236e-03  1.114981e-03  1.073292e-03  1.004972e-03  9.535023e-04 
#>           V33           V56           V31            V8           V34 
#>  9.453580e-04  8.407759e-04  6.083788e-04  5.536828e-04  4.306289e-04 
#>           V57           V30            V3            V7           V41 
#>  3.916661e-04  3.283200e-04  1.758428e-04 -8.456304e-05 -4.921452e-04 

# Make predictions for the test rows
predictions = learner$predict(task, row_ids = ids$test)

# Score the predictions
predictions$score()
#> classif.ce 
#>  0.1594203