Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to visualize a single decision tree in Python
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')
@BarryDeCicco

This comment has been minimized.

Copy link

BarryDeCicco commented Feb 9, 2020

Note, this doesn't work in my jupyter notebook running python 3.7.4

`FileNotFoundError Traceback (most recent call last)
in
21 # Convert to png using system command (requires Graphviz)
22 from subprocess import call
---> 23 call(['dot', '-Tpng', 'tree.dot', '-o', 'tree.png', '-Gdpi=600'])
24
25 # Display in jupyter notebook

C:\ProgramData\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)

C:\ProgramData\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.

C:\ProgramData\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 pipe

FileNotFoundError: [WinError 2] The system cannot find the file specified
`

@skmishra0628

This comment has been minimized.

Copy link

skmishra0628 commented Apr 30, 2020

@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.

@taekhoonkim

This comment has been minimized.

Copy link

taekhoonkim commented Jun 18, 2020

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'

@9kwon

This comment has been minimized.

Copy link

9kwon commented Jul 28, 2020

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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.