customRF <- list(type = "Classification", library = "randomForest", loop = NULL)
customRF$parameters <- data.frame(parameter = c("mtry", "ntree"), class =
rep("numeric", 2), label = c("mtry", "ntree"))
customRF$grid <- function(x, y, len = NULL, search = "grid") {}
customRF$fit <- function(x, y, wts, param, lev, last, weights, classProbs, ...)
{randomForest(x,y, mtry = param$mtry, ntree=param$ntree, ...)
}
customRF$predict <- function(modelFit, test, preProc = NULL, submodels = NULL){
predict(modelFit, test)}
customRF$prob <- function(modelFit, test, preProc = NULL, submodels = NULL){
predict(modelFit, test, type = "prob")}
customRF$sort <- function(x){x[order(x[,1]),]}
customRF$levels <- function(x){ x$classes}
control <- trainControl(method="repeatedcv", number=10,
repeats=3,search="grid")
tunegrid <- expand.grid(.mtry=c(1:13), .ntree=c(500, 700, 900, 1000))
custom <- train(disease~., data=cardi, method=customRF, metric=metric,
tuneGrid=tunegrid, trControl=control, na.action = na.exclude)
summary(custom)
plot(custom)
我在处理此代码时遇到了很多麻烦,因此它绘制了.mtry = c(1:13)并为四个.ntree选项中的每一个都有单独的一行。
control <- trainControl(method="repeatedcv", number=10, repeats=3,
search="grid")
tunegrid <- expand.grid(.mtry=c(1:13))
rf_gridsearch <- train(disease~., data=cardi, method="rf", metric=metric,
tuneGrid=tunegrid, trControl=control, na.action=na.exclude)
plot(rf_gridsearch)
上面的代码将打印以下图像,该图像几乎在那里,但是我想对每个ntree选项重复该过程,以便最佳地优化randomForest的参数。任何帮助将不胜感激!
原始代码为:https://machinelearningmastery.com/tune-machine-learning-algorithms-in-r/#comment-457883