Alberto Bonsanto Alberto Bonsanto - 1 month ago 13
Java Question

How to consume a H2OModel computed and saved in Python API

I've been reading the H2O documentation for a while, and I haven't found a clear example about how load

trained and saved using the Python API. I was following the next example.

import h2o
from h2o.estimators.naive_bayes import H2ONaiveBayesEstimator

model = H2ONaiveBayesEstimator()
h2o_df = h2o.import_file("")
model.train(y = "IsDepDelayed", x = ["Year", "Origin"],
training_frame = h2o_df,
family = "binomial",
lambda_search = True,
max_active_predictors = 10)
h2o.save_model(model, path=models)

But if you check the official documentation it states that you have to download the model as a
from the flow UI. Is it the only way? or, may I achieve the same result via python? Just for information I show the doc's example below. I really need the some guidance.

import hex.genmodel.easy.RowData;
import hex.genmodel.easy.EasyPredictModelWrapper;
import hex.genmodel.easy.prediction.*;

public class main {
private static String modelClassName = "gbm_pojo_test";

public static void main(String[] args) throws Exception {
hex.genmodel.GenModel rawModel;
rawModel = (hex.genmodel.GenModel) Class.forName(modelClassName).newInstance();
EasyPredictModelWrapper model = new EasyPredictModelWrapper(rawModel);
// By default, unknown categorical levels throw PredictUnknownCategoricalLevelException.
// Optionally configure the wrapper to treat unknown categorical levels as N/A instead:
// EasyPredictModelWrapper model = new EasyPredictModelWrapper(
// new EasyPredictModelWrapper.Config()
// .setModel(rawModel)
// .setConvertUnknownCategoricalLevelsToNa(true));

RowData row = new RowData();
row.put("Year", "1987");
row.put("Month", "10");
row.put("DayofMonth", "14");
row.put("DayOfWeek", "3");
row.put("CRSDepTime", "730");
row.put("UniqueCarrier", "PS");
row.put("Origin", "SAN");
row.put("Dest", "SFO");

BinomialModelPrediction p = model.predictBinomial(row);
System.out.println("Label (aka prediction) is flight departure delayed: " + p.label);
System.out.print("Class probabilities: ");
for (int i = 0; i < p.classProbabilities.length; i++) {
if (i > 0) {


h2o.save_model will save binary model to the provided file system however looking at the Java application above it seems you want to use model into a Java based scoring application.

Because of that you should be using h2o.download_pojo API to save the model to local file system along with genmodel jar file. The API is documented as below:

download_pojo(model, path=u'', get_jar=True)
Download the POJO for this model to the directory specified by path; if path is "", then dump to screen.

:param model: the model whose scoring POJO should be retrieved.
:param path: an absolute path to the directory where POJO should be saved.
:param get_jar: retrieve the h2o-genmodel.jar also.

Once you have download POJO you can use the above sample application to perform the scoring and make sure the POJO class name and the "modelClassName" are same along with model type.