-
-
Save WillKoehrsen/ff77f5f308362819805a3defd9495ffd to your computer and use it in GitHub Desktop.
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']) | |
# Display in jupyter notebook | |
from IPython.display import Image | |
Image(filename = 'tree.png') |
It doesn't work in my jupyter
I run in Centos 8 and python 3.6.7
FileNotFoundError Traceback (most recent call last)
in
1 # Convert to png using system command (requires Graphviz)
2 from subprocess import call
----> 3 call(['dot', '-Tpng', 'tree.dot', '-o', 'tree.png', '-Gdpi=600'])
/usr/lib64/python3.6/subprocess.py in call(timeout, *popenargs, **kwargs)
285 retcode = call(["ls", "-l"])
286 """
--> 287 with Popen(*popenargs, **kwargs) as p:
288 try:
289 return p.wait(timeout=timeout)
/usr/lib64/python3.6/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)
727 c2pread, c2pwrite,
728 errread, errwrite,
--> 729 restore_signals, start_new_session)
730 except:
731 # Cleanup if the child failed starting.
/usr/lib64/python3.6/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, restore_signals, start_new_session)
1362 if errno_num == errno.ENOENT:
1363 err_msg += ': ' + repr(err_filename)
-> 1364 raise child_exception_type(errno_num, err_msg, err_filename)
1365 raise child_exception_type(err_msg)
1366
FileNotFoundError: [Errno 2] No such file or directory: 'dot': 'dot'
I use this on Ubuntu 16 based jupyter notebook but have the same problem as the first question.
FileNotFoundError: [Errno 2] No such file or directory: 'dot': 'dot'
Hi, installing Graphwiz on Windows makes the script work on Windows. Remember to add Graphwiz to PATH for current user.
https://graphviz.org/download/
Note for thecml's solution - add Graphwiz to PATH:
https://stackoverflow.com/questions/35064304/runtimeerror-make-sure-the-graphviz-executables-are-on-your-systems-path-aft
,answered Jun 19 '17 at 8:43, Aprameyo Roy
works on Jupyter
Use below command in your terminal if you are using ubuntu 20.04
sudo apt-get install graphviz
Before this i was getting below error
Traceback (most recent call last):
File "visualize_decision_tree.py", line 23, in
call(['dot', '-Tpng', 'tree.dot', '-o', 'tree.png', '-Gdpi=600'])
File "/usr/lib/python3.8/subprocess.py", line 340, in call
with Popen(*popenargs, **kwargs) as p:
File "/usr/lib/python3.8/subprocess.py", line 858, in init
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/lib/python3.8/subprocess.py", line 1704, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'dot'
Hi, installing Graphwiz on Windows makes the script work on Windows. Remember to add Graphwiz to PATH for current user. https://graphviz.org/download/
This works! Thank you!
@barry - You will need to run the jupyter notebook on a Linux or Linux-like platform. If you work on a Windows 10 O/S, try installing the Ubuntu O/S - comes free with Windows 10. You can use this Windows Subsystem Linux to run the jupyter notebook (after installation of the Anaconda package) and then above notebook should work.