无法打印随机森林回归树

Not able to print Random Forest Regressor tree

我查看了几个示例并按照它们进行了操作,但无法打印树图。

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" 的目录中)将以下内容转换为 png :

dot -Tpng tree.dot -o tree.png

如果这不起作用,您也可以尝试使用 dot.exe:

的完整路径

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

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

来自 another Whosebug answer 的解决方案。