Caret and GBM: task 1 failed - “arguments imply differing number of rows”

北城以北 提交于 2021-01-27 14:36:34

问题


I'm trying to run a GBM with caret with the code below:

library(caret)
library(doParallel)
detectCores()
registerDoParallel(detectCores() - 1)

set.seed(668)
in.train <- createDataPartition(y = dat$target, p = 0.80, list = T)

ctrl <- trainControl(method = 'cv', number = 2, classProbs = T, verboseIter = T,
                 summaryFunction = LogLossSummary2)

gbm.grid <- expand.grid(interaction.depth = 10,
                        n.trees = (2:7) * 50,
                        shrinkage = 0.1)

Sys.time()
set.seed(1234)
gbm.fit <- train(target ~., data = otto.new[in.train, ], 
                 method = 'gbm', distribution = 'multinomial', 
                 metric = 'LogLoss', maximize = F, 
                 tuneGrid = gbm.grid, trControl = ctrl,
                 n.minobsinnode = 4, bag.fraction = 0.9)
Sys.time()

However, it fails with the error:

Error in { : 
task 1 failed - "arguments imply differing number of rows: 0, 24754"
In addition: Warning messages:
1: package ‘gbm’ was built under R version 3.0.3 
2: package ‘survival’ was built under R version 3.0.3 
3: package ‘plyr’ was built under R version 3.0.3 

Here's my session info:

> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252     LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] splines   parallel  stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] plyr_1.8.1       gbm_2.1.1        survival_2.38-1  doParallel_1.0.8 iterators_1.0.7  foreach_1.4.2    data.table_1.9.4
[8] caret_6.0-41     ggplot2_1.0.1    Revobase_7.1.0   RevoMods_7.1.0   RevoScaleR_7.1.0 lattice_0.20-27  rpart_4.1-5     

loaded via a namespace (and not attached):
[1] BradleyTerry2_1.0-6 brglm_0.5-9         car_2.0-25          chron_2.3-45        class_7.3-12        codetools_0.2-11   
[7] colorspace_1.2-6    compiler_3.0.2      digest_0.6.8        e1071_1.6-4         grid_3.0.2          gtable_0.1.2       
[13] gtools_3.4.1        lme4_1.1-7          MASS_7.3-37         Matrix_1.1-5         mgcv_1.8-5          minqa_1.2.4        
[19] munsell_0.4.2       nlme_3.1-120        nloptr_1.0.4        nnet_7.3-9           pbkrtest_0.4-2      proto_0.3-10       
[25] quantreg_5.11       Rcpp_0.11.5         reshape2_1.4.1      scales_0.2.4            SparseM_1.6         stringr_0.6.2      
[31] tools_3.0.2        

I've noticed that this issue happens intermittently and seems to be reduced when I ensure that my dataset is a multiple of k-folds. (In the case above, my dataset has 49506 rows). Nonetheless, it seems to crop up every now and then. Has anyone else encountered this and come across a way to prevent it?


回答1:


I was facing the same problem then I realized that one of my rows had an "NA" . The model did not make a prediction for it and thus it was missing 1 row in when I ran the predict(). After 2 days worth of wasted time, I imputed the NA and reran the exact same script. Worked fine. So please try imputing the NAs or nulls in your data. (BTW: I had some "null" also which it was reading as factors so please look for those as well) . Please let us know if it solved the problem for you or not.




回答2:


I was having the same problem and I was trying to reconstruct a gbm model I had previously constructed - the code had worked and then stopped working. In addition R would not start every time I tried to start it. It would terminate the session before it started. It would usually start the 2nd time, but I went to start it and it did not start several times in a row, so I followed the instructions here: https://support.rstudio.com/hc/en-us/articles/200534577-Resetting-RStudio-s-State

and renamed my rstudio-desktop file. After I did this I was able to run the gbm model. It should be clear from reading this that I have little idea why this worked, however I would not expect it to work unless you are getting the error message: Error in { : task 1 failed - "arguments imply differing number of rows: 0, (some number)



来源:https://stackoverflow.com/questions/29471784/caret-and-gbm-task-1-failed-arguments-imply-differing-number-of-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!