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


LearnerClassifRandomForest$new()

Creates a new instance of this R6 class.


LearnerClassifRandomForest$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().


LearnerClassifRandomForest$oob_error()

OOB errors are extracted from the model slot err.rate.

Usage

LearnerClassifRandomForest$oob_error()

Returns

numeric(1).


LearnerClassifRandomForest$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 = lrn("classif.randomForest", importance = "accuracy")
print(learner)
#> 
#> ── <LearnerClassifRandomForest> (classif.randomForest): Random Forest ──────────
#> • Model: -
#> • Parameters: importance=accuracy
#> • Packages: mlr3, mlr3extralearners, and randomForest
#> • Predict Types: [response] and prob
#> • Feature Types: logical, integer, numeric, factor, and ordered
#> • Encapsulation: none (fallback: -)
#> • Properties: importance, multiclass, oob_error, twoclass, and weights
#> • Other settings: use_weights = 'use', predict_raw = 'FALSE'

# Define a Task
task = tsk("sonar")
# Create train and test set
ids = 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 67  9   0.1184211
#> R 20 43   0.3174603
print(learner$importance())
#>           V12           V48            V9           V49           V11 
#>  1.630681e-02  1.604841e-02  1.380363e-02  1.320254e-02  1.285559e-02 
#>           V36           V47           V45           V10           V37 
#>  1.163667e-02  8.030089e-03  8.018064e-03  7.991237e-03  6.445518e-03 
#>           V51           V28           V13           V52           V20 
#>  6.120938e-03  5.120452e-03  5.113938e-03  4.783384e-03  4.580919e-03 
#>           V21            V5           V15           V46           V31 
#>  4.549427e-03  4.323355e-03  4.309241e-03  4.091397e-03  4.073778e-03 
#>           V23           V16           V22           V17           V44 
#>  3.659394e-03  3.338645e-03  3.099383e-03  2.878023e-03  2.858915e-03 
#>           V26           V32           V24           V18           V27 
#>  2.511304e-03  2.408658e-03  2.372575e-03  2.133420e-03  2.028357e-03 
#>            V8           V19           V38           V40           V25 
#>  1.829875e-03  1.758624e-03  1.687899e-03  1.545946e-03  1.526883e-03 
#>           V29           V55           V43            V2           V14 
#>  1.501878e-03  1.219948e-03  1.181902e-03  1.143867e-03  1.094171e-03 
#>            V6           V59           V30           V34           V54 
#>  1.068321e-03  1.064417e-03  9.501179e-04  9.400285e-04  8.566311e-04 
#>            V3            V4           V35            V7           V58 
#>  8.113130e-04  7.281868e-04  6.697755e-04  6.162394e-04  4.535585e-04 
#>           V50           V42            V1           V41           V39 
#>  2.742992e-04  2.735058e-04  2.393185e-04  2.191519e-04  1.760967e-04 
#>           V56           V57           V33           V53           V60 
#> -8.072641e-05 -1.424109e-04 -1.712127e-04 -2.096669e-04 -4.894667e-04 

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

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