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 68  9   0.1168831
#> R 20 42   0.3225806
print(learner$importance())
#>            V9           V11           V12           V10           V49 
#>  2.917475e-02  2.236524e-02  1.631477e-02  1.283972e-02  1.239122e-02 
#>           V48           V47           V36           V16            V4 
#>  8.972802e-03  7.227179e-03  6.389857e-03  5.972599e-03  5.271394e-03 
#>           V31           V17           V45           V13           V51 
#>  4.899074e-03  4.782425e-03  4.330378e-03  4.149344e-03  4.036351e-03 
#>           V20            V8           V37           V21           V46 
#>  3.605309e-03  3.494013e-03  3.295737e-03  3.075456e-03  3.062617e-03 
#>           V28           V30           V59            V6           V44 
#>  2.808179e-03  2.805633e-03  2.742479e-03  2.740594e-03  2.582705e-03 
#>           V50           V39            V5           V52           V18 
#>  2.415963e-03  2.401929e-03  2.359473e-03  2.254632e-03  2.216812e-03 
#>           V14           V26           V35           V32           V15 
#>  2.149727e-03  2.144001e-03  2.123356e-03  1.882355e-03  1.788891e-03 
#>           V58           V23            V3           V29           V25 
#>  1.694806e-03  1.373189e-03  1.146878e-03  1.105613e-03  1.082671e-03 
#>           V22           V43            V1           V53           V42 
#>  1.036162e-03  9.982038e-04  9.738504e-04  8.843141e-04  8.452774e-04 
#>           V56           V27            V2           V19           V38 
#>  7.030434e-04  6.513843e-04  5.463449e-04  5.450852e-04  4.731585e-04 
#>           V40           V54           V34           V24           V33 
#>  4.385642e-04  4.266878e-04  4.173558e-04  2.625243e-04  6.028029e-05 
#>           V55           V57           V41            V7           V60 
#> -1.564957e-04 -3.910790e-04 -4.230614e-04 -6.654719e-04 -6.951574e-04 

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

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