如何可视化H2O树?

时间:2019-07-08 16:20:39

标签: python visualization decision-tree h2o

我有df data_categorical和一个模型model
我用
将df转换为h2o帧 data = h2o.H2OFrame(data_categorical)

并用

训练了我的模型
model = H2ORandomForestEstimator(ntrees=1, max_depth=20, nfolds=10)
# Train model
model.train(x=training_columns, y=response_column, training_frame=train) 

我正在尝试可视化所创建的树(请注意,我只需要一棵树),但是我似乎做不到。
我用
下载了mojo文件 model.download_mojo(path,get_genmodel_jar=True)
但是我不知道下一步该怎么做

2 个答案:

答案 0 :(得分:1)

您可以使用以下文档中描述的工作流程:

java -cp h2o.jar hex.genmodel.tools.PrintMojo --tree 0 -i model.zip -o model.gv -f 20 -d 3
dot -Tpng model.gv -o model.png
open model.png

答案 1 :(得分:1)

对于 Windows/python 用户,我整理了一个小食谱,包括所有步骤的链接:

一般信息: http://docs.h2o.ai/h2o/latest-stable/h2o-docs/mojo-quickstart.html

  1. 安装graphViz: https://forum.graphviz.org/t/new-simplified-installation-procedure-on-windows/224

  2. 安装 Java JDK 14: https://www.oracle.com/java/technologies/javase/jdk14-archive-downloads.html

  3. 将 Java 添加到 PATH 环境变量(以便能够从控制台执行)

  4. 在 python 中编译 H2O 模型并使用 .download_mojo(path) 函数导出模型:

decTreeModel.download_mojo('C:/User/L/mojoExports/myMojoModel.zip')

  1. 转到 myMojoModel.zip 所在的位置并在此目录中启动 CMD。在 CMD 中键入:

java -cp h2o.jar hex.genmodel.tools.PrintMojo --tree 0 --levels 21 --title "title for tree" -i myMojoModel.zip -o model.gv -f 20 -d 3

meaning of parameters:

--tree n        : n is the number of the tree to be exported if there are more than one model in the mojo model (e.g. when using cross validation)

--levels n      : n is number of categorical levels to be printed (default 10)

--title "string"    : you can specify the title here

-i "path"       : "path" is path to input model (myMojoModel.zip)

-o "path"       : "path" is path to output graph (model.gv)

-f n            : n is the font size

-d n            : n is the number of decimals displayed for numbers
  1. 在 CMD 中,使用 graphViz 创建 .png。输入

dot -Tpng model.gv -o model.png

  1. 它将在该目录中创建一个 .png 文件,您可以使用任何 png 查看器查看该文件