输出' h2o'函数结果为向量

时间:2015-11-25 20:28:53

标签: r gbm h2o

我有一个与此类似的问题(link),但我的问题是指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的列表/结构/数组,然后我可以对整个数据集进行预测,并剔除信息量较少的数据集。我正在努力创造一个体面的,随机的森林和#34;&#34;遵循Eugene Tuv的步骤。我没有他的代码。

问题:
是否有正确的方法将h2o gbm和几百个好友一起打包到r中的一个商店?

如果在java中抛弃引用的对象,使这种方法变得不可行,那么使用&#39; gbm&#39;是否存在可行的变体。图书馆?如果我最终不得不使用gbm,那么与h2o的速度差异是什么?

1 个答案:

答案 0 :(得分: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))

将把所有结果放在一个列表中