Classification Random Forest Learner
mlr_learners_classif.randomForest.Rd
Random 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
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 = mlr3::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 = mlr3::tsk("sonar")
# Create train and test set
ids = mlr3::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: 18.71%
#> Confusion matrix:
#> M R class.error
#> M 65 9 0.1216216
#> R 17 48 0.2615385
print(learner$importance())
#> V11 V28 V12 V49 V48
#> 2.561690e-02 1.551117e-02 1.374209e-02 1.199957e-02 1.139715e-02
#> V52 V9 V47 V10 V27
#> 9.994226e-03 9.775650e-03 9.566746e-03 7.980612e-03 6.295587e-03
#> V45 V43 V16 V51 V46
#> 6.001118e-03 5.716601e-03 5.332084e-03 5.315305e-03 5.252474e-03
#> V5 V23 V17 V21 V15
#> 5.010737e-03 4.973872e-03 4.396738e-03 4.250232e-03 3.888349e-03
#> V4 V42 V44 V20 V13
#> 3.881554e-03 3.399768e-03 3.244855e-03 2.942757e-03 2.895556e-03
#> V36 V22 V18 V30 V55
#> 2.616078e-03 2.498035e-03 2.239274e-03 2.070261e-03 1.783599e-03
#> V2 V53 V14 V1 V29
#> 1.772114e-03 1.647427e-03 1.641110e-03 1.585866e-03 1.561360e-03
#> V35 V37 V8 V31 V26
#> 1.499517e-03 1.498270e-03 1.378708e-03 1.323586e-03 1.256396e-03
#> V19 V38 V6 V24 V40
#> 9.929197e-04 9.447357e-04 8.506525e-04 8.267421e-04 6.261365e-04
#> V41 V54 V3 V33 V25
#> 5.979489e-04 5.825278e-04 5.419922e-04 4.777987e-04 3.720668e-04
#> V34 V59 V58 V60 V56
#> 2.861834e-04 2.738708e-04 2.347632e-04 1.565367e-04 6.887820e-05
#> V50 V32 V7 V57 V39
#> 4.042314e-05 -8.161016e-05 -2.484872e-04 -2.512960e-04 -5.943839e-04
# Make predictions for the test rows
predictions = learner$predict(task, row_ids = ids$test)
# Score the predictions
predictions$score()
#> classif.ce
#> 0.1594203