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 64  8   0.1111111
#> R 21 46   0.3134328
print(learner$importance())
#>           V11            V9           V49           V12           V10 
#>  3.006151e-02  1.904614e-02  1.372383e-02  1.339351e-02  1.320822e-02 
#>           V48           V47           V21           V45           V52 
#>  9.842676e-03  8.126560e-03  7.619489e-03  6.878568e-03  6.710456e-03 
#>            V4           V20           V36           V46           V17 
#>  6.571320e-03  6.430260e-03  6.226161e-03  4.794111e-03  4.671071e-03 
#>           V23           V28           V18           V39           V35 
#>  4.510343e-03  3.937851e-03  3.534600e-03  3.348480e-03  3.011992e-03 
#>           V27           V19           V37           V44           V16 
#>  2.968183e-03  2.944083e-03  2.849459e-03  2.724859e-03  2.713333e-03 
#>           V22           V51           V31            V1           V26 
#>  2.669834e-03  2.522467e-03  2.266004e-03  2.073883e-03  1.828775e-03 
#>            V5           V30           V43           V25            V2 
#>  1.703760e-03  1.432214e-03  1.370920e-03  1.332823e-03  9.390031e-04 
#>           V24           V32            V8           V38           V15 
#>  8.498513e-04  8.144633e-04  7.364079e-04  7.315953e-04  7.294239e-04 
#>           V41           V55           V53           V34            V7 
#>  6.618511e-04  6.259566e-04  5.568435e-04  5.532269e-04  4.957157e-04 
#>           V29           V57           V42           V59           V13 
#>  4.028236e-04  3.969816e-04  3.830966e-04  3.775797e-04  3.653505e-04 
#>           V60           V14            V6           V40           V33 
#>  3.639320e-04  2.791276e-04  2.034076e-04  1.471439e-04  1.459719e-04 
#>            V3           V58           V54           V50           V56 
#>  4.574223e-05 -4.375508e-04 -5.058738e-04 -6.340515e-04 -9.588299e-04 

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

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