Ваши итерации дают различия, основанные на случайной выборке, выполненной caret::createDataPartition
. Чтобы сделать это воспроизводимым, вы можете использовать пакет doRNG
, написанный для этой цели - большое спасибо @HenrikB за то, что он меня проинформировал!
Редактировать: исправил функцию foreach (сделал не изменить результат)
invisible(suppressPackageStartupMessages(
lapply(c("data.table", "randomForest", "caret", "foreach",
"doRNG", "rngtools", "doParallel"),
require, character.only = TRUE)))
cores = detectCores()
cl = makeCluster(cores - 1)
registerDoParallel(cl)
res <- foreach(i = 1:50, .packages = c("caret", "randomForest", "data.table"), .combine = rbind,
.options.RNG=1234) %dorng% {
trainIndex <- caret::createDataPartition(iris$Species, p = 0.5, list = FALSE)
irisTrain <- iris[ trainIndex,]
irisTest <- iris[-trainIndex,]
model <- randomForest(x = irisTrain[,c(1:4)], y = irisTrain[,5], importance = TRUE,
replace = TRUE, mtry = 4, ntree = 500, na.action=na.omit,
do.trace = 100, type = "classification")
pred_test <- predict(model, irisTest[,c(1:4)])
con.mat_test <- confusionMatrix(pred_test, irisTest[,5], mode ="everything")
return(data.table(Iteration=i, t(con.mat_test[["overall"]])))
}
stopCluster(cl)
seeds <- attr(res, 'rng')
res[which.min(Accuracy),]
#> Iteration Accuracy Kappa AccuracyLower AccuracyUpper AccuracyNull
#> 1: 6 0.9066667 0.86 0.8171065 0.9616461 0.3333333
#> AccuracyPValue McnemarPValue
#> 1: 4.39803e-25 NaN
best.seed <- res[which.min(Accuracy),]$Iteration
rngtools::setRNG(seeds[[best.seed]])
trainIndex <- caret::createDataPartition(iris$Species, p = 0.5, list = FALSE)
irisTrain <- iris[ trainIndex,]
irisTest <- iris[-trainIndex,]
model <- randomForest(x = irisTrain[,c(1:4)], y = irisTrain[,5], importance = TRUE,
replace = TRUE, mtry = 4, ntree = 500, na.action=na.omit,
do.trace = 100, type = "classification")
#> ntree OOB 1 2 3
#> 100: 4.00% 0.00% 4.00% 8.00%
#> 200: 2.67% 0.00% 4.00% 4.00%
#> 300: 2.67% 0.00% 4.00% 4.00%
#> 400: 2.67% 0.00% 4.00% 4.00%
#> 500: 4.00% 0.00% 4.00% 8.00%
pred_test <- predict(model, irisTest[,c(1:4)])
con.mat_test <- confusionMatrix(pred_test, irisTest[,5], mode ="everything")
con.mat_test[["overall"]]
#> Accuracy Kappa AccuracyLower AccuracyUpper AccuracyNull
#> 9.066667e-01 8.600000e-01 8.171065e-01 9.616461e-01 3.333333e-01
#> AccuracyPValue McnemarPValue
#> 4.398030e-25 NaN
Создано в 2020-05-05 пакетом Представление (v0.3.0)