Reputation: 41
I have model built in H2O (say, GLM model) Now, I want to import that model in Python to use for other apps.
How can I do it ?
Upvotes: 4
Views: 4643
Reputation: 6753
Newer versions of H2O have the ability to import MOJOs via the python API:
# re-import saved MOJO
imported_model = h2o.import_mojo(path)
new_observations = h2o.import_file(path='new_observations.csv')
predictions = imported_model.predict(new_observations)
Caution: MOJO cannot be re-imported into python in older H2O versions, which lack the h2o.import_mojo()
function.
So h2o.save_model()
seems to have lost its role - we can use just my_model.save_mojo()
(notice it's not a h2o
method, but a property of the model object), as these files can be used not just for Java apps deployment, but also in python as well (in fact they still use a python-Java bridge for that internally).
Upvotes: 0
Reputation: 2007
Try this:
# build the model
model = H2ODeepLearningEstimator(params)
model.train(params)
# save the model
model_path = h2o.save_model(model=model, path="/tmp/mymodel", force=True)
print(model_path)
/tmp/mymodel/DeepLearning_model_python_1441838096933
# load the model
saved_model = h2o.load_model(model_path)
Upvotes: 2
Reputation: 28913
You need to export the model as a MOJO or POJO (prefer MOJO if your algorithm supports it). This is a Java object, so you need to use Java to run it. There are lots of options for how to do this:
http://docs.h2o.ai/h2o/latest-stable/h2o-docs/productionizing.html
(BTW, the R API recently added h2o.predict_json() which does the conversion of arguments to JSON and the Java call for you; there appears to be nothing in the Python API yet, but if you read the R code you'll see it is not doing anything complex: just running a shell command.)
The other alternative is to stick with running the H2O server, and using it from Python. In that case you just want to save your model (a binary format), and then load it (back into the H2O cluster) in each time you want to make to make predictions: http://docs.h2o.ai/h2o/latest-stable/h2o-docs/save-and-load-model.html
The downside of this approach is you the binary format is always tied to the H2O version. So if you upgrade H2O you cannot use your saved models any more.
Upvotes: 0