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: 18.71%
#> Confusion matrix:
#>    M  R class.error
#> M 65  9   0.1216216
#> R 17 48   0.2615385
print(learner$importance())
#>           V11           V28           V12           V49           V48 
#>  2.561690e-02  1.551117e-02  1.374209e-02  1.199957e-02  1.139715e-02 
#>           V52            V9           V47           V10           V27 
#>  9.994226e-03  9.775650e-03  9.566746e-03  7.980612e-03  6.295587e-03 
#>           V45           V43           V16           V51           V46 
#>  6.001118e-03  5.716601e-03  5.332084e-03  5.315305e-03  5.252474e-03 
#>            V5           V23           V17           V21           V15 
#>  5.010737e-03  4.973872e-03  4.396738e-03  4.250232e-03  3.888349e-03 
#>            V4           V42           V44           V20           V13 
#>  3.881554e-03  3.399768e-03  3.244855e-03  2.942757e-03  2.895556e-03 
#>           V36           V22           V18           V30           V55 
#>  2.616078e-03  2.498035e-03  2.239274e-03  2.070261e-03  1.783599e-03 
#>            V2           V53           V14            V1           V29 
#>  1.772114e-03  1.647427e-03  1.641110e-03  1.585866e-03  1.561360e-03 
#>           V35           V37            V8           V31           V26 
#>  1.499517e-03  1.498270e-03  1.378708e-03  1.323586e-03  1.256396e-03 
#>           V19           V38            V6           V24           V40 
#>  9.929197e-04  9.447357e-04  8.506525e-04  8.267421e-04  6.261365e-04 
#>           V41           V54            V3           V33           V25 
#>  5.979489e-04  5.825278e-04  5.419922e-04  4.777987e-04  3.720668e-04 
#>           V34           V59           V58           V60           V56 
#>  2.861834e-04  2.738708e-04  2.347632e-04  1.565367e-04  6.887820e-05 
#>           V50           V32            V7           V57           V39 
#>  4.042314e-05 -8.161016e-05 -2.484872e-04 -2.512960e-04 -5.943839e-04 

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

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