【发布时间】:2016-08-08 11:35:37
【问题描述】:
我正在尝试使用带有 xgboost 的 R 来研究我的模型。训练模型通常效果很好,但插入符号是度量的一些问题。
我试图为一个类列设置一个因子,但仍然没有结果。
我的数据
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
为什么会这样?
【问题讨论】:
-
rmse 用于连续因变量
-
@user20650 你能建议我应该改变什么吗?我从一个具有相同分类问题的站点中获取了这个示例。好像我错过了一些点
-
我对插入符号不熟悉,但是看着
?train,看起来metric参数被设置为rmse(metric = ifelse(is.factor(y), "Accuracy", "RMSE"),。所以我会尝试将我的结果设置为train.y <- factor(train$TARGET)或明确设置metric="Accuracy"。 -
train.y <- lapply(train.y, factor)我怀疑可能不会像您期望的那样:看看lapply(1:10, factor) -
@user20650 如果我这样做
train.y <- factor(train$TARGET),那么我会收到At least one of the class levels is not a valid R variable name的错误