5

Is there any "simple" way to plot trees from an H2O random forest model. I am also interestred in extracting the resulting rules ?

Ala Ham
  • 169
  • 1
  • 7

2 Answers2

1

A sample python implementation could be found here: https://gist.github.com/ahmedengu/e2cbc2d937e48de3f43b3c903d656143 https://dzone.com/articles/visualizing-h2o-gbm-and-random-forest-mojo-models

# save model to mojo and view it as an image
# R code sample and more information available here: http://docs.h2o.ai/h2o/latest-stable/h2o-docs/productionizing.html#viewing-a-mojo-model
# another python example could be found here: https://dzone.com/articles/visualizing-h2o-gbm-and-random-forest-mojo-models

model = aml.leader # the model that we want to plot it can be any h2o model as long as it's not a StackedEnsemble model
model_path = model.download_mojo(get_genmodel_jar=True)

# download h2o jar 
!wget -c http://h2o-release.s3.amazonaws.com/h2o/rel-xia/2/h2o-3.22.0.2.zip
!unzip -n h2o-3.22.0.2.zip 

!java -cp h2o-3.22.0.2/h2o.jar hex.genmodel.tools.PrintMojo --tree 0 -i $model_path -o model.gv -f 20 -d 3
!dot -Tpng model.gv -o model.png

from IPython.display import display
from PIL import Image

# showing the image in notebook
display(Image.open('model.png'))
ahmedengu
  • 93
  • 2
  • 6
0

Yes.

From this documentation:

The following code snippet shows how to download a MOJO from R and run the PrintMojo tool on the command line to make a .png file:

library(h2o)
h2o.init()
df <- h2o.importFile("http://s3.amazonaws.com/h2o-public-test-data/smalldata/airlines/allyears2k_headers.zip")
model <- h2o.gbm(model_id = "model",
                 training_frame = df,
                 x = c("Year", "Month", "DayofMonth", "DayOfWeek", "UniqueCarrier"),
                 y = "IsDepDelayed",
                 max_depth = 3,
                 ntrees = 5)
h2o.download_mojo(model, getwd(), FALSE)

# Now download the latest stable h2o release from http://www.h2o.ai/download/
# and run the PrintMojo tool from the command line.
#
# (For MacOS: brew install graphviz)
# java -cp h2o.jar hex.genmodel.tools.PrintMojo --tree 0 -i model.zip -o model.gv
# dot -Tpng model.gv -o model.png
# open model.png
TomKraljevic
  • 3,661
  • 11
  • 14
  • 1
    thank you for the answer. I was looking for a solution for python not for R. i get this error when i run "h2o.download_mojo..." : AttributeError: module 'h2o' has no attribute 'download_mojo' H2O session _sid_81cf closed. – Ala Ham May 22 '18 at 12:33