我使用随机森林训练了我的模型,并希望可视化决策树。然后,我希望将 export_graphviz 转换为 png 文件。
from subprocess import call# Convert to pngcall(['dot', '-Tpng', 'tree_from_optimized_forest.dot', '-o', 'tree_from_optimized_forest.png', '-Gdpi=200'])Image('tree_from_optimized_forest.png')
输出结果是
---------------------------------------------------------------------------FileNotFoundError Traceback (most recent call last)<ipython-input-95-f836aa32a65b> in <module> 2 3 # Convert to png----> 4 call(['dot', '-Tpng', 'tree_from_optimized_forest.dot', '-o', 'tree_from_optimized_forest.png', '-Gdpi=200']) 5 Image('tree_from_optimized_forest.png')~\Anaconda3\lib\subprocess.py in call(timeout, *popenargs, **kwargs) 321 retcode = call(["ls", "-l"]) 322 """--> 323 with Popen(*popenargs, **kwargs) as p: 324 try: 325 return p.wait(timeout=timeout)~\Anaconda3\lib\subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors, text) 773 c2pread, c2pwrite, 774 errread, errwrite,--> 775 restore_signals, start_new_session) 776 except: 777 # Cleanup if the child failed starting.~\Anaconda3\lib\subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_start_new_session) 1176 env, 1177 os.fspath(cwd) if cwd is not None else None,-> 1178 startupinfo) 1179 finally: 1180 # Child is launched. Close the parent's copy of those pipeFileNotFoundError: [WinError 2] The system cannot find the file specified
如何解决此错误?
回答:
我使用 pydot 库解决了这个问题
import pydot(graph,) = pydot.graph_from_dot_file('tree_real_data.dot')graph.write_png('somefile.png')