1

I'm executing the code below and getting the error following it -

from IPython.display import Image
from sklearn.tree import export_graphviz
from six import StringIO
import pydotplus
features = list(df.columns[:-1])
features
dot_data = StringIO()  
export_graphviz(dtree, out_file=dot_data,feature_names=features,filled=True,rounded=True)

graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
Image(graph.create_png())

Error-

InvocationException                       Traceback (most recent call last)
<ipython-input-98-1978b4285d97> in <module>
      3 
      4 graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
----> 5 Image(graph.create_png())

~\anaconda3\lib\site-packages\pydotplus\graphviz.py in <lambda>(f, prog)
   1789             self.__setattr__(
   1790                 'create_' + frmt,
-> 1791                 lambda f=frmt, prog=self.prog: self.create(format=f, prog=prog)
   1792             )
   1793             f = self.__dict__['create_' + frmt]

~\anaconda3\lib\site-packages\pydotplus\graphviz.py in create(self, prog, format)
   2022 
   2023         if status != 0:
-> 2024             raise InvocationException(
   2025                 'Program terminated with status: %d. stderr follows: %s' % (
   2026                     status, stderr_output))

InvocationException: Program terminated with status: 1. stderr follows: 'C:\Users\Ankit' is not recognized as an internal or external command,
operable program or batch file.

I'm assuming it is because of my Username - Ankit Chawrai. ALthough please tell me what could be the possible solution.

  • I'm getting this same error, as I too have a space in my user name, I rue the day that I added those spaces for readability as this has caused problems many times. I want to be able to add the 'r' qualifier to the path name as this was suggested in similar errors, but I'm not sure how to do this. I hope your questioned is answered soon! – Peej1226 Jun 01 '21 at 16:47
  • The same question was asked here: https://stackoverflow.com/questions/63675518/pydotplus-graphviz-error-program-terminated-with-status-1-stderr-follows-c – Peej1226 Jun 01 '21 at 17:43

1 Answers1

0

I have a very similar example that I'm trying out, it's based on a ML how-to book which is working with a Taiwan Credit Card dataset predicting default risk. My setup is as follows:

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

Then creating the decision tree plot is done in this way:

dot_data = StringIO()
export_graphviz(decision_tree=class_tree,
                out_file=dot_data,
                filled=True,
                rounded=True,
                feature_names = X_train.columns,
                class_names = ['pay','default'],
                special_characters=True)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) 
Image(graph.create_png())

I think it's all coming from the out_file=dot_data argument but cannot figure out where the file path is created and stored as print(dot_data.getvalue()) did not show any pathname.

In my research I came across sklearn.plot_tree() which seems to do everything that the graphviz does. So I took the above exporet_graphviz arguments and were matching arguments were in the .plot_tree method I added them.

I ended up with the following which created the same image as was found in the text:

from sklearn import tree

plt.figure(figsize=(20, 10))
tree.plot_tree(class_tree, 
               filled=True, rounded=True, 
               feature_names = X_train.columns,
               class_names = ['pay','default'],
               fontsize=12)
plt.show()
Peej1226
  • 132
  • 12