Classification Random Forest Learner
Source:R/learner_randomForest_classif_randomForest.R
mlr_learners_classif.randomForest.RdRandom forest for classification.
Calls randomForest::randomForest() from randomForest.
Meta Information
Task type: “classif”
Predict Types: “response”, “prob”
Feature Types: “logical”, “integer”, “numeric”, “factor”, “ordered”
Required Packages: mlr3, mlr3extralearners, randomForest
Parameters
| Id | Type | Default | Levels | Range |
| ntree | integer | 500 | \([1, \infty)\) | |
| mtry | integer | - | \([1, \infty)\) | |
| replace | logical | TRUE | TRUE, FALSE | - |
| classwt | untyped | NULL | - | |
| cutoff | untyped | - | - | |
| strata | untyped | - | - | |
| sampsize | untyped | - | - | |
| nodesize | integer | 1 | \([1, \infty)\) | |
| maxnodes | integer | - | \([1, \infty)\) | |
| importance | character | FALSE | accuracy, gini, none | - |
| localImp | logical | FALSE | TRUE, FALSE | - |
| proximity | logical | FALSE | TRUE, FALSE | - |
| oob.prox | logical | - | TRUE, FALSE | - |
| norm.votes | logical | TRUE | TRUE, FALSE | - |
| do.trace | logical | FALSE | TRUE, FALSE | - |
| keep.forest | logical | TRUE | TRUE, FALSE | - |
| keep.inbag | logical | FALSE | TRUE, FALSE | - |
| predict.all | logical | FALSE | TRUE, FALSE | - |
| nodes | logical | FALSE | TRUE, FALSE | - |
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 -> LearnerClassifRandomForest
Methods
Inherited methods
mlr3::Learner$base_learner()mlr3::Learner$configure()mlr3::Learner$encapsulate()mlr3::Learner$format()mlr3::Learner$help()mlr3::Learner$predict()mlr3::Learner$predict_newdata()mlr3::Learner$print()mlr3::Learner$reset()mlr3::Learner$selected_features()mlr3::Learner$train()mlr3::LearnerClassif$predict_newdata_fast()
Method importance()
The importance scores are extracted from the slot importance.
Parameter 'importance' must be set to either "accuracy" or "gini".
Returns
Named numeric().
Examples
# Define the Learner
learner = lrn("classif.randomForest", importance = "accuracy")
print(learner)
#>
#> ── <LearnerClassifRandomForest> (classif.randomForest): Random Forest ──────────
#> • Model: -
#> • Parameters: importance=accuracy
#> • Packages: mlr3, mlr3extralearners, and randomForest
#> • Predict Types: [response] and prob
#> • Feature Types: logical, integer, numeric, factor, and ordered
#> • Encapsulation: none (fallback: -)
#> • Properties: importance, multiclass, oob_error, 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)
#>
#> Call:
#> randomForest(formula = formula, data = data, classwt = classwt, cutoff = cutoff, importance = TRUE)
#> Type of random forest: classification
#> Number of trees: 500
#> No. of variables tried at each split: 7
#>
#> OOB estimate of error rate: 20.14%
#> Confusion matrix:
#> M R class.error
#> M 57 13 0.1857143
#> R 15 54 0.2173913
print(learner$importance())
#> V10 V11 V12 V9 V48
#> 0.0236657562 0.0218849275 0.0196456409 0.0112775345 0.0112685116
#> V13 V51 V45 V28 V36
#> 0.0099084587 0.0094799096 0.0090131344 0.0085073092 0.0082405354
#> V27 V46 V37 V49 V18
#> 0.0061936899 0.0059476097 0.0058778123 0.0047354982 0.0041040657
#> V47 V44 V16 V17 V14
#> 0.0040211414 0.0037086102 0.0036445987 0.0036314918 0.0028367118
#> V34 V15 V43 V30 V59
#> 0.0027265327 0.0026681918 0.0025480003 0.0022866843 0.0021391976
#> V40 V1 V7 V52 V22
#> 0.0019944001 0.0019747599 0.0018617517 0.0018192359 0.0017905172
#> V32 V29 V20 V35 V5
#> 0.0017847207 0.0017035516 0.0016594961 0.0016307288 0.0014964652
#> V26 V23 V53 V19 V39
#> 0.0014846096 0.0013648152 0.0012856937 0.0012751547 0.0012109509
#> V31 V55 V24 V21 V2
#> 0.0011992791 0.0011295395 0.0011070465 0.0010759565 0.0009899498
#> V6 V41 V33 V25 V42
#> 0.0009510100 0.0009345773 0.0009280118 0.0008412527 0.0007994251
#> V54 V60 V8 V3 V58
#> 0.0006155597 0.0005396103 0.0005006996 0.0003126762 0.0002952348
#> V50 V56 V4 V57 V38
#> 0.0001667621 -0.0000397199 -0.0001113896 -0.0004923574 -0.0005164539
# Make predictions for the test rows
predictions = learner$predict(task, row_ids = ids$test)
# Score the predictions
predictions$score()
#> classif.ce
#> 0.1449275