繁体   English   中英

Python / H2o:H2ORandomForestEstimator模型中的图树/提取规则

[英]Python/H2o : Plot tree/Extract rules from H2ORandomForestEstimator model

是否有任何“简单”方式可以从H2O随机森林模型中绘制树木。 我也对提取结果规则感兴趣吗?

是。

从此文档中:

以下代码段显示了如何从R下载MOJO并在命令行上运行PrintMojo工具以创建.png文件:

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

可以在这里找到示例python实现: 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'))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM