Skip to contents

Random forest for classification. Calls randomForestSRC::rfsrc() from randomForestSRC.

Dictionary

This Learner can be instantiated via lrn():

lrn("classif.rfsrc")

Meta Information

  • Task type: “classif”

  • Predict Types: “response”, “prob”

  • Feature Types: “logical”, “integer”, “numeric”, “factor”

  • Required Packages: mlr3, mlr3extralearners, randomForestSRC

Parameters

IdTypeDefaultLevelsRange
ntreeinteger500\([1, \infty)\)
mtryinteger-\([1, \infty)\)
mtry.rationumeric-\([0, 1]\)
nodesizeinteger15\([1, \infty)\)
nodedepthinteger-\([1, \infty)\)
splitrulecharacterginigini, auc, entropy-
nsplitinteger10\([0, \infty)\)
importancecharacterFALSEFALSE, TRUE, none, permute, random, anti-
block.sizeinteger10\([1, \infty)\)
bootstrapcharacterby.rootby.root, by.node, none, by.user-
samptypecharactersworswor, swr-
sampuntyped--
membershiplogicalFALSETRUE, FALSE-
sampsizeuntyped--
sampsize.rationumeric-\([0, 1]\)
na.actioncharacterna.omitna.omit, na.impute-
nimputeinteger1\([1, \infty)\)
proximitycharacterFALSEFALSE, TRUE, inbag, oob, all-
distancecharacterFALSEFALSE, TRUE, inbag, oob, all-
forest.wtcharacterFALSEFALSE, TRUE, inbag, oob, all-
xvar.wtuntyped--
split.wtuntyped--
forestlogicalTRUETRUE, FALSE-
var.usedcharacterFALSEFALSE, all.trees-
split.depthcharacterFALSEFALSE, all.trees, by.tree-
seedinteger-\((-\infty, -1]\)
do.tracelogicalFALSETRUE, FALSE-
get.treeuntyped--
outcomecharactertraintrain, test-
ptn.countinteger0\([0, \infty)\)
coresinteger1\([1, \infty)\)
save.memorylogicalFALSETRUE, FALSE-
perf.typecharacter-gmean, misclass, brier, none-
case.depthlogicalFALSETRUE, FALSE-
marginal.xvaruntypedNULL-

Custom mlr3 parameters

  • mtry: This hyperparameter can alternatively be set via the added hyperparameter mtry.ratio as mtry = max(ceiling(mtry.ratio * n_features), 1). Note that mtry and mtry.ratio are mutually exclusive.

  • sampsize: This hyperparameter can alternatively be set via the added hyperparameter sampsize.ratio as sampsize = max(ceiling(sampsize.ratio * n_obs), 1). Note that sampsize and sampsize.ratio are mutually exclusive.

  • cores: This value is set as the option rf.cores during training and is set to 1 by default.

References

Breiman, Leo (2001). “Random Forests.” Machine Learning, 45(1), 5–32. ISSN 1573-0565. doi:10.1023/A:1010933404324 .

See also

Author

RaphaelS1

Super classes

mlr3::Learner -> mlr3::LearnerClassif -> LearnerClassifRandomForestSRC

Methods

Inherited methods


LearnerClassifRandomForestSRC$new()

Creates a new instance of this R6 class.


LearnerClassifRandomForestSRC$importance()

The importance scores are extracted from the model slot importance, returned for 'all'.

Usage

LearnerClassifRandomForestSRC$importance()

Returns

Named numeric().


LearnerClassifRandomForestSRC$selected_features()

Selected features are extracted from the model slot var.used.

Note: Due to a known issue in randomForestSRC, enabling var.used = "all.trees" causes prediction to fail. Therefore, this setting should be used exclusively for feature selection purposes and not when prediction is required.

Usage

LearnerClassifRandomForestSRC$selected_features()

Returns

character().


LearnerClassifRandomForestSRC$oob_error()

OOB error extracted from the model slot err.rate.

Usage

LearnerClassifRandomForestSRC$oob_error()

Returns

numeric().


LearnerClassifRandomForestSRC$clone()

The objects of this class are cloneable with this method.

Usage

LearnerClassifRandomForestSRC$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

# Define the Learner
learner = lrn("classif.rfsrc", importance = "TRUE")
print(learner)
#> 
#> ── <LearnerClassifRandomForestSRC> (classif.rfsrc): Random Forest ──────────────
#> • Model: -
#> • Parameters: importance=TRUE
#> • Packages: mlr3, mlr3extralearners, and randomForestSRC
#> • Predict Types: [response] and prob
#> • Feature Types: logical, integer, numeric, and factor
#> • Encapsulation: none (fallback: -)
#> • Properties: importance, missings, multiclass, oob_error, selected_features,
#> 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)
#>                          Sample size: 139
#>            Frequency of class labels: M=75, R=64
#>                      Number of trees: 500
#>            Forest terminal node size: 1
#>        Average no. of terminal nodes: 17.592
#> No. of variables tried at each split: 8
#>               Total no. of variables: 60
#>        Resampling used to grow trees: swor
#>     Resample size used to grow trees: 88
#>                             Analysis: RF-C
#>                               Family: class
#>                       Splitting rule: gini *random*
#>        Number of random split points: 10
#>                     Imbalanced ratio: 1.1719
#>                    (OOB) Brier score: 0.14567701
#>         (OOB) Normalized Brier score: 0.58270804
#>                            (OOB) AUC: 0.90635417
#>                       (OOB) Log-loss: 0.45693743
#>                         (OOB) PR-AUC: 0.89967139
#>                         (OOB) G-mean: 0.7804913
#>    (OOB) Requested performance error: 0.20143885, 0.09333333, 0.328125
#> 
#> Confusion matrix:
#> 
#>           predicted
#>   observed  M  R class.error
#>          M 68  7      0.0933
#>          R 22 42      0.3438
#> 
#>       (OOB) Misclassification rate: 0.2086331
#> 
#> Random-classifier baselines (uniform):
#>    Brier: 0.25   Normalized Brier: 1   Log-loss: 0.69314718
print(learner$importance())
#>           V12           V11           V36           V37           V10 
#>  0.0593802784  0.0555654763  0.0358937532  0.0282041528  0.0265991836 
#>           V39           V52           V17           V16           V51 
#>  0.0264479691  0.0234096441  0.0196437894  0.0194409863  0.0186364680 
#>           V13           V42            V9           V58           V28 
#>  0.0183282405  0.0179943039  0.0176134054  0.0168613173  0.0168475665 
#>           V18           V49           V15           V20           V22 
#>  0.0164261962  0.0140771382  0.0135507524  0.0130842778  0.0129193087 
#>            V4           V54           V27           V41           V21 
#>  0.0126097716  0.0123384625  0.0120468618  0.0119068265  0.0116490794 
#>           V34           V26           V38           V48           V45 
#>  0.0113121506  0.0112181146  0.0107704949  0.0101528538  0.0098821685 
#>           V47           V33           V43            V3            V8 
#>  0.0097319621  0.0089900895  0.0088885083  0.0087259952  0.0085670222 
#>           V31           V46           V30           V23            V7 
#>  0.0084115762  0.0082976223  0.0081522507  0.0078562920  0.0074223507 
#>           V29           V35           V24            V1           V44 
#>  0.0069707823  0.0063486761  0.0062545173  0.0054093147  0.0052430422 
#>           V55            V6           V32           V59           V50 
#>  0.0052231731  0.0050937892  0.0050854653  0.0048254198  0.0048221823 
#>            V5           V57           V60           V53           V19 
#>  0.0047641154  0.0045067677  0.0036512960  0.0032181207  0.0032035125 
#>           V56           V14            V2           V25           V40 
#>  0.0031884975  0.0017742260  0.0010346243 -0.0008684147 -0.0013043160 

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

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