pythonpython-3.xjupyter-notebookanacondapydotplus

NameError: name 'pydotplus' is not defined


I am using Anaconda and Jupyter Notebook and got the following error:

NameError: name 'pydotplus' is not defined

when running the following code for a python3 machine learning decision-tree:

import pandas
from sklearn import tree
import pydotplus
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt
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')
imgplot = plt.imshow(img)
plt.show()

Solution

  • Have you tried importing the specific method you need from pydotplus instead?

    Like: from pydotplus import graph_from_dot_data since you're only using that one method from the module.