我正在尝试将点文件转换为 png 或 jpeg 文件,我可以在其中查看随机森林树。我正在学习本教程:https://towardsdatascience.com/how-to-visualize-a-decision-tree-from-a-random-forest-in-python-using-scikit-learn-38ad2d75f21c .
我收到错误 FileNotFoundError: [WinError 2] 系统找不到指定的文件
我可以看到 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')
最佳答案
我运行了 docker - ubuntu image 并在 Dockerfile 中运行:RUN apt-get install graphviz -y
。它开始工作了。然后使用dot -Tpng tree.dot -o tree.png
关于Python - 点文件到 png 文件未找到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52505606/