h2o中的留一法交叉验证

时间:2019-02-25 17:41:11

标签: r statistics cross-validation h2o

我想检查一下h2o中我很小的df的留一法交叉验证的结果。这是我的输入df:https://drive.google.com/file/d/1UiIkxlHCq1tJZNOH6hQD30gEMaPdmhgh/view?usp=sharing

是否可以在h2o中设置nfolds(即nfolds = nrow(df))参数以获得这种交叉验证? 我无法为nrow(df)= 69设置nfolds> 25。

u$dc=as.factor(u$dc)
train <- as.h2o(u)
model <- h2o.gbm(x= colnames(train)[1:15],
                y="dc", training_frame=train,
                nfolds = 25,
                learn_rate = 0.06,
                ntrees = 90, max_depth = 3,   
                min_rows = 2,
                distribution = "bernoulli")

上面的代码中出现异常:

Error: water.exceptions.H2OIllegalArgumentException:
     Not enough data to create 25 random cross-validation splits. Either reduce nfolds, specify a larger dataset

它被抛出在ModelBuilder.java中:

    at hex.ModelBuilder.cv_makeWeights(ModelBuilder.java:357)
    at hex.ModelBuilder.computeCrossValidation(ModelBuilder.java:276)
    at hex.ModelBuilder$1.compute2(ModelBuilder.java:207)
    at water.H2O$H2OCountedCompleter.compute(H2O.java:1263)
    at jsr166y.CountedCompleter.exec(CountedCompleter.java:468)
    at jsr166y.ForkJoinTask.doExec(ForkJoinTask.java:263)
    at jsr166y.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:974)
    at jsr166y.ForkJoinPool.runWorker(ForkJoinPool.java:1477)
    at jsr166y.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:104)

1 个答案:

答案 0 :(得分:0)

对于提供的包含69个示例的数据集,您需要在h2o.gbm调用中使用以下参数:

nfolds = 69,
fold_assignment = "Modulo"

例如,此完整代码块使用留一法交叉验证运行您的示例,并包括一些额外的行以确认折叠是否已正确分配:

library(h2o)

h2o.init(strict_version_check = FALSE)

u$dc=as.factor(u$dc)
train <- as.h2o(u)
model <- h2o.gbm(x= colnames(train)[1:15],
                 y="dc", training_frame=train,
                 nfolds = 69,
                 fold_assignment = "Modulo",
                 keep_cross_validation_fold_assignment = TRUE, # keep track of fold assignment to confirm leave-one-out
                 learn_rate = 0.06,
                 ntrees = 90, max_depth = 3,   
                 min_rows = 2,
                 distribution = "bernoulli")

folds <- h2o.cross_validation_fold_assignment(model) # get fold assignments
print(folds, n = 69) # print all assignment for the 69 folds
print(h2o.dim(h2o.unique(folds))) # count the number of unique values