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: 16.55%
#> Confusion matrix:
#>    M  R class.error
#> M 72  6  0.07692308
#> R 17 44  0.27868852
print(learner$importance())
#>           V11           V12            V9           V10           V51 
#>  3.315535e-02  2.300663e-02  2.259985e-02  1.622792e-02  1.030349e-02 
#>           V36           V49           V13            V4           V47 
#>  9.316545e-03  7.991834e-03  7.914133e-03  7.520963e-03  7.134379e-03 
#>           V21           V48           V34           V46           V45 
#>  6.304167e-03  5.476671e-03  4.791380e-03  4.756388e-03  4.711105e-03 
#>            V8           V20           V37           V28           V31 
#>  4.378724e-03  4.223794e-03  3.888425e-03  3.784626e-03  3.579800e-03 
#>           V16           V23           V30           V35           V15 
#>  3.427552e-03  3.124478e-03  3.013062e-03  2.843649e-03  2.243705e-03 
#>            V1           V27           V22           V33           V32 
#>  2.166332e-03  2.158773e-03  2.103045e-03  1.987891e-03  1.846971e-03 
#>            V6           V44           V17           V26           V52 
#>  1.846549e-03  1.840895e-03  1.799067e-03  1.757556e-03  1.711575e-03 
#>           V42            V3           V14           V18            V5 
#>  1.662913e-03  1.495564e-03  1.323870e-03  1.222602e-03  1.136574e-03 
#>           V58            V7           V60           V38            V2 
#>  1.041699e-03  1.019123e-03  9.837200e-04  9.118684e-04  9.075802e-04 
#>           V53           V54           V40           V19           V29 
#>  8.719732e-04  6.968688e-04  6.658135e-04  6.466411e-04  5.947849e-04 
#>           V43           V39           V25           V24           V56 
#>  5.632690e-04  2.965314e-04  2.937429e-04  2.455842e-04  1.647085e-04 
#>           V41           V50           V59           V55           V57 
#>  9.185076e-05  5.329999e-05  2.870137e-05 -1.868396e-04 -5.118505e-04 

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

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