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: 19.42%
#> Confusion matrix:
#>    M  R class.error
#> M 66  9     0.12000
#> R 18 46     0.28125
print(learner$importance())
#>           V11           V48           V12           V10           V49 
#>  0.0274410257  0.0150082443  0.0140897656  0.0127151217  0.0127024964 
#>           V45           V51           V46           V28            V9 
#>  0.0097565741  0.0093218080  0.0076787641  0.0073146800  0.0072498912 
#>           V27           V47           V31           V35           V36 
#>  0.0071790705  0.0064309095  0.0050554613  0.0048762964  0.0045719307 
#>           V13           V22           V23           V34            V5 
#>  0.0043655071  0.0042829971  0.0041351848  0.0040696380  0.0040377592 
#>           V21           V32           V16           V20            V8 
#>  0.0035493755  0.0028141939  0.0027896539  0.0027795633  0.0027705667 
#>            V4            V2           V15           V18           V44 
#>  0.0027203103  0.0026669743  0.0025414005  0.0023792122  0.0022941025 
#>           V52           V59           V26            V6           V54 
#>  0.0019105738  0.0017200181  0.0017077635  0.0016685721  0.0016229313 
#>           V17            V3            V1           V30           V24 
#>  0.0013335560  0.0013320355  0.0012857663  0.0012295489  0.0012041792 
#>           V58           V33           V38           V55           V60 
#>  0.0011548302  0.0010744409  0.0009503307  0.0009336425  0.0007873398 
#>           V50           V42           V29           V53           V25 
#>  0.0007077532  0.0007044190  0.0007029965  0.0005520869  0.0005369241 
#>           V39           V14            V7           V57           V41 
#>  0.0004795967  0.0004607807  0.0002708844  0.0002309180  0.0002176908 
#>           V19           V37           V43           V56           V40 
#>  0.0001879547  0.0001835097  0.0001157940 -0.0003531379 -0.0006577003 

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

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