1

i am trying to learn machine learn through Python from W3School. I am trying to get mydecisiontree. PNG using PyDotPlus

I am using pip PyCharm professional 2020.3 the code is as follow:

import numpy as np
import matplotlib.pyplot as plt
import pandas
from sklearn import tree
import pydotplus
from sklearn.tree import DecisionTreeClassifier
import matplotlib.image as pltimg

df = pandas.read_csv("shows.csv")
d = {'UK' : 0,'USA' : 1, 'N' : 2}
df['Nationality'] = df['Nationality'].map(d)
d = {'YES' : 1, "NO" : 0}
df['Go'] = df['Go'].map(d)
features = ['Age', 'Experience', 'Rank', 'Nationality']
X = df[features]
y = df['Go']

dtree = DecisionTreeClassifier()
dtree = dtree.fit(X, y)
data = tree.export_graphviz(dtree, out_file=None, feature_names=features)
graph = pydotplus.graph_from_dot_data(data)
graph.write_png('mydecisiontree.png')

img=pltimg.imread('mydecisiontree.png')
img_plot = plt.imshow(img)
plt.show()

Although the PyCharm shows no error but when i run the code it cant make the PNG file and gives an error on the line:

graph.write_png('mydecisiontree.png')

It shows the following error:

File "direc.....\venv\lib\site-packages\pydotplus\graphviz.py", line 1960, in create
    'GraphViz\'s executables not found')
pydotplus.graphviz.InvocationException: GraphViz's executables not found

I can't see the problem. How to solve this?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Mehrizi98
  • 11
  • 2

2 Answers2

1

PyCharm doesn't show an error because your code doesn't contain any. The problem is in your environment. Have you installed GraphViz (using pip install graphviz) in the environment that you are using to run it?

Also see the answers here:

1

I had that problem too, I installed graphviz from: here and it solved.

  • be sure to put GraphViz in the environment path. – Hamid Saadi Jun 26 '21 at 19:21
  • Welcome to stackoverflow, and for taking the effort to post an answer here. However, it's not so clear to me what your answer contributes to the answer that has already been posted a long time ago. If you want to add something to an already posted answer, a comment may be more appropriate. You may also be interested in this: https://stackoverflow.com/help/how-to-answer – Matthias C. M. Troffaes Jun 27 '21 at 07:54