【问题标题】:Python/H2o : Plot tree/Extract rules from H2ORandomForestEstimator modelPython/H2o:从 H2ORandomForestEstimator 模型中绘制树/提取规则
【发布时间】:2018-10-28 16:23:56
【问题描述】:

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

【问题讨论】:

    标签: python random-forest h2o


    【解决方案1】:

    是的。

    来自本文档:

    以下代码 sn-p 显示如何从 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
    

    【讨论】:

    • 感谢您的回答。我一直在寻找不适用于 R 的 Python 解决方案。当我运行“h2o.download_mojo ...”时出现此错误:AttributeError: module 'h2o' has no attribute 'download_mojo' H2O session _sid_81cf closed.
    【解决方案2】:

    可以在此处找到示例 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'))
    

    【讨论】:

      猜你喜欢
      • 2018-04-19
      • 2021-12-16
      • 2016-10-27
      • 2019-10-11
      • 2023-03-16
      • 2020-01-13
      • 2015-10-25
      • 2018-03-07
      • 2019-03-03
      相关资源
      最近更新 更多