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: 18.71%
#> Confusion matrix:
#> M R class.error
#> M 57 12 0.173913
#> R 14 56 0.200000
print(learner$importance())
#> V11 V9 V12 V48 V49
#> 2.523579e-02 1.991220e-02 1.456923e-02 1.426493e-02 1.184312e-02
#> V10 V28 V13 V47 V51
#> 1.108961e-02 1.098317e-02 9.425265e-03 8.982336e-03 8.172230e-03
#> V45 V52 V46 V27 V43
#> 6.507316e-03 6.250183e-03 5.451653e-03 4.707661e-03 4.585914e-03
#> V16 V20 V21 V37 V18
#> 4.539153e-03 4.521362e-03 4.453315e-03 4.228846e-03 3.147825e-03
#> V36 V17 V15 V44 V31
#> 2.998408e-03 2.880706e-03 2.678012e-03 2.314436e-03 2.178903e-03
#> V19 V30 V32 V14 V35
#> 2.081266e-03 1.993183e-03 1.865241e-03 1.818042e-03 1.751395e-03
#> V2 V6 V4 V29 V5
#> 1.666919e-03 1.635394e-03 1.456663e-03 1.377111e-03 1.323026e-03
#> V42 V38 V50 V22 V1
#> 1.275474e-03 1.124329e-03 1.094446e-03 1.013468e-03 9.646374e-04
#> V39 V8 V54 V23 V26
#> 8.918000e-04 7.856474e-04 7.182793e-04 6.929886e-04 6.925293e-04
#> V59 V7 V53 V55 V25
#> 6.519475e-04 5.497382e-04 5.048283e-04 4.813611e-04 4.013525e-04
#> V40 V58 V24 V34 V41
#> 3.824096e-04 3.633771e-04 3.489969e-04 2.336143e-04 2.048128e-04
#> V60 V56 V3 V57 V33
#> 5.120256e-05 -2.182754e-04 -2.882612e-04 -3.610545e-04 -5.224050e-04
# Make predictions for the test rows
predictions = learner$predict(task, row_ids = ids$test)
# Score the predictions
predictions$score()
#> classif.ce
#> 0.173913