Есть несколько проблем с вашим кодом.
- указать правильные имена аргументов
caret::train
не имеет аргумента tune_grid
, а скорее tuneGrid
- вы пытаетесь выполнить классификацию, но вы предоставляете числовой
target
. Вот что говорит вам сообщение об ошибке:
You are trying to do regression and your outcome only has two possible values Are you trying to do classification? If so, use a 2 level factor as your outcome column.
- при публикации минимальных примеров здесь на SO, старайтесь ограничить время вычислений до минимума. В вашем примере это довольно легко сделать, просто уменьшив пространство поиска.
Вот код, который должен работать:
library(caret)
library(xgboost)
data(agaricus.train, package = "xgboost")
data(agaricus.test, package='xgboost')
train <- agaricus.train
test <- agaricus.test
train$label <- ifelse(train$label == 0, "no", "yes") #convert target to character or factor
xgb_grid_1 = expand.grid(
nrounds = 100,
eta = c(0.01, 0.001, 0.0001),
max_depth = c(2, 4, 6, 8, 10),
gamma = 1,
colsample_bytree = 0.6,
min_child_weight = 1,
subsample = 0.75
)
xgb_trcontrol_1 <- trainControl(
method = "cv",
number = 3,
search = "grid",
verboseIter = TRUE,
returnData = FALSE,
returnResamp = "all",
classProbs = TRUE,
summaryFunction = twoClassSummary
)
xgb_train1 <- caret::train(
x = as.matrix(train$data),
y = train$label,
trControl = xgb_trcontrol_1,
tuneGrid = xgb_grid_1,
metric ="ROC",
method = 'xgbTree'
)
#output
eXtreme Gradient Boosting
No pre-processing
Resampling: Cross-Validated (3 fold)
Summary of sample sizes: 4343, 4341, 4342
Resampling results across tuning parameters:
eta max_depth ROC Sens Spec
1e-04 2 0.9963189 0.9780604 0.9656045
1e-04 4 0.9999604 0.9985172 0.9974527
1e-04 6 1.0000000 1.0000000 0.9974527
1e-04 8 1.0000000 1.0000000 0.9974527
1e-04 10 1.0000000 1.0000000 0.9974527
1e-03 2 0.9972687 0.9629358 0.9713391
1e-03 4 0.9999479 0.9985172 0.9974527
1e-03 6 1.0000000 1.0000000 0.9974527
1e-03 8 1.0000000 1.0000000 0.9974527
1e-03 10 1.0000000 1.0000000 0.9977714
1e-02 2 0.9990705 0.9780604 0.9757951
1e-02 4 0.9999674 1.0000000 0.9974527
1e-02 6 1.0000000 1.0000000 0.9977714
1e-02 8 1.0000000 1.0000000 0.9977714
1e-02 10 1.0000000 1.0000000 0.9977714
Tuning parameter 'nrounds' was held constant at a value of 100
Tuning parameter 'gamma' was held constant at a value of 1
Tuning
parameter 'colsample_bytree' was held constant at a value of 0.6
Tuning parameter 'min_child_weight' was held constant at a value of
1
Tuning parameter 'subsample' was held constant at a value of 0.75
ROC was used to select the optimal model using the largest value.
The final values used for the model were nrounds = 100, max_depth = 6, eta = 1e-04, gamma = 1, colsample_bytree = 0.6, min_child_weight
= 1 and subsample = 0.75.