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=77, R=62
#>                      Number of trees: 500
#>            Forest terminal node size: 1
#>        Average no. of terminal nodes: 17.054
#> 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.2419
#>                    (OOB) Brier score: 0.13211214
#>         (OOB) Normalized Brier score: 0.52844854
#>                            (OOB) AUC: 0.92773356
#>                       (OOB) Log-loss: 0.424355
#>                         (OOB) PR-AUC: 0.91908969
#>                         (OOB) G-mean: 0.84192609
#>    (OOB) Requested performance error: 0.14388489, 0.06493506, 0.24193548
#> 
#> Confusion matrix:
#> 
#>           predicted
#>   observed  M  R class.error class.freq
#>          M 72  5      0.0649         77
#>          R 17 45      0.2742         62
#> 
#>       (OOB) Misclassification rate: 0.1582734
#> 
#> Random-classifier baselines (uniform):
#>    Brier: 0.25   Normalized Brier: 1   Log-loss: 0.69314718
print(learner$importance())
#>           V11            V9           V12           V21           V10 
#>  0.0987813478  0.0593791004  0.0512614697  0.0331202156  0.0320977654 
#>           V36           V37           V16           V17           V31 
#>  0.0237869425  0.0219793789  0.0203196750  0.0203003395  0.0194645221 
#>           V52            V4           V20           V27           V43 
#>  0.0190317497  0.0165330673  0.0164176416  0.0161322476  0.0148287744 
#>           V45           V18           V15           V51           V44 
#>  0.0144172671  0.0142078647  0.0140801562  0.0123332676  0.0115968392 
#>           V22           V23           V48           V13            V1 
#>  0.0110893929  0.0108954143  0.0105645146  0.0104599442  0.0104416656 
#>           V49            V5            V6           V42           V47 
#>  0.0101475012  0.0090504574  0.0087511417  0.0087118723  0.0086664118 
#>           V28           V38           V32           V34           V58 
#>  0.0085869701  0.0081290054  0.0079811329  0.0078316218  0.0072614470 
#>            V3           V19           V41           V40           V53 
#>  0.0071198027  0.0068596317  0.0068235537  0.0065304165  0.0061147089 
#>           V33           V46            V7           V29            V8 
#>  0.0061123612  0.0059760832  0.0059486068  0.0056664494  0.0056453367 
#>           V59           V14           V50           V39           V56 
#>  0.0052482250  0.0052093265  0.0048155740  0.0047967299  0.0045033783 
#>           V57           V54           V55           V60           V35 
#>  0.0037916061  0.0037683615  0.0036157820  0.0034908795  0.0030766116 
#>           V26           V30           V24           V25            V2 
#>  0.0030329760  0.0024638747  0.0021846459 -0.0006963026 -0.0021939367 

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

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