Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/H2o : Plot tree/Extract rules from H2ORandomForestEstimator model

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

like image 464
Ala Ham Avatar asked Nov 07 '22 06:11

Ala Ham


1 Answers

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'))
like image 107
ahmedengu Avatar answered Nov 15 '22 11:11

ahmedengu