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=73, R=66
#>                      Number of trees: 500
#>            Forest terminal node size: 1
#>        Average no. of terminal nodes: 17.522
#> 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.1061
#>                    (OOB) Brier score: 0.14918668
#>         (OOB) Normalized Brier score: 0.59674673
#>                            (OOB) AUC: 0.90182648
#>                       (OOB) Log-loss: 0.46606578
#>                         (OOB) PR-AUC: 0.89806727
#>                         (OOB) G-mean: 0.77314837
#>    (OOB) Requested performance error: 0.21582734, 0.12328767, 0.31818182
#> 
#> Confusion matrix:
#> 
#>           predicted
#>   observed  M  R class.error
#>          M 64  9      0.1233
#>          R 21 45      0.3182
#> 
#>       (OOB) Misclassification rate: 0.2158273
#> 
#> Random-classifier baselines (uniform):
#>    Brier: 0.25   Normalized Brier: 1   Log-loss: 0.69314718
print(learner$importance())
#>           V12           V11           V45           V10           V36 
#>  0.0665314863  0.0661425968  0.0309533479  0.0291046131  0.0252665819 
#>           V46            V9           V49           V51           V13 
#>  0.0208554356  0.0191303566  0.0189899070  0.0167126114  0.0155813376 
#>           V37           V47           V16           V15           V21 
#>  0.0154949602  0.0152291597  0.0146812629  0.0144465656  0.0127956525 
#>           V52           V27            V4            V5           V48 
#>  0.0123931724  0.0120582125  0.0112140706  0.0109505908  0.0101868814 
#>           V17           V20           V39           V58           V23 
#>  0.0100495965  0.0100250896  0.0079803262  0.0068298629  0.0065455893 
#>           V14            V3            V7           V31           V44 
#>  0.0065317987  0.0064430409  0.0061237334  0.0057962746  0.0056713338 
#>           V40           V26           V57           V19           V35 
#>  0.0056345099  0.0055533631  0.0055147694  0.0054936609  0.0051174400 
#>           V56           V18            V8           V32           V24 
#>  0.0050378616  0.0046321816  0.0046315177  0.0042438463  0.0042396965 
#>           V30           V38            V6           V41           V53 
#>  0.0042285038  0.0041893279  0.0038384556  0.0037970541  0.0030339188 
#>           V60           V33           V54           V25           V22 
#>  0.0029539534  0.0028926185  0.0026119772  0.0024713305  0.0023747955 
#>           V28           V43            V2           V42           V34 
#>  0.0018715940  0.0017234631  0.0016918556  0.0016008765  0.0014974743 
#>            V1           V29           V59           V55           V50 
#>  0.0014510737  0.0013020738  0.0002896040 -0.0004349047 -0.0004384282 

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

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