mardi 11 décembre 2018

Tune machine learning: Random Forest

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)

I am having a lot of trouble manipulating this code so that it plots the .mtry=c(1:13) and has a separate line for each of the four .ntree options.

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)

The code above prints the following image, which is almost there but I would like to repeat the process for each of the ntree options in order to best optimize the parameters of the randomForest. Any help would be greatly appreciated!

The original code is: https://machinelearningmastery.com/tune-machine-learning-algorithms-in-r/#comment-457883




Aucun commentaire:

Enregistrer un commentaire