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/basics.html#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'
# 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=69, R=70
#> Number of trees: 500
#> Forest terminal node size: 1
#> Average no. of terminal nodes: 16.8
#> 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.0145
#> (OOB) Brier score: 0.13048773
#> (OOB) Normalized Brier score: 0.52195094
#> (OOB) AUC: 0.93343685
#> (OOB) Log-loss: 0.41807743
#> (OOB) PR-AUC: 0.94109969
#> (OOB) G-mean: 0.83343685
#> (OOB) Requested performance error: 0.17266187, 0.13043478, 0.21428571
#>
#> Confusion matrix:
#>
#> predicted
#> observed M R class.error
#> M 61 8 0.1159
#> R 15 55 0.2143
#>
#> (OOB) Misclassification rate: 0.1654676
#>
#> Random-classifier baselines (uniform):
#> Brier: 0.25 Normalized Brier: 1 Log-loss: 0.69314718
print(learner$importance())
#> V11 V12 V48 V10 V9
#> 0.0727601616 0.0554490301 0.0442865980 0.0258418193 0.0234597126
#> V13 V49 V17 V5 V36
#> 0.0224567452 0.0221717157 0.0219679583 0.0195737654 0.0184736399
#> V51 V52 V47 V18 V45
#> 0.0179962077 0.0174704240 0.0174318548 0.0168957762 0.0167335835
#> V26 V37 V20 V43 V16
#> 0.0144228477 0.0143992960 0.0140827143 0.0133658740 0.0129363565
#> V39 V1 V46 V42 V28
#> 0.0122050311 0.0119355793 0.0117647015 0.0115344028 0.0107601739
#> V27 V21 V14 V4 V15
#> 0.0103207935 0.0101758565 0.0101702876 0.0097462589 0.0082965333
#> V34 V55 V32 V29 V30
#> 0.0082698644 0.0082572452 0.0081522653 0.0079774364 0.0077057100
#> V40 V31 V19 V3 V23
#> 0.0075489744 0.0072694554 0.0072536724 0.0071540544 0.0070274658
#> V44 V59 V33 V6 V24
#> 0.0065559993 0.0064152723 0.0062613785 0.0059634802 0.0059514825
#> V22 V53 V2 V7 V41
#> 0.0049609898 0.0048098392 0.0047678360 0.0046565489 0.0042146896
#> V60 V56 V50 V8 V38
#> 0.0041802696 0.0033384968 0.0030539185 0.0024828043 0.0021802572
#> V58 V35 V57 V25 V54
#> 0.0017735872 0.0016461259 0.0016070079 0.0013388895 -0.0002812774
# Make predictions for the test rows
predictions = learner$predict(task, row_ids = ids$test)
# Score the predictions
predictions$score()
#> classif.ce
#> 0.1884058