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, and randomForest
#> • Predict Types: [response] and prob
#> • Feature Types: logical, integer, numeric, factor, and ordered
#> • Encapsulation: none (fallback: -)
#> • Properties: importance, multiclass, oob_error, twoclass, and weights
#> • Other settings: use_weights = 'use'

# 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.14%
#> Confusion matrix:
#>    M  R class.error
#> M 62 11   0.1506849
#> R 17 49   0.2575758
print(learner$importance())
#>           V12           V11           V48           V49           V10 
#>  1.954012e-02  1.823393e-02  1.810986e-02  1.478098e-02  1.195677e-02 
#>            V9           V36           V47           V13           V51 
#>  1.140873e-02  9.079952e-03  8.809801e-03  7.725510e-03  5.737290e-03 
#>           V21           V45           V37           V22           V28 
#>  5.632227e-03  5.467520e-03  5.311507e-03  4.869905e-03  4.794957e-03 
#>           V44           V35           V23            V5            V8 
#>  4.652545e-03  3.404877e-03  3.227543e-03  3.074009e-03  2.877529e-03 
#>           V18           V46           V20            V6           V27 
#>  2.836130e-03  2.413177e-03  2.402010e-03  2.329209e-03  2.325164e-03 
#>           V43           V52           V15            V2           V16 
#>  2.290321e-03  2.196867e-03  2.184775e-03  2.043311e-03  1.991092e-03 
#>            V1           V14           V31            V7           V42 
#>  1.827775e-03  1.602281e-03  1.586132e-03  1.521835e-03  1.510376e-03 
#>           V17            V3           V26           V56           V60 
#>  1.482003e-03  1.471900e-03  1.436512e-03  1.410681e-03  1.376443e-03 
#>           V38           V59           V19           V33           V41 
#>  1.321945e-03  1.278806e-03  1.270419e-03  1.055345e-03  1.008573e-03 
#>           V58           V25            V4           V50           V53 
#>  9.385504e-04  9.325867e-04  9.145518e-04  8.907383e-04  7.172904e-04 
#>           V32           V30           V39           V24           V40 
#>  5.990286e-04  5.888455e-04  5.590209e-04  5.561615e-04  4.824768e-04 
#>           V55           V57           V54           V34           V29 
#>  4.032725e-04  1.810352e-04  3.046178e-05 -3.321111e-04 -4.656126e-04 

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

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