如何在 R 中创建一个产生多数票的函数?我想对 XGBoost 模型的一些参数进行网格搜索。我的目标是通过网格搜索中每个分类器组合的多数投票来选择最佳 XGBoost 模型。
library(datasets)
library(xgboost)
library(caret)
library(dplyr)
library(caTools)
data(iris)
iris$ID<- 1:nrow(iris)
for(i in iris$ID) {
mytrain <- iris %>%
filter(ID != i) %>%
select(!ID)
mytest <- iris %>%
filter(ID == i) %>%
select(!ID)
}
ctrl <- trainControl(method = "LOOCV",
number = 10,
classProbs = TRUE,
#summaryFunction = MajVot, <- should I insert here the Maj voting function?
allowParallel=TRUE,
verboseIter = F,
savePredictions=T)
tune_grid <- expand.grid(nrounds=c(100),
max_depth = c(1:10),
eta = c(0.01,0.05),
gamma = c(0.01,0.5),
colsample_bytree = c(0.75),
subsample = c(0.50),
min_child_weight = c(0,10))
m <- caret::train(Species ~ .,
data = mytrain,
method = "xgbTree",
metric="ROC",
tuneGrid = tune_grid,
verbose = FALSE,
trControl = ctrl)