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: 21.58%
#> Confusion matrix:
#>    M  R class.error
#> M 71  8   0.1012658
#> R 22 38   0.3666667
print(learner$importance())
#>          V11          V12          V49          V10          V48          V47 
#> 3.141615e-02 2.059075e-02 1.553557e-02 1.373584e-02 1.021993e-02 7.409149e-03 
#>           V9          V37          V45          V52          V21          V13 
#> 6.851922e-03 6.819531e-03 6.358177e-03 5.911165e-03 5.235409e-03 5.172277e-03 
#>          V44          V23          V51          V36           V8          V34 
#> 4.976401e-03 4.830731e-03 4.560495e-03 4.264481e-03 4.218096e-03 4.168760e-03 
#>          V19          V20           V5          V27          V43          V18 
#> 3.507162e-03 3.390688e-03 3.262520e-03 3.111309e-03 2.953992e-03 2.952221e-03 
#>          V17           V4          V28          V16          V14          V33 
#> 2.942026e-03 2.660873e-03 2.565565e-03 2.471871e-03 2.105247e-03 2.066788e-03 
#>           V6          V22          V31          V46          V32          V38 
#> 1.938467e-03 1.898716e-03 1.840324e-03 1.708950e-03 1.585245e-03 1.506568e-03 
#>           V7          V40          V55          V59           V1          V60 
#> 1.490747e-03 1.465199e-03 1.389956e-03 1.343599e-03 1.343137e-03 1.192643e-03 
#>          V54          V25          V15          V26          V30          V42 
#> 1.184769e-03 1.092707e-03 1.056038e-03 1.050757e-03 1.041535e-03 1.040271e-03 
#>          V35          V56           V2           V3          V58          V39 
#> 9.223719e-04 8.995145e-04 8.516036e-04 7.193312e-04 6.616488e-04 6.206022e-04 
#>          V57          V29          V53          V41          V50          V24 
#> 5.575594e-04 3.828462e-04 1.965573e-04 1.708466e-04 1.636299e-04 1.929717e-05 

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

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