This document describes how to use H2O's generated POJO models.
About generated models
H2O-generated POJO models are intended to be easily embeddable in any Java environment.
The only compilation and runtime dependency for a generated model is the h2o-genmodel.jar
file produced as the build output of this package.
If you are just getting started, refer to the Quick start section below first,
then look at the documentation for the hex.genmodel.easy package.
The following topics are covered here:
Quick start
Step 1 (in terminal window 1): Start H2O
$ java -jar h2o.jar
Step 2 (in a web browser): Build model
- Go to http://localhost:54321
- Click "view example Flows" near the right edge of the screen. Refer to the following screenshot:
- Click "GBM_Airlines_Classification.flow"
- If a confirmation prompt appears, click "Load Notebook"
- From the "Flow" menu, choose "Run all cells"
- Scroll down and find the "Model" cell in the notebook. Click the Download POJO button (shown in the following screenshot):
- NOTE: The instructions below assume the POJO model was downloaded to the "Downloads" folder.
Step 3 (in a *new* terminal window - H2O must still be running in the first terminal window): Download model pieces
$ mkdir experiment
$ cd experiment
$ mv ~/Downloads/gbm_pojo_test.java .
$ curl http://localhost:54321/3/h2o-genmodel.jar > h2o-genmodel.jar
Step 4 (in terminal window 2): Create the main program
Create a new file called main.java ( `vim main.java`) with the following contents:
{@code
import java.io.*;
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);
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) {
System.out.print(",");
}
System.out.print(p.classProbabilities[i]);
}
System.out.println("");
}
}
}
Step 5 (in terminal window 2): Compile and Run
$ javac -cp h2o-genmodel.jar -J-Xmx2g -J-XX:MaxPermSize=128m gbm_pojo_test.java main.java
$ java -cp .:h2o-genmodel.jar main
The following output displays:
Label (aka prediction) is flight departure delayed: YES
Class probabilities: 0.4790490513429604,0.5209509486570396
Extracting generated models from H2O
Generated models can be extracted from H2O in the following ways:
From the H2O Flow Web UI:
When viewing a model, click the "Download POJO" button at the top of the model cell
(refer to the example in the "Quick Start" section).
You can also preview the POJO inside Flow, but it will only show the first 1000
lines or so in the web browser, truncating large models.
From R:
The following code snippet represents an example of H2O building a model and downloading its
corresponding POJO from an R script:
{@code
library(h2o)
h2o.init()
path = system.file("extdata", "prostate.csv", package = "h2o")
h2o_df = h2o.importFile(path)
h2o_df$CAPSULE = as.factor(h2o_df$CAPSULE)
model = h2o.glm(y = "CAPSULE",
x = c("AGE", "RACE", "PSA", "GLEASON"),
training_frame = h2o_df,
family = "binomial")
h2o.download_pojo(model)
}
From Python:
The following code snippet represents an example of H2O building a model and downloading its
corresponding POJO from a Python script:
{@code
import h2o
h2o.init()
path = h2o.system_file("prostate.csv")
h2o_df = h2o.import_file(path)
h2o_df['CAPSULE'] = h2o_df['CAPSULE'].asfactor()
model = h2o.glm(y = "CAPSULE",
x = ["AGE", "RACE", "PSA", "GLEASON"],
training_frame = h2o_df,
family = "binomial")
h2o.download_pojo(model)
}
From Java:
TODO: provide pointer of doing this directly from Java
From Sparkling Water:
TODO: provide pointer of doing this from Sparkling Water
Use cases
The following use cases are demonstrated with code examples:
Reading new data from a CSV file and predicting on it
The {@link hex.genmodel.tools.PredictCsv} class is used by the H2O test harness to make
predictions on new data points.
Getting a new observation from a JSON request and returning a prediction
TODO: write an example for this
A user-defined-function called directly from hive
See the H2O-3 training github repository