有类似的问题被提出,然而回答中的链接指向随机森林示例,在我的情况下似乎不起作用。
这里是我正在尝试做的示例:
gbmGrid <- expand.grid(interaction.depth = c(5, 9),
n.trees = (1:3)*200,
shrinkage = c(0.05, 0.1))
fitControl <- trainControl(
method = "cv",
number = 3,
classProbs = TRUE)
gbmFit <- train(strong~.-Id-PlayerName, data = train[1:10000,],
method = "gbm",
trControl = fitControl,
verbose = TRUE,
tuneGrid = gbmGrid)
gbmFit
一切顺利,我得到了最佳参数。现在如果进行预测:
predictStrong = predict(gbmFit, newdata=train[11000:50000,])
我得到了一个二进制的预测向量,这很好:
[1] 0 1 0 0 1 0 0 0 0 0 0 0 1 1 0 0 1 1 1 0 0 0 1 ...
然而,当我尝试获取概率时,出现了错误:
predictStrong = predict(gbmFit, newdata=train[11000:50000,], type="prob")
Error in `[.data.frame`(out, , obsLevels, drop = FALSE) :
undefined columns selected
问题出在哪里?
附加信息:
traceback()
5: stop("undefined columns selected")
4: `[.data.frame`(out, , obsLevels, drop = FALSE)
3: out[, obsLevels, drop = FALSE]
2: predict.train(gbmFit, newdata = train[11000:50000, ], type = "prob")
1: predict(gbmFit, newdata = train[11000:50000, ], type = "prob")
版本:
R version 3.1.0 (2014-04-10) -- "Spring Dance"
Copyright (C) 2014 The R Foundation for Statistical Computing
Platform: x86_64-unknown-linux-gnu (64-bit)
caret version: 6.0-29
编辑:
我也看到了这个主题,虽然我的变量名中有几个带下划线的变量名,但我并没有收到关于变量名的错误提示,我认为这是有效的,因为我使用了make.names
并得到与原始名称相同的名称。
colnames(train) == make.names(colnames(train))
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
str(train$strong)
的结果。我猜测你正在进行回归(而不是分类),因为预测值似乎是数字。对于分类,将strong
设为一个因子,其级别不是 "0" 和 "1"。 - topepo