Classification Random Forest SRC Learner
Source:R/learner_randomForestSRC_classif_rfsrc.R
mlr_learners_classif.rfsrc.Rd
Random 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.ratio
asmtry = max(ceiling(mtry.ratio * n_features), 1)
. Note thatmtry
andmtry.ratio
are mutually exclusive.sampsize
: This hyperparameter can alternatively be set via the added hyperparametersampsize.ratio
assampsize = max(ceiling(sampsize.ratio * n_obs), 1)
. Note thatsampsize
andsampsize.ratio
are mutually exclusive.cores
: This value is set as the optionrf.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
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=72, R=67
#> Number of trees: 500
#> Forest terminal node size: 1
#> Average no. of terminal nodes: 17.654
#> 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.0746
#> (OOB) Brier score: 0.15234861
#> (OOB) Normalized Brier score: 0.60939442
#> (OOB) AUC: 0.88557214
#> (OOB) Log-loss: 0.47010024
#> (OOB) PR-AUC: 0.88389273
#> (OOB) G-mean: 0.7520012
#> (OOB) Requested performance error: 0.23741007, 0.13888889, 0.34328358
#>
#> Confusion matrix:
#>
#> predicted
#> observed M R class.error
#> M 62 10 0.1389
#> R 23 44 0.3433
#>
#> (OOB) Misclassification rate: 0.2374101
#>
#> Random-classifier baselines (uniform):
#> Brier: 0.25 Normalized Brier: 1 Log-loss: 0.69314718
print(learner$importance())
#> V11 V10 V9 V12 V51 V49
#> 0.067599411 0.057239465 0.037889476 0.028804850 0.022395125 0.021837913
#> V45 V48 V20 V30 V47 V16
#> 0.020831506 0.019658120 0.019033280 0.017746281 0.017410307 0.016758188
#> V4 V28 V52 V36 V37 V32
#> 0.016082119 0.016001859 0.013656088 0.012972014 0.012704441 0.012521657
#> V27 V40 V17 V18 V31 V13
#> 0.012338326 0.012162557 0.011945741 0.011503208 0.011062606 0.010926955
#> V15 V6 V43 V19 V8 V5
#> 0.010470720 0.009359556 0.009132666 0.008560630 0.008552323 0.008544926
#> V34 V23 V26 V21 V44 V14
#> 0.008415923 0.008267802 0.008144920 0.008138852 0.007399309 0.007307627
#> V53 V58 V41 V38 V59 V1
#> 0.007283823 0.006998183 0.006734628 0.006720811 0.006709906 0.006408192
#> V22 V3 V57 V56 V7 V33
#> 0.006279150 0.005948666 0.005691862 0.005215567 0.004652026 0.004473813
#> V60 V42 V50 V2 V39 V55
#> 0.004346702 0.004213972 0.003947644 0.003942211 0.003327298 0.003038502
#> V46 V24 V29 V54 V25 V35
#> 0.002635236 0.002457247 0.001884243 0.001633071 0.001420752 0.001315825
# Make predictions for the test rows
predictions = learner$predict(task, row_ids = ids$test)
# Score the predictions
predictions$score()
#> classif.ce
#> 0.1449275