3

I am trying to convert dot file into a png or jpeg file where I can view the Random Forest Tree. I am following this tutorial: https://towardsdatascience.com/how-to-visualize-a-decision-tree-from-a-random-forest-in-python-using-scikit-learn-38ad2d75f21c.

I am getting error FileNotFoundError: [WinError 2] The system cannot find the file specified

I can see that tree.dot is there and I am able to open it. Trying to find why it is not reading it? Thanks.

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')
sharp
  • 2,140
  • 9
  • 43
  • 80
  • Is dot in your command PATH? Can you run it directly in the command prompt? I assume tree.dot is being created in the current directory. – PM 2Ring Sep 25 '18 at 19:52
  • If I run this command `call(['dot', '-Tpng', 'tree.dot', '-o', 'tree.png', '-Gdpi=600'])` in Jupyter notebook I also get an error, but if I run from regular python terminal it works ok. – hellpanderr Sep 25 '18 at 20:01
  • https://imgur.com/a/pJpOYBn – hellpanderr Sep 25 '18 at 20:02
  • @hellpanderr I tried running it from python terminal and command line. Same issue – sharp Sep 25 '18 at 20:08
  • 1
    Does it work if you give the full path to the dot command? – PM 2Ring Sep 25 '18 at 20:27
  • If you are searching for this in windows you might want to check https://stackoverflow.com/questions/5316206/converting-dot-to-png-in-python – Jorge Diaz Nov 21 '19 at 17:35

1 Answers1

0

I ran through docker - ubuntu image and ran: RUN apt-get install graphviz -y in the Dockerfile. It started to work. Then used dot -Tpng tree.dot -o tree.png

sharp
  • 2,140
  • 9
  • 43
  • 80