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: 19.42%
#> Confusion matrix:
#> M R class.error
#> M 66 9 0.12000
#> R 18 46 0.28125
print(learner$importance())
#> V11 V48 V12 V10 V49
#> 0.0274410257 0.0150082443 0.0140897656 0.0127151217 0.0127024964
#> V45 V51 V46 V28 V9
#> 0.0097565741 0.0093218080 0.0076787641 0.0073146800 0.0072498912
#> V27 V47 V31 V35 V36
#> 0.0071790705 0.0064309095 0.0050554613 0.0048762964 0.0045719307
#> V13 V22 V23 V34 V5
#> 0.0043655071 0.0042829971 0.0041351848 0.0040696380 0.0040377592
#> V21 V32 V16 V20 V8
#> 0.0035493755 0.0028141939 0.0027896539 0.0027795633 0.0027705667
#> V4 V2 V15 V18 V44
#> 0.0027203103 0.0026669743 0.0025414005 0.0023792122 0.0022941025
#> V52 V59 V26 V6 V54
#> 0.0019105738 0.0017200181 0.0017077635 0.0016685721 0.0016229313
#> V17 V3 V1 V30 V24
#> 0.0013335560 0.0013320355 0.0012857663 0.0012295489 0.0012041792
#> V58 V33 V38 V55 V60
#> 0.0011548302 0.0010744409 0.0009503307 0.0009336425 0.0007873398
#> V50 V42 V29 V53 V25
#> 0.0007077532 0.0007044190 0.0007029965 0.0005520869 0.0005369241
#> V39 V14 V7 V57 V41
#> 0.0004795967 0.0004607807 0.0002708844 0.0002309180 0.0002176908
#> V19 V37 V43 V56 V40
#> 0.0001879547 0.0001835097 0.0001157940 -0.0003531379 -0.0006577003
# Make predictions for the test rows
predictions = learner$predict(task, row_ids = ids$test)
# Score the predictions
predictions$score()
#> classif.ce
#> 0.2028986