【问题标题】:Not able to print Random Forest Regressor tree无法打印随机森林回归器树
【发布时间】:2018-12-02 04:03:49
【问题描述】:

我查看了几个示例并遵循了它们,但无法打印树形图。

R_forest = RandomForestRegressor(bootstrap=False,
  max_depth=30,
  max_features ='sqrt',
  min_samples_leaf= 4,
  min_samples_split=2,
  n_estimators = 600)
model=R_forest.fit(X_train,y_train)

from sklearn.datasets import *
from sklearn import tree
from sklearn.tree import export_graphviz
import graphviz
import pydot


tree = R_forest.estimators_[5]
# Export the image to a dot file
export_graphviz(tree, out_file = 'tree.dot', feature_names = X_train.columns, 
rounded = True, precision = 1)
# Use dot file to create a graph
(graph, ) = pydot.graph_from_dot_file('tree.dot')
# Write graph to a png file
graph.write_png('tree.png')

我收到此错误:

FileNotFoundError: [WinError 2] "dot.exe" not found in path.

我遵循了这个解决方案,但仍然遇到同样的错误。

"RuntimeError: Make sure the Graphviz executables are on your system's path" after installing Graphviz 2.38

我的系统截图。

任何帮助或建议表示赞赏

【问题讨论】:

  • 哪一行代码给了你错误?你能生成“tree.dot”文件吗?
  • 是的,我能够生成“tree.dot”文件。感谢您的回答,我将确认为正确答案。

标签: graphviz random-forest pydot


【解决方案1】:

如果您能够生成“tree.dot”文件,那么您可以从命令行(在“tree.dot”所在的目录中)运行以下命令以转换为 png:

dot -Tpng tree.dot -o tree.png

如果这不起作用,您也可以尝试使用 dot.exe 的完整路径:

path\to\dot.exe -Tpng tree.dot -o tree.png

使用 Python 从 Python 运行 graph.write_png('tree.png') 在我的 Windows 机器上不起作用,但命令行操作可以。

来自another StackOverflow answer 的解决方案。

【讨论】:

    猜你喜欢
    • 2019-10-23
    • 2019-12-06
    • 2023-03-14
    • 2013-12-04
    • 2021-12-25
    • 1970-01-01
    • 2020-08-28
    • 2018-06-24
    • 2016-04-24
    相关资源
    最近更新 更多