5

我正在尝试gbm使用caretR 中的包来训练一个。我最初收到以下错误,并认为这是由于缺少输入,所以我创建了gbmGrid但仍然收到相同的错误消息。

sub4Collect1 <- data.frame(testing$row_id)
> 
> cl <- makeCluster(10, type = "SOCK")
> registerDoSNOW(cl)
> ptm <- proc.time()
> 
> for(i in 2:7){
+ trainClass <- postPrior1[,i]
+ testClass <- postTest1[,i]
+ gbmGrid <- expand.grid(.interaction.depth = (1:5) * 2, .n.trees = (1:5)*50, .shrinkage = .1)
+ bootControl <- trainControl(number = 1)
+ set.seed(2)
+ gbmFit <- train(prePrior1[,-c(2,60,61,161)], trainClass, method = "gbm", tuneLength = 5,
+ trControl = bootControl
+ ##, scaled = FALSE
+ , tuneGrid = gbmGrid 
+ )
+ pred1 <- predict(gbmFit$finalModel, newdata = preTest1[,-c(2,60,61,161)])
+ sub4Collect1 <- cbind(sub4Collect1, pred1)
+ print(i)
+ flush.console()
+ }
Iter   TrainDeviance   ValidDeviance   StepSize   Improve
     1        0.0000            -nan     0.1000    0.0000
     2        0.0000            -nan     0.1000    0.0000
     3        0.0000            -nan     0.1000    0.0000
     4        0.0000            -nan     0.1000    0.0000
     5        0.0000            -nan     0.1000    0.0000
     6        0.0000            -nan     0.1000    0.0000
     7        0.0000            -nan     0.1000    0.0000
     8        0.0000            -nan     0.1000    0.0000
     9        0.0000            -nan     0.1000    0.0000
    10        0.0000            -nan     0.1000    0.0000
    50        0.0000            -nan     0.1000    0.0000

Error in n.trees[n.trees > object$n.trees] <- object$n.trees : 
  argument "n.trees" is missing, with no default
> stopCluster(cl)
> timee4 <- proc.time() - ptm
> timee4 
   user  system elapsed 
  3.563   0.306  14.472 

有什么建议么?

4

3 回答 3

6

predict() 函数的正确代码需要从 gbmFit$finalModel 对象手动输入 .n.trees 参数,如下所示:

    pred1 <- predict(gbmFit$finalModel, newdata = preTest1[,-c(2,60,61,161)], 
              n.trees=gbmFit1$bestTune$.n.trees)
于 2012-03-17T21:30:54.137 回答
1

如果这不起作用:

pred1 <- predict(gbmFit$finalModel, newdata = preTest1[,-c(2,60,61,161)], 
          n.trees=gbmFit1$bestTune$.n.trees)

你可以使用这个:

pred1 <- predict(gbmFit, newdata = preTest1[,-c(2,60,61,161)], 
          n.trees=gbmFit1$n.trees)
于 2015-07-16T14:26:13.973 回答
0

我不认为你需要同时传递tuneLengthtuneGrid参数。尝试其中一种,看看问题是否仍然存在。

于 2012-01-05T19:54:48.880 回答