R Metric RMSE 不适用于分类模型

R Metric RMSE not applicable for classification models

我正在尝试使用 R 和 xgboost 研究我的模型。训练模型总体上运行良好,但对于插入符,它是度量的一些问题。

我尝试为 class 列设置一个因子,但仍然没有结果。

我的数据

ID  var1var2TARGET
1   5   0   1
2   4   3   1
3   4   2   0
4   3   1   0
5   2   4   1
6   1   2   1
7   5   3   1
8   4   1   0
9   4   1   0
10  2   4   1
11  5   5   1

为此我做

train <- read.csv()
train.y <- train$TARGET
train$TARGET <- NULL
train$ID <- NULL
train.y <- lapply(train.y, factor)

然后我准备模型参数

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 函数

xgb_train_1 = train(
  x = train,
  y = train.y,
  trControl = xgb_trcontrol_1,
  tuneGrid = xgb_grid_1,
  method = "xgbTree"
)

它给了我

Error in train.default(x = train, y = train.y, trControl = xgb_trcontrol_1,  : 
  Metric RMSE not applicable for classification models

为什么会这样?

您应该尝试将 train.y <- lapply(train.y, factor) 更改为 train.y <- factor(train.y, labels = c("yes", "no"))

caret 通常会在标签为 0 或 1 时报错,因此请尝试更改它。