Classification Random Forest SRC Learner
Source:R/learner_randomForestSRC_classif_rfsrc.R
mlr_learners_classif.rfsrc.RdRandom forest for classification.
Calls randomForestSRC::rfsrc() from randomForestSRC.
Meta Information
Task type: “classif”
Predict Types: “response”, “prob”
Feature Types: “logical”, “integer”, “numeric”, “factor”
Required Packages: mlr3, mlr3extralearners, randomForestSRC
Parameters
| Id | Type | Default | Levels | Range |
| ntree | integer | 500 | \([1, \infty)\) | |
| mtry | integer | - | \([1, \infty)\) | |
| mtry.ratio | numeric | - | \([0, 1]\) | |
| nodesize | integer | 15 | \([1, \infty)\) | |
| nodedepth | integer | - | \([1, \infty)\) | |
| splitrule | character | gini | gini, auc, entropy | - |
| nsplit | integer | 10 | \([0, \infty)\) | |
| importance | character | FALSE | FALSE, TRUE, none, permute, random, anti | - |
| block.size | integer | 10 | \([1, \infty)\) | |
| bootstrap | character | by.root | by.root, by.node, none, by.user | - |
| samptype | character | swor | swor, swr | - |
| samp | untyped | - | - | |
| membership | logical | FALSE | TRUE, FALSE | - |
| sampsize | untyped | - | - | |
| sampsize.ratio | numeric | - | \([0, 1]\) | |
| na.action | character | na.omit | na.omit, na.impute | - |
| nimpute | integer | 1 | \([1, \infty)\) | |
| proximity | character | FALSE | FALSE, TRUE, inbag, oob, all | - |
| distance | character | FALSE | FALSE, TRUE, inbag, oob, all | - |
| forest.wt | character | FALSE | FALSE, TRUE, inbag, oob, all | - |
| xvar.wt | untyped | - | - | |
| split.wt | untyped | - | - | |
| forest | logical | TRUE | TRUE, FALSE | - |
| var.used | character | FALSE | FALSE, all.trees | - |
| split.depth | character | FALSE | FALSE, all.trees, by.tree | - |
| seed | integer | - | \((-\infty, -1]\) | |
| do.trace | logical | FALSE | TRUE, FALSE | - |
| get.tree | untyped | - | - | |
| outcome | character | train | train, test | - |
| ptn.count | integer | 0 | \([0, \infty)\) | |
| cores | integer | 1 | \([1, \infty)\) | |
| save.memory | logical | FALSE | TRUE, FALSE | - |
| perf.type | character | - | gmean, misclass, brier, none | - |
| case.depth | logical | FALSE | TRUE, FALSE | - |
| marginal.xvar | untyped | NULL | - |
Custom mlr3 parameters
mtry: This hyperparameter can alternatively be set via the added hyperparametermtry.ratioasmtry = max(ceiling(mtry.ratio * n_features), 1). Note thatmtryandmtry.ratioare mutually exclusive.sampsize: This hyperparameter can alternatively be set via the added hyperparametersampsize.ratioassampsize = max(ceiling(sampsize.ratio * n_obs), 1). Note thatsampsizeandsampsize.ratioare mutually exclusive.cores: This value is set as the optionrf.coresduring 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
as.data.table(mlr_learners)for a table of available Learners in the running session (depending on the loaded packages).Chapter in the mlr3book: https://mlr3book.mlr-org.com/chapters/chapter2/data_and_basic_modeling.html#sec-learners
mlr3learners for a selection of recommended learners.
mlr3cluster for unsupervised clustering learners.
mlr3pipelines to combine learners with pre- and postprocessing steps.
mlr3tuning for tuning of hyperparameters, mlr3tuningspaces for established default tuning spaces.
Super classes
mlr3::Learner -> mlr3::LearnerClassif -> LearnerClassifRandomForestSRC
Methods
Inherited methods
Method importance()
The importance scores are extracted from the model slot importance, returned for
'all'.
Returns
Named numeric().
Method 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.
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=78, R=61
#> Number of trees: 500
#> Forest terminal node size: 1
#> Average no. of terminal nodes: 16.888
#> 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.2787
#> (OOB) Brier score: 0.13368692
#> (OOB) Normalized Brier score: 0.53474766
#> (OOB) AUC: 0.92654477
#> (OOB) Log-loss: 0.42841033
#> (OOB) PR-AUC: 0.91282222
#> (OOB) G-mean: 0.78339213
#> (OOB) Requested performance error: 0.18705036, 0.06410256, 0.3442623
#>
#> Confusion matrix:
#>
#> predicted
#> observed M R class.error
#> M 73 5 0.0641
#> R 21 40 0.3443
#>
#> (OOB) Misclassification rate: 0.1870504
#>
#> Random-classifier baselines (uniform):
#> Brier: 0.25 Normalized Brier: 1 Log-loss: 0.69314718
print(learner$importance())
#> V11 V12 V16 V9 V46
#> 0.0731097590 0.0646758753 0.0415437353 0.0328580575 0.0292458280
#> V10 V17 V23 V47 V52
#> 0.0287885508 0.0287884842 0.0200680526 0.0197889317 0.0193582605
#> V44 V49 V21 V15 V20
#> 0.0192347299 0.0190430666 0.0189130546 0.0178568609 0.0177411254
#> V4 V28 V45 V6 V27
#> 0.0173257063 0.0172925012 0.0172472518 0.0162899096 0.0156250924
#> V48 V51 V13 V14 V39
#> 0.0139940649 0.0132227022 0.0122149615 0.0121890427 0.0110203940
#> V18 V55 V50 V36 V43
#> 0.0103299619 0.0102960702 0.0100629275 0.0100056079 0.0097669922
#> V30 V7 V26 V29 V42
#> 0.0095855911 0.0094736194 0.0091603766 0.0088131062 0.0083036029
#> V19 V5 V31 V54 V53
#> 0.0081491830 0.0077007612 0.0066894721 0.0066663500 0.0057970493
#> V32 V35 V40 V38 V57
#> 0.0057858748 0.0054101115 0.0053574300 0.0049757189 0.0048221259
#> V24 V22 V37 V33 V1
#> 0.0046517253 0.0045572949 0.0045507244 0.0043628997 0.0040825049
#> V58 V59 V8 V34 V3
#> 0.0037780714 0.0032012717 0.0028857109 0.0026160236 0.0021622579
#> V60 V41 V2 V25 V56
#> 0.0017192451 0.0015920378 0.0013001468 0.0011636210 -0.0001723745
# Make predictions for the test rows
predictions = learner$predict(task, row_ids = ids$test)
# Score the predictions
predictions$score()
#> classif.ce
#> 0.2463768