【发布时间】:2019-03-01 12:23:18
【问题描述】:
我正在尝试将点文件转换为可以查看随机森林树的 png 或 jpeg 文件。我正在关注本教程:https://towardsdatascience.com/how-to-visualize-a-decision-tree-from-a-random-forest-in-python-using-scikit-learn-38ad2d75f21c。
我收到错误FileNotFoundError: [WinError 2] The system cannot find the file specified
我可以看到 tree.dot 在那里,我可以打开它。试图找出为什么它不阅读它?谢谢。
from sklearn.datasets import load_iris
iris = load_iris()
# Model (can also use single decision tree)
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators=10)
# Train
model.fit(iris.data, iris.target)
# Extract single tree
estimator = model.estimators_[5]
from sklearn.tree import export_graphviz
# Export as dot file
export_graphviz(estimator, out_file='tree.dot',
feature_names = iris.feature_names,
class_names = iris.target_names,
rounded = True, proportion = False,
precision = 2, filled = True)
<<error occurs here>>
# Convert to png using system command (requires Graphviz)
from subprocess import call
call(['dot', '-Tpng', 'tree.dot', '-o', 'tree.png', '-Gdpi=600'])
# Display in jupyter notebook
from IPython.display import Image
Image(filename = 'tree.png')
【问题讨论】:
-
你的命令路径中有点吗?可以直接在命令提示符下运行吗?我假设 tree.dot 正在当前目录中创建。
-
如果我在 Jupyter 笔记本中运行此命令
call(['dot', '-Tpng', 'tree.dot', '-o', 'tree.png', '-Gdpi=600'])我也会收到错误消息,但如果我从常规 python 终端运行它就可以了。 -
@hellpanderr 我尝试从 python 终端和命令行运行它。同样的问题
-
如果你给出点命令的完整路径是否有效?
标签: python scikit-learn random-forest