@@ -103,11 +103,20 @@ crossValPlan <- vtreat::kWayStratifiedY(nrow(titanic_train),
103103evaluateModelingProcedure <- function (xMatrix , outcomeV , crossValPlan ) {
104104 preds <- rep(NA_real_ , nrow(xMatrix ))
105105 for (ci in crossValPlan ) {
106- model <- xgboost(data = xMatrix [ci $ train , ],
106+ nrounds <- 1000
107+ cv <- xgb.cv(data = xMatrix [ci $ train , ],
107108 label = outcomeV [ci $ train ],
108109 objective = ' binary:logistic' ,
109- nrounds = 1000 ,
110- verbose = 0 )
110+ nrounds = nrounds ,
111+ verbose = 0 ,
112+ nfold = 5 )
113+ # nrounds <- which.min(cv$evaluation_log$test_rmse_mean) # regression
114+ nrounds <- which.min(cv $ evaluation_log $ test_error_mean ) # classification
115+ model <- xgboost(data = xMatrix [ci $ train , ],
116+ label = outcomeV [ci $ train ],
117+ objective = ' binary:logistic' ,
118+ nrounds = nrounds ,
119+ verbose = 0 )
111120 preds [ci $ app ] <- predict(model , xMatrix [ci $ app , ])
112121 }
113122 preds
@@ -249,10 +258,19 @@ for(ci in crossValPlan) {
249258 sf <- tplan $ scoreFrame
250259 newvars <- sf $ varName [sf $ sig < 1 / nrow(sf )]
251260 trainVtreat <- cfe $ crossFrame [ , c(newvars , outcome ), drop = FALSE ]
261+ nrounds <- 1000
262+ cv <- xgb.cv(data = as.matrix(trainVtreat [, newvars , drop = FALSE ]),
263+ label = trainVtreat [[outcome ]]== 1 ,
264+ objective = ' binary:logistic' ,
265+ nrounds = nrounds ,
266+ verbose = 0 ,
267+ nfold = 5 )
268+ # nrounds <- which.min(cv$evaluation_log$test_rmse_mean) # regression
269+ nrounds <- which.min(cv $ evaluation_log $ test_error_mean ) # classification
252270 model <- xgboost(data = as.matrix(trainVtreat [, newvars , drop = FALSE ]),
253271 label = trainVtreat [[outcome ]]== 1 ,
254272 objective = ' binary:logistic' ,
255- nrounds = 1000 ,
273+ nrounds = nrounds ,
256274 verbose = 0 )
257275 appVtreat <- vtreat :: prepare(tplan ,
258276 titanic_train [ci $ app , , drop = FALSE ],
0 commit comments