Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Saving H2O Model Objects as Text Locally

DZone's Guide to

Saving H2O Model Objects as Text Locally

In this code-heavy tutorial, learn how to easily save H2O model objects to your local disk as simple text content using Python.

· Big Data Zone ·
Free Resource

The open source HPCC Systems platform is a proven, easy to use solution for managing data at scale. Visit our Easy Guide to learn more about this completely free platform, test drive some code in the online Playground, and get started today.

Sometimes, you may want to store an H2O model object as text to a local file system. In this example, I will show you how you can save H2O model objects to your local disk as simple text content. You can get the full-working Jupyter notebook for this example here from my GitHub.

Based on my experience, the following example works fine with Python 2.7.12 and Python 3.4. I also found that the H2O model object tables were not saved to text files from Jupyter notebook. However, when I ran the same code from the command line into the Python shell, all the content was written perfectly.

Let's build an H2O GBM model using the public PROSTATE dataset. The following script is the full working script, which will generate the GBM binomial model:

import h2o
h2o.init()

local_url = "https://raw.githubusercontent.com/h2oai/sparkling-water/master/examples/smalldata/prostate.csv"
df = h2o.import_file(local_url)

y = "CAPSULE"
feature_names = df.col_names
feature_names.remove(y) 
df[y] = df[y].asfactor()

df_train, df_valid = df.split_frame(ratios=[0.9])
print(df_train.shape)
print(df_valid.shape)

prostate_gbm = H2OGradientBoostingEstimator(model_id = "prostate_gbm",
 ntrees=1000,
 learn_rate=0.5,
 max_depth=20,
 stopping_tolerance=0.001,
 stopping_rounds=2,
 score_each_iteration=True)

prostate_gbm.train(x = feature_names, y = y, training_frame=df_train, validation_frame=df_valid)
prostate_gbm

Save the model details to the disk as shown below:

old_target = sys.stdout
f = open('/Users/avkashchauhan/Downloads/model_output.txt', 'w')
sys.stdout = f

Let's see the content of the local file we have just created in the above step (it is empty):

!cat /Users/avkashchauhan/Downloads/model_output.txt

Launch the following commands, which will fill the standard output buffer with the model details as text:

print("Model summary>>> model_object.show()")
prostate_gbm.show()

Push the standard output buffer to the text file, which is created locally:

sys.stdout = old_target

Check back with the local file contents. This time, you will see that the output of the above command is written into the file:

!cat /Users/avkashchauhan/Downloads/model_output.txt

You will see the command output stored into the local text file as below:

Model summary>>> model_object.show()
Model Details
=============
H2OGradientBoostingEstimator :  Gradient Boosting Machine
Model Key:  prostate_gbm


ModelMetricsBinomial: gbm
** Reported on train data. **

MSE: 0.036289343297
RMSE: 0.190497620187
LogLoss: 0.170007804527
Mean Per-Class Error: 0.0160045361428
AUC: 0.998865964296
Gini: 0.997731928592
Confusion Matrix (Act/Pred) for max f1 @ threshold = 0.487417363665: 
Maximum Metrics: Maximum metrics at their respective thresholds

Gains/Lift Table: Avg response rate: 40.36 %



ModelMetricsBinomial: gbm
** Reported on validation data. **

MSE: 0.161786079676
RMSE: 0.402226403505
LogLoss: 0.483923658542
Mean Per-Class Error: 0.174208144796
AUC: 0.871040723982
Gini: 0.742081447964
Confusion Matrix (Act/Pred) for max f1 @ threshold = 0.205076283533: 
Maximum Metrics: Maximum metrics at their respective thresholds

Gains/Lift Table: Avg response rate: 39.53 %


Scoring History: 
Variable Importances:

Note: If you are wondering what the ! sign does here, it is used here to run a Linux shell command (in this case, cat  is the Linux command) inside the Jupyter cell.

That's it; enjoy!

Managing data at scale doesn’t have to be hard. Find out how the completely free, open source HPCC Systems platform makes it easier to update, easier to program, easier to integrate data, and easier to manage clusters. Download and get started today.

Topics:
h2o ,python ,linux ,big data ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}