R:将游侠与插入符,tuneGrid参数一起使用 [英] R: using ranger with caret, tuneGrid argument
问题描述
我正在使用插入符包来分析随机使用 ranger 构建的森林模型.我不知道如何使用tuneGrid参数调优模型参数来调用训练函数.
I'm using the caret package to analyse Random Forest models built using ranger. I can't figure out how to call the train function using the tuneGrid argument to tune the model parameters.
我认为我把tuneGrid参数称为错误,但无法弄清楚为什么它是错误的.任何帮助将不胜感激.
I think I'm calling the tuneGrid argument wrong, but can't figure out why it's wrong. Any help would be appreciated.
data(iris)
library(ranger)
model_ranger <- ranger(Species ~ ., data = iris, num.trees = 500, mtry = 4,
importance = 'impurity')
library(caret)
# my tuneGrid object:
tgrid <- expand.grid(
num.trees = c(200, 500, 1000),
mtry = 2:4
)
model_caret <- train(Species ~ ., data = iris,
method = "ranger",
trControl = trainControl(method="cv", number = 5, verboseIter = T, classProbs = T),
tuneGrid = tgrid,
importance = 'impurity'
)
推荐答案
以下是插入符中游侠的语法:
Here is the syntax for ranger in caret:
library(caret)
在调整参数之前添加.
:
tgrid <- expand.grid(
.mtry = 2:4,
.splitrule = "gini",
.min.node.size = c(10, 20)
)
插入号仅支持这三个,而不支持树的数量.在训练中,您可以指定数字树和重要性:
Only these three are supported by caret and not the number of trees. In train you can specify num.trees and importance:
model_caret <- train(Species ~ ., data = iris,
method = "ranger",
trControl = trainControl(method="cv", number = 5, verboseIter = T, classProbs = T),
tuneGrid = tgrid,
num.trees = 100,
importance = "permutation")
获得可变的重要性:
varImp(model_caret)
#output
Overall
Petal.Length 100.0000
Petal.Width 84.4298
Sepal.Length 0.9855
Sepal.Width 0.0000
要检查是否可以将树木数量设置为1000+,拟合速度会慢很多.更改importance = "impurity"
之后:
To check if this works set number of trees to 1000+ - the fit will be much slower. After changing importance = "impurity"
:
#output:
Overall
Petal.Length 100.00
Petal.Width 81.67
Sepal.Length 16.19
Sepal.Width 0.00
如果它不起作用,我建议从CRAN安装最新的游侠,并从git hub安装插入符号:
If it does not work I recommend installing latest ranger from CRAN and caret from git hub:
devtools::install_github('topepo/caret/pkg/caret')
要训练树的数量,可以将lapply
与由createMultiFolds
或createFolds
创建的固定折叠一起使用.
To train the number of trees you can use lapply
with fixed folds created by createMultiFolds
or createFolds
.
编辑:尽管以上示例适用于插入符号软件包6.0-84,但使用不带点的超级参数名称也适用.
EDIT: while the above example works with caret package version 6.0-84, using the names of hyper parameters without dots works as well.
tgrid <- expand.grid(
mtry = 2:4,
splitrule = "gini",
min.node.size = c(10, 20)
)
这篇关于R:将游侠与插入符,tuneGrid参数一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!