ตั้งแต่อินเตอร์เฟซxgboostในcaretมีการเปลี่ยนแปลงเมื่อเร็ว ๆ นี้ที่นี่เป็นสคริปต์ที่ให้คำแนะนำให้ความเห็นอย่างเต็มที่ของการใช้caretการปรับแต่งxgboostHyper-พารามิเตอร์
สำหรับเรื่องนี้ผมจะใช้ข้อมูลการฝึกอบรมจากการแข่งขัน Kaggle "Give Me บางเครดิต"
1. การติดตั้งxgboostแบบจำลอง
ในส่วนนี้เรา:    
- พอดีกับxgboostโมเดลที่มีพารามิเตอร์หลายมิติตามอำเภอใจ
- ประเมินการสูญเสีย (AUC-ROC) โดยใช้การตรวจสอบข้าม ( xgb.cv)
- วางแผนการฝึกอบรมเปรียบเทียบกับการประเมินผลการทดสอบ
นี่คือรหัสที่จะทำ 
library(caret)
library(xgboost)
library(readr)
library(dplyr)
library(tidyr)
# load in the training data
df_train = read_csv("04-GiveMeSomeCredit/Data/cs-training.csv") %>%
  na.omit() %>%                                                                # listwise deletion 
  select(-`[EMPTY]`) %>%
  mutate(SeriousDlqin2yrs = factor(SeriousDlqin2yrs,                           # factor variable for classification
                                   labels = c("Failure", "Success")))
# xgboost fitting with arbitrary parameters
xgb_params_1 = list(
  objective = "binary:logistic",                                               # binary classification
  eta = 0.01,                                                                  # learning rate
  max.depth = 3,                                                               # max tree depth
  eval_metric = "auc"                                                          # evaluation/loss metric
)
# fit the model with the arbitrary parameters specified above
xgb_1 = xgboost(data = as.matrix(df_train %>%
                                   select(-SeriousDlqin2yrs)),
                label = df_train$SeriousDlqin2yrs,
                params = xgb_params_1,
                nrounds = 100,                                                 # max number of trees to build
                verbose = TRUE,                                         
                print.every.n = 1,
                early.stop.round = 10                                          # stop if no improvement within 10 trees
)
# cross-validate xgboost to get the accurate measure of error
xgb_cv_1 = xgb.cv(params = xgb_params_1,
                  data = as.matrix(df_train %>%
                                     select(-SeriousDlqin2yrs)),
                  label = df_train$SeriousDlqin2yrs,
                  nrounds = 100, 
                  nfold = 5,                                                   # number of folds in K-fold
                  prediction = TRUE,                                           # return the prediction using the final model 
                  showsd = TRUE,                                               # standard deviation of loss across folds
                  stratified = TRUE,                                           # sample is unbalanced; use stratified sampling
                  verbose = TRUE,
                  print.every.n = 1, 
                  early.stop.round = 10
)
# plot the AUC for the training and testing samples
xgb_cv_1$dt %>%
  select(-contains("std")) %>%
  mutate(IterationNum = 1:n()) %>%
  gather(TestOrTrain, AUC, -IterationNum) %>%
  ggplot(aes(x = IterationNum, y = AUC, group = TestOrTrain, color = TestOrTrain)) + 
  geom_line() + 
  theme_bw()
นี่คือลักษณะของการทดสอบกับการฝึกอบรม AUC: 

2. การค้นหาพารามิเตอร์ระดับสูงโดยใช้ train
สำหรับการค้นหาพารามิเตอร์หลายค่าเราดำเนินขั้นตอนต่อไปนี้: 
- สร้าง a data.frameด้วยชุดค่าผสมที่ไม่ซ้ำกันของพารามิเตอร์ที่เราต้องการสำหรับรุ่นที่ผ่านการฝึกอบรมมาแล้ว
- ระบุพารามิเตอร์ควบคุมที่ใช้กับการฝึกอบรมของแต่ละรุ่นรวมถึงพารามิเตอร์การตรวจสอบข้ามและระบุว่าความน่าจะเป็นที่คำนวณได้เพื่อให้สามารถคำนวณ AUC ได้   
- ตรวจสอบความถูกต้องและฝึกอบรมโมเดลสำหรับการรวมกันของพารามิเตอร์แต่ละชุดช่วยประหยัด AUC สำหรับแต่ละรุ่น
นี่คือรหัสบางส่วนที่แสดงวิธีการทำ 
# set up the cross-validated hyper-parameter search
xgb_grid_1 = expand.grid(
  nrounds = 1000,
  eta = c(0.01, 0.001, 0.0001),
  max_depth = c(2, 4, 6, 8, 10),
  gamma = 1
)
# pack the training control parameters
xgb_trcontrol_1 = trainControl(
  method = "cv",
  number = 5,
  verboseIter = TRUE,
  returnData = FALSE,
  returnResamp = "all",                                                        # save losses across all models
  classProbs = TRUE,                                                           # set to TRUE for AUC to be computed
  summaryFunction = twoClassSummary,
  allowParallel = TRUE
)
# train the model for each parameter combination in the grid, 
#   using CV to evaluate
xgb_train_1 = train(
  x = as.matrix(df_train %>%
                  select(-SeriousDlqin2yrs)),
  y = as.factor(df_train$SeriousDlqin2yrs),
  trControl = xgb_trcontrol_1,
  tuneGrid = xgb_grid_1,
  method = "xgbTree"
)
# scatter plot of the AUC against max_depth and eta
ggplot(xgb_train_1$results, aes(x = as.factor(eta), y = max_depth, size = ROC, color = ROC)) + 
  geom_point() + 
  theme_bw() + 
  scale_size_continuous(guide = "none")
สุดท้ายคุณสามารถสร้าง bubbleplot สำหรับ AUC ในรูปแบบต่างๆetaและmax_depth:
