1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| library(caret) checkInstall("xgboost")
numCV <- 3L cvFold <- 10L ratioTrain <- 0.6
numSeeds <- 4L * numCV
set.seed(1) seeds <- replicate(numCV * cvFold + 1L, sample.int(2^31-1, numSeeds), simplify = FALSE)
trIndex <- createDataPartition(iris$Species, 1L, ratioTrain, FALSE) irisTrain <- iris[trIndex, ] irisTest <- iris[setdiff(1L:nrow(iris), trIndex), ]
ctrl <- trainControl("repeatedcv", cvFold, numCV, seeds = seeds)
xgFit1 <- train(Species ~ ., data = irisTrain, trControl = ctrl, method = "xgbTree", num_class = nlevels(iris$Species)) xgFit1
confusionMatrix(iris.Train$Species, predict(xgFit1, iris.Train))
confusionMatrix(iris.Test$Species, predict(xgFit1, iris.Test))
modelLookup("xgbTree")
trGrid <- expand.grid(nrounds = c(50, 100), max_depth = c(10, 12), eta = c(0.1, 0.2), gamma = 0, colsample_bytree = c(0.5, 0.6), min_child_weight = 1)
xgFit2 <- train(Species ~ ., data = iris.Train, trControl = ctrl, method = "xgbTree", tuneGrid = trGrid, num_class = nlevels(iris$Species)) xgFit2
confusionMatrix(iris.Train$Species, predict(xgFit2, iris.Train))
confusionMatrix(iris.Test$Species, predict(xgFit2, iris.Test))
|