【发布时间】:2021-05-08 11:16:50
【问题描述】:
我正在关注this article 提供的this sample code。 IDE 是 Spyder 4.1.5 和 Python 3.8,anaconda 得到以下异常“FileNotFoundError: [WinError 2] The system cannot find the file specified”。
我是 python(和 Spyder)的新手,所以不确定缺少哪个文件,因为异常消息不包含文件名。任何提示将不胜感激。
我已经检查了环境,dot在路径中可用,并且包graphviz已经安装。
异常跟踪:
runfile('C:/my/work/smlb/challenge_1/code/tree_to_image.py', wdir='C:/my/work/smlb/challenge_1/code')
Traceback (most recent call last):
File "C:\my\work\smlb\challenge_1\code\tree_to_image.py", line 31, in <module>
call(['dot', '-Tpng', 'tree.dot', '-o', 'tree.png', '-Gdpi=600'])
File "C:\Users\user\anaconda3\lib\subprocess.py", line 340, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Users\user\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 105, in __init__
super(SubprocessPopen, self).__init__(*args, **kwargs)
File "C:\Users\user\anaconda3\lib\subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\user\anaconda3\lib\subprocess.py", line 1307, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
我尝试运行的示例代码:
我正在尝试运行以下示例源代码来生成图像以可视化二元决策。
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)
# Convert to png using system command (requires Graphviz)
from subprocess import call
call(['dot', '-Tpng', 'tree.dot', '-o', 'tree.png', '-Gdpi=600'])
#
# It works if directly call command `dot -Tpng tree.dot -o tree.png -Gdpi=600`
# but the subprocess call here doesn't work
#
# Display in jupyter notebook
from IPython.display import Image
Image(filename = 'tree.png')
在 Anaconda Prompt 中检查 python 包:
(base) C:\Users\user>conda list graphviz
# packages in environment at C:\Users\user\anaconda3:
#
# Name Version Build Channel
graphviz 2.38 hfd603c8_2
python-graphviz 0.16 pyhd3eb1b0_1
(base) C:\Users\user>where dot
C:\Users\user\anaconda3\Library\bin\dot.bat
(base) C:\Users\user>
【问题讨论】:
-
它是否将 tree.dot 转换为 tree.png?
-
是的。文件
tree.dot已生成;该错误发生在稍后尝试将其转换为 .png 的步骤中。
标签: python scikit-learn anaconda spyder