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: 18.71%
#> Confusion matrix:
#>    M  R class.error
#> M 57 12    0.173913
#> R 14 56    0.200000
print(learner$importance())
#>           V11            V9           V12           V48           V49 
#>  2.523579e-02  1.991220e-02  1.456923e-02  1.426493e-02  1.184312e-02 
#>           V10           V28           V13           V47           V51 
#>  1.108961e-02  1.098317e-02  9.425265e-03  8.982336e-03  8.172230e-03 
#>           V45           V52           V46           V27           V43 
#>  6.507316e-03  6.250183e-03  5.451653e-03  4.707661e-03  4.585914e-03 
#>           V16           V20           V21           V37           V18 
#>  4.539153e-03  4.521362e-03  4.453315e-03  4.228846e-03  3.147825e-03 
#>           V36           V17           V15           V44           V31 
#>  2.998408e-03  2.880706e-03  2.678012e-03  2.314436e-03  2.178903e-03 
#>           V19           V30           V32           V14           V35 
#>  2.081266e-03  1.993183e-03  1.865241e-03  1.818042e-03  1.751395e-03 
#>            V2            V6            V4           V29            V5 
#>  1.666919e-03  1.635394e-03  1.456663e-03  1.377111e-03  1.323026e-03 
#>           V42           V38           V50           V22            V1 
#>  1.275474e-03  1.124329e-03  1.094446e-03  1.013468e-03  9.646374e-04 
#>           V39            V8           V54           V23           V26 
#>  8.918000e-04  7.856474e-04  7.182793e-04  6.929886e-04  6.925293e-04 
#>           V59            V7           V53           V55           V25 
#>  6.519475e-04  5.497382e-04  5.048283e-04  4.813611e-04  4.013525e-04 
#>           V40           V58           V24           V34           V41 
#>  3.824096e-04  3.633771e-04  3.489969e-04  2.336143e-04  2.048128e-04 
#>           V60           V56            V3           V57           V33 
#>  5.120256e-05 -2.182754e-04 -2.882612e-04 -3.610545e-04 -5.224050e-04 

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

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