0

我有一个与此类似的问题(链接),除了我的问题是指 java 工具“h2o”及其与“r”的连接。

特别是我想将“h2o”对象分配给向量(或结构或数组)的一部分。我想循环并存储其中的几个而不必手动枚举。

我在链接上尝试了解决方案,但它不适用于“h2o”对象。

这是我更长的代码(疣和所有):

#libraries
library(h2o)      #for tree control

#specify data
mydata <- iris[iris$Species!="setosa",]
mydata$Species <- as.factor(as.character(mydata$Species))

#most informative variable is petal length
x1 <- mydata$Petal.Length
x2 <- mydata$Petal.Width

#build classes
C <- matrix(0,nrow=length(x1),ncol=1)
idx1 <- which(mydata$Species == "versicolor",arr.ind=T)
idx2 <- which(mydata$Species != "versicolor",arr.ind=T)
C[idx1] <- +1
C[idx2] <- 0

#start h2o
localH2O = h2o.init(nthreads = -1)

# Run regression GBM on iris.hex data
irisPath = system.file("extdata", "iris.csv", package="h2o")
iris.hex = h2o.uploadFile(localH2O, path = irisPath)
names(iris.hex) <- c("Sepal.Length",
                     "Sepal.Width",
                     "Petal.Length",
                     "Petal.Width",
                     "Species" )

iris2 <- iris
iris2$Species <- unclass(iris$Species)
iris2.hex <- as.h2o(iris2)
iris.hex$Species <- as.factor(iris2.hex$Species)

independent <- c("Sepal.Length","Sepal.Width","Petal.Length","Petal.Width")
dependent <- "Species"

mare <- numeric()
mae <- matrix(1,nrow=10,ncol=1)

est2.h2o <- vector(mode="list", length=150)

for (i in 1:150){

     est2.h2o[[i]] <- h2o.gbm(y = dependent, 
                         x = independent, 
                         training_frame = iris.hex,
                         distribution="AUTO",
                         ntrees = i, max_depth = 3, min_rows = 2,
                         learn_rate = 0.5)


     pred <- h2o.predict(est2.h2o,newdata=iris.hex)

     err <- iris2$Species-(as.data.frame(pred)$predict+1)

     mae[i] <- mean(abs(err))
     mare[i] <- mean(abs(err)/iris2$Species)

     print(c(i,log10(mae[i])))

}

我得到的错误是:

Error in paste0("Predictions/models/", object@model_id, "/frames/", newdata@frame_id) : 
  trying to get slot "model_id" from an object of a basic class ("list") with no slots

我的意图是拥有一个 GBM 的列表/结构/数组,然后我可以针对整个数据集运行预测,并剔除信息量较少的那些。我正在尝试按照 Eugene Tuv 的步骤制作一个体面的“gbt 随机森林”。我没有他的密码。

问题:
是否有适当的方法将 h2o gbm 及其几个(数百个)伙伴打包到 r 中的单个商店中?

如果引用的对象在 java 中被丢弃,使这种方法不可行,是否有使用“gbm”库的可行变体?如果我最终不得不使用 gbm,与 h2o 的速度差异是多少?

4

1 回答 1

1

在没有看到您正在使用的确切参数的情况下,我的猜测是问题在于您正在使用sapply而不是lapply.

sapply经常试图简化结果,这在大多数情况下是好的。但是,如果您想要可以包含任何类型的对象的东西,那么您需要一个列表。

如果我们定义paramListList为一个列表,其中每个条目都是一个包含 h2o.gbm 参数的列表:

前任:

paramListList <- list(list(x = xVALUES1, 
                           y = yVALUES1, 
                           training_frame = tfVALUES1, 
                           model_id = miVALUES1, 
                           checkpoint = checkVALUES1),
                      list(x = xVALUES2, 
                           y = yVALUES2, 
                           training_frame = tfVALUES2, 
                           model_id = miVALUES2, 
                           checkpoint = checkVALUES2),
                     )

那么您可以执行以下操作:

lapply(paramListList, function(paramlist) do.call(h2o.gbm, paramlist))

这会将您的所有结果放在一个列表中

于 2015-11-25T22:29:13.277 回答