Scoring with H2O MOJO model at command line with Java

If you are using H2O MOJO model you can use that to scoring in python or any other language just by using java runtime. This is a quick hack way to do the scoring on command line or from Python. Here are few example:

What you will have:

  • H2O MOJO model (it will be i.e. gbm_prostate_new.zip)
  • H2O supported class file for scoring i.e.  h2o-genmodel.jar
  • Your data set in JSON format to score i.e. ‘{“AGE”:”68″,”RACE”:”2″, “DCAPS”:”2″, “VOL”:”0″,”GLEASON”:”6″ }’

 

Here is command line way to perform scoring:

$ java -Xmx4g -cp .:/Users/avkashchauhan/src/github.com/h2oai/h2o-tutorials/tutorials/python_mojo_scoring/h2o-genmodel.jar:/Users/avkashchauhan/src/github.com/h2oai/h2o-tutorials/tutorials/python_mojo_scoring:genmodel.jar:/ water.util.H2OPredictor /Users/avkashchauhan/src/github.com/h2oai/h2o-tutorials/tutorials/python_mojo_scoring/gbm_prostate_new.zip ‘{\”AGE\”:\”68\”, \”RACE\”:\”2\”, \”DCAPS\”:\”2\”, \”VOL\”:\”0\”,\”GLEASON\”:\”6\” }’

Here is the results of above command:

{“labelIndex”:1,”label”:”1″,”classProbabilities”:[0.44056667027822005,0.55943332972178]}

Here is python code to score by launch external Java process:

> import subprocess

> gen_model_arg = ‘.:/Users/avkashchauhan/src/github.com/h2oai/h2o-tutorials/tutorials/python_mojo_scoring/h2o-genmodel.jar:/Users/avkashchauhan/src/github.com/h2oai/h2o-tutorials/tutorials/python_mojo_scoring:genmodel.jar:/’> h2o_predictor_class = ‘water.util.H2OPredictor’
> mojo_model_args = ‘/Users/avkashchauhan/src/github.com/h2oai/h2o-tutorials/tutorials/python_mojo_scoring/gbm_prostate_new.zip’
> json_data = {“AGE”:”68″,”RACE”:”2″, “DCAPS”:”2″, “VOL”:”0″,”GLEASON”:”6″}

Calling subprocess module:

> output = subprocess.check_output([“java” , “-Xmx4g”, “-cp”, gen_model_arg, h2o_predictor_class,
mojo_model_args, json_data], shell=False).decode()

## Generating output

> output

u'[ {"labelIndex":0,"label":"0",
    "classProbabilities":[0.8378244965684887,0.1621755034315113]} 
  ]\n'

Thats it, enjoy!!

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s