6
votes

I'm trying to save all the models from an h2o.automl as part of the h2o package. Currently I am able to save a single model using h2o.saveModel(aml@leader, path = "/home/data/user").

How can I save all the models?

Here is my attempt on a sample dataset:

 library(h2o)
 h2o.init()
 prostate.hex <- h2o.importFile(path = paste("https://raw.github.com",
    "h2oai/h2o-2/master/smalldata/logreg/prostate.csv", sep = "/"),
    destination_frame = "prostate.hex")

Get data from github or import via readr:

 library(readr)
 prostate <- read_csv("/home/data/user/prostate.csv")

 prostate.hex<- as.h2o(prostate, "prostate.hex")

 aml <- h2o.automl(y = "CAPSULE", x = c("AGE","RACE","PSA","DCAPS"),
    training_frame = prostate.hex,
    max_runtime_secs = 180,
    exclude_algos = c("StackedEnsemble")
    )

Now I'm trying to save the models within aml:

mod_ids <- as_tibble(aml@leaderboard$model_id)

Now I can't figure out how to save the models:

 for(i in 1:nrow(mod_ids)) {
   print(mod_ids[i,])
   #h2o.saveModel(object = aml@leaderboard[[i]], "/home/data/user/")
 }

Here is what I've tried:

tutorial automl

H2O AUTOML: How to save reuse and build on top of existing automl models

1

1 Answers

6
votes

Try this, it'll do your job:

 for(i in 1:nrow(mod_ids)) {

    aml1 <- h2o.getModel(aml@leaderboard[i, 1]) # get model object in environment
    h2o.saveModel(object = aml1, "C:/Users/sm/Documents/stack/models") # pass that model object to h2o.saveModel as an argument

  }