Python - 点文件到 png 文件未找到错误

标签 python scikit-learn random-forest

我正在尝试将点文件转换为 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/

相关文章:

python - xgboost、extratreeclassifier 和 randomforrestclasiffier 之间有什么区别?

python - AWS DynamoDB Python - boto3 Key() 方法无法识别(查询)

python 2.7 : how to read only a few lines at a time from a file?

c++ - SWIG:将异常从 Python 抛出到 C++

python - 使用 Matplotlib、Python 的多个函数绘制为 1 个图形

python - 如何将下面代码中的性能指标从准确度更改为精确度、召回率和其他指标?

python - 使用神经网络对 KDD Cup 99 数据集建模

python - pandas 结果变量为 NaN

python - 强制分类器选择固定数量的目标

python - Sklearn 随机森林回归器出错