2

I try to make png from decision tree that i made before from a data. Using package pydotplus, I want to save the png in my local storage and show it. It work in google colab, but it error in jupyter. It said

GraphViz's executables not found

This is my code

from sklearn.tree import export_graphviz 
from six import StringIO
from IPython.display import Image  
import pydotplus

feature_cols = ['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin',
       'BMI', 'DiabetesPedigreeFunction', 'Age']
dot_data = StringIO()
export_graphviz(clf, out_file=dot_data,  
                filled=True, rounded=True,
                special_characters=True, feature_names = feature_cols, class_names=['0','1'])
graph = pydotplus.graphviz.graph_from_dot_data(dot_data.getvalue())  
graph.write_png('diabetes.png')
Image(graph.create_png())

The error show in jupyter

---------------------------------------------------------------------------
InvocationException                       Traceback (most recent call last)
Input In [46], in <cell line: 14>()
     12 graph = pydotplus.graphviz.graph_from_dot_data(dot_data.getvalue())  
     13 # graph.write_png('file//diabetes.png')
---> 14 Image(graph.create_png())

File ~\.conda\envs\butterfly\lib\site-packages\pydotplus\graphviz.py:1797, in Dot.__init__.<locals>.<lambda>(f, prog)
   1792 # Automatically creates all the methods enabling the creation
   1793 # of output in any of the supported formats.
   1794 for frmt in self.formats:
   1795     self.__setattr__(
   1796         'create_' + frmt,
-> 1797         lambda f=frmt, prog=self.prog: self.create(format=f, prog=prog)
   1798     )
   1799     f = self.__dict__['create_' + frmt]
   1800     f.__doc__ = (
   1801         '''Refer to the docstring accompanying the'''
   1802         ''''create' method for more information.'''
   1803     )

File ~\.conda\envs\butterfly\lib\site-packages\pydotplus\graphviz.py:1959, in Dot.create(self, prog, format)
   1957     self.progs = find_graphviz()
   1958     if self.progs is None:
-> 1959         raise InvocationException(
   1960             'GraphViz\'s executables not found')
   1962 if prog not in self.progs:
   1963     raise InvocationException(
   1964         'GraphViz\'s executable "%s" not found' % prog)

InvocationException: GraphViz's executables not found

I hope you guys can help me to solve this problem

Mind Mon
  • 85
  • 4

2 Answers2

0

Hello Try these answers in the link, Maybe this is helpful. Or you can try importing OpenCV or PIL or matplotlib and write the variable as a png file. Install OpenCV library before doing this. You can do it by

#Installing OpenCV library
conda install -c anaconda opencv

#importing opencv 
import cv2

#writing a variable as an image but it should be in an array format to achieve this.
cv2.imwrite("graph.png", graph)
0

I will answer my question.

This question problem is in the file graphviz. I can fix this problem by download package from graphviz website. If you download from pip install pydotplus or pip install graphviz maybe you get the same error.

---------------------------------------------------------------------------
InvocationException                       Traceback (most recent call last)
Input In [46], in <cell line: 14>()
     12 graph = pydotplus.graphviz.graph_from_dot_data(dot_data.getvalue())  
     13 # graph.write_png('file//diabetes.png')
---> 14 Image(graph.create_png())

File ~\.conda\envs\butterfly\lib\site-packages\pydotplus\graphviz.py:1797, in Dot.__init__.<locals>.<lambda>(f, prog)
   1792 # Automatically creates all the methods enabling the creation
   1793 # of output in any of the supported formats.
   1794 for frmt in self.formats:
   1795     self.__setattr__(
   1796         'create_' + frmt,
-> 1797         lambda f=frmt, prog=self.prog: self.create(format=f, prog=prog)
   1798     )
   1799     f = self.__dict__['create_' + frmt]
   1800     f.__doc__ = (
   1801         '''Refer to the docstring accompanying the'''
   1802         ''''create' method for more information.'''
   1803     )

File ~\.conda\envs\butterfly\lib\site-packages\pydotplus\graphviz.py:1959, in Dot.create(self, prog, format)
   1957     self.progs = find_graphviz()
   1958     if self.progs is None:
-> 1959         raise InvocationException(
   1960             'GraphViz\'s executables not found')
   1962 if prog not in self.progs:
   1963     raise InvocationException(
   1964         'GraphViz\'s executable "%s" not found' % prog)

InvocationException: GraphViz's executables not found

The problem is in the library itself. So, try to download zip file from graphviz website and direct the extract file bin in my computer. This link will help you to the detail

Mind Mon
  • 85
  • 4
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 15 '22 at 07:22