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: 17.99%
#> Confusion matrix:
#>    M  R class.error
#> M 67  8   0.1066667
#> R 17 47   0.2656250
print(learner$importance())
#>           V11           V12           V36            V9           V37 
#>  2.701970e-02  2.102930e-02  1.533491e-02  9.249568e-03  8.564426e-03 
#>           V51           V45           V49           V35           V21 
#>  8.502019e-03  6.675853e-03  6.351720e-03  6.117027e-03  5.657142e-03 
#>           V10           V48           V46           V28           V20 
#>  5.573533e-03  5.506034e-03  5.241043e-03  5.187344e-03  4.712465e-03 
#>           V13           V43           V47           V14           V27 
#>  4.431181e-03  4.310211e-03  4.146777e-03  3.820411e-03  3.696917e-03 
#>           V44            V1           V16           V15           V52 
#>  3.606418e-03  3.366482e-03  3.286341e-03  3.127338e-03  3.014141e-03 
#>            V4            V5           V34            V8           V18 
#>  2.988543e-03  2.929859e-03  2.626060e-03  2.624030e-03  2.362636e-03 
#>           V32           V31           V17           V42           V19 
#>  2.223753e-03  2.205070e-03  2.106189e-03  2.062113e-03  1.946484e-03 
#>           V59            V3           V39           V22            V6 
#>  1.926465e-03  1.908904e-03  1.650690e-03  1.395143e-03  1.379314e-03 
#>           V25           V24           V23           V26           V30 
#>  1.304881e-03  1.274995e-03  1.156992e-03  1.094499e-03  1.042638e-03 
#>            V2           V29           V58           V56           V55 
#>  9.716408e-04  8.558301e-04  8.108714e-04  8.041240e-04  7.781107e-04 
#>           V33           V50           V41           V54           V57 
#>  5.477553e-04  5.384663e-04  2.467835e-04  2.397294e-04  2.353618e-04 
#>           V40           V38           V53            V7           V60 
#>  4.737011e-05  6.232633e-06 -2.747339e-05 -6.743501e-04 -9.366648e-04 

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

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