Как выбрать лучшее значение ntree в поиске каретки? - PullRequest
2 голосов
/ 17 апреля 2020

Я настроил параметры вручную, чтобы найти лучшее ntree:

bestMtry <- 3
control <- trainControl(method = 'repeatedcv',
                                number = 10,
                                repeats = 3,
                                search = 'grid')


storeMaxtrees <- list()
tuneGrid <- expand.grid(.mtry = bestMtry)
for (ntree in c(1000, 1500, 2000)) {
  set.seed(291)
  rf.maxtrees <- train(survived ~ .,
                       data = trainingSet,
                       method = "rf",
                       metric = "Accuracy",
                       tuneGrid = tuneGrid,
                       trControl = control,
                       importance = TRUE,
                       nodesize = 14,
                       maxnodes = 24,
                       ntree = ntree)
  key <- toString(ntree)
  storeMaxtrees[[key]] <- rf.maxtrees
}
resultsTree <- resamples(storeMaxtrees)
summary(resultsTree)

Вывод:

Call:
summary.resamples(object = resultsTree)

Models: 1000, 1500, 2000 
Number of resamples: 30 

Accuracy 
          Min.   1st Qu.    Median      Mean   3rd Qu.      Max. NA's
1000 0.7865169 0.8181818 0.8305031 0.8335064 0.8498787 0.8764045    0
1500 0.7865169 0.8181818 0.8305031 0.8319913 0.8522727 0.8764045    0
2000 0.7865169 0.8181818 0.8305031 0.8327446 0.8522727 0.8764045    0

Kappa 
          Min.   1st Qu.    Median      Mean   3rd Qu.      Max. NA's
1000 0.2700461 0.4243663 0.4786274 0.4753027 0.5252316 0.6281808    0
1500 0.2700461 0.4218811 0.4710053 0.4705338 0.5270828 0.6281808    0
2000 0.2700461 0.4218811 0.4786274 0.4721715 0.5270828 0.6281808    0

Исходя из вывода, я могу понять, что 2000 является лучшим значением для ntree на основе Точность и каппа. Я хочу динамически хранить лучшее значение ntree (2000). Есть ли способ что-то вроде best_ntree <- resultsTree.bestTune?

1 Ответ

2 голосов
/ 17 апреля 2020

Вы можете сохранить результаты из вызова summary (), например:

bestMtry <- 3
control <- trainControl(method = 'repeatedcv',number = 5)
data = MASS::Pima.tr                                

storeMaxtrees <- list()
tuneGrid <- expand.grid(.mtry = bestMtry)
for (ntree in c(1000, 1500, 2000)) {
  set.seed(291)
  rf.maxtrees <- train(type ~ .,
                       data = data,
                       method = "rf",
                       metric = "Accuracy",
                       tuneGrid = tuneGrid,
                       trControl = control,
                       importance = TRUE,
                       nodesize = 14,
                       maxnodes = 24,
                       ntree = ntree)
  key <- toString(ntree)
  storeMaxtrees[[key]] <- rf.maxtrees
}
resultsTree <- resamples(storeMaxtrees)

Мы можем взять тот, который имеет максимальную среднюю точность:

res = summary(resultsTree)
res$models[which.max(res$statistics$Accuracy[,"Mean"])]
[1] "1500"

Вы можете преобразовать 1500 в моем примере в цифру c ...

...