NameError:名称“ pydotplus”未定义

时间:2020-08-24 15:35:46

标签: python python-3.x jupyter-notebook anaconda pydotplus

我正在使用Anaconda和Jupyter Notebook并出现以下错误:

NameError: name 'pydotplus' is not defined

在为python3机器学习决策树运行以下代码时:

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()

2 个答案:

答案 0 :(得分:3)

您是否尝试过从pydotplus导入所需的特定方法?

就像:from pydotplus import graph_from_dot_data,因为您只使用模块中的一种方法。

答案 1 :(得分:0)

我能够实现所选答案中提供的解决方案:

import pydotplus # <----------------------- LOOK HERE
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) # <-------- LOOK HERE
graph.write_png('mydecisiontree.png')

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

我能够在Anaconda中解决我的问题,但是后来我去了kaggle,却无法在那儿工作。我改为将实现改为导入graphiz,而不再使用pydotplus。

import graphviz
dtree = tree.DecisionTreeClassifier(random_state = 1,max_depth = 5,min_samples_split=2)
dtree = dtree.fit(X, y)
data = tree.export_graphviz(dtree,feature_names=features,out_file=None)
graph = graphviz.Source(data)
graph