AttributeError:模块“ pydotplus”没有属性“ Node”

时间:2019-08-29 13:45:45

标签: python plot decision-tree

我正在尝试根据在DataCamp上找到的文章https://www.datacamp.com/community/tutorials/decision-tree-classification-python来绘制决策树图。但是,我遇到了属性错误:

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

decision_tree = DecisionTreeRegressor(max_depth=3)
decision_tree.fit(train_features, train_targets)

# Predict values for train and test
train_predictions = decision_tree.predict(train_features)
test_predictions = decision_tree.predict(test_features)



dot_data = StringIO()

export_graphviz(decision_tree, out_file=dot_data,  filled=True, rounded=True, special_characters=True)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) 
graph.write_png('decision_tree.png')
Image(graph.create_png())



AttributeError: module 'pydotplus' has no attribute 'Node'

有人有什么线索我应该调查吗?

1 个答案:

答案 0 :(得分:1)

我刚刚尝试了本教程中的代码,并且得到了一个不错的决策树图。我从here下载了数据集。

请尝试以下代码。我同时使用python-3python-2.7进行了测试。

# Load libraries
import pandas as pd
from sklearn.tree import DecisionTreeClassifier # Import Decision Tree Classifier
from sklearn.model_selection import train_test_split # Import train_test_split function
from sklearn import metrics #Import scikit-learn metrics module for accuracy calculation


col_names = ['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI', 'DiabetesPedigreeFunction', 'Age', 'Outcome']
# load dataset
pima = pd.read_csv("diabetes.csv", header=None, names=col_names)



#split dataset in features and target variable
feature_cols = ['Pregnancies', 'Insulin', 'BMI', 'Age','Glucose','BloodPressure','SkinThickness']
X = pima[feature_cols] # Features
y = pima['Outcome']# Target variable


# Split dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1) # 70% training and 30% test


# Create Decision Tree classifer object
clf = DecisionTreeClassifier()

# Train Decision Tree Classifer
clf = clf.fit(X_train,y_train)


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

decision_tree = DecisionTreeRegressor(max_depth=3)
decision_tree.fit(X_train, y_train)

# Predict values for train and test
# train_predictions = decision_tree.predict(X_train)
# test_predictions = decision_tree.predict(X_test)



dot_data = StringIO()

export_graphviz(decision_tree, out_file=dot_data,  filled=True, rounded=True, special_characters=True)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) 
graph.write_png('decision_tree.png')
Image(graph.create_png())

输出 Output figure: