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, randomForest
#> * Predict Types: [response], prob
#> * Feature Types: logical, integer, numeric, factor, ordered
#> * Properties: importance, multiclass, oob_error, twoclass, weights
# 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: 17.99%
#> Confusion matrix:
#> M R class.error
#> M 67 8 0.1066667
#> R 17 47 0.2656250
print(learner$importance())
#> V11 V12 V36 V9 V37
#> 2.701970e-02 2.102930e-02 1.533491e-02 9.249568e-03 8.564426e-03
#> V51 V45 V49 V35 V21
#> 8.502019e-03 6.675853e-03 6.351720e-03 6.117027e-03 5.657142e-03
#> V10 V48 V46 V28 V20
#> 5.573533e-03 5.506034e-03 5.241043e-03 5.187344e-03 4.712465e-03
#> V13 V43 V47 V14 V27
#> 4.431181e-03 4.310211e-03 4.146777e-03 3.820411e-03 3.696917e-03
#> V44 V1 V16 V15 V52
#> 3.606418e-03 3.366482e-03 3.286341e-03 3.127338e-03 3.014141e-03
#> V4 V5 V34 V8 V18
#> 2.988543e-03 2.929859e-03 2.626060e-03 2.624030e-03 2.362636e-03
#> V32 V31 V17 V42 V19
#> 2.223753e-03 2.205070e-03 2.106189e-03 2.062113e-03 1.946484e-03
#> V59 V3 V39 V22 V6
#> 1.926465e-03 1.908904e-03 1.650690e-03 1.395143e-03 1.379314e-03
#> V25 V24 V23 V26 V30
#> 1.304881e-03 1.274995e-03 1.156992e-03 1.094499e-03 1.042638e-03
#> V2 V29 V58 V56 V55
#> 9.716408e-04 8.558301e-04 8.108714e-04 8.041240e-04 7.781107e-04
#> V33 V50 V41 V54 V57
#> 5.477553e-04 5.384663e-04 2.467835e-04 2.397294e-04 2.353618e-04
#> V40 V38 V53 V7 V60
#> 4.737011e-05 6.232633e-06 -2.747339e-05 -6.743501e-04 -9.366648e-04
# Make predictions for the test rows
predictions = learner$predict(task, row_ids = ids$test)
# Score the predictions
predictions$score()
#> classif.ce
#> 0.1594203