-1

I'm trying to graph my decision tree based on the article I found on DataCamp: https://www.datacamp.com/community/tutorials/decision-tree-classification-python. However, I'm getting an attribute error:

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'

Has anyone any clue where should I look into?

krakowi
  • 583
  • 5
  • 17
  • What is the line related to the error? – Achintha Ihalage Aug 29 '19 at 13:50
  • it refers to the pydotplus package: ~\AppData\Local\Programs\Python\Python36\lib\site-packages\pydotplus\parser.py in push_node_stmt(s, loc, toks) 374 node_name = node_name[0] 375 --> 376 n = pydotplus.Node(str(node_name), **attrs) 377 return n 378 – krakowi Aug 29 '19 at 13:52
  • in 10 11 export_graphviz(decision_tree, out_file=dot_data, special_characters=True) ---> 12 graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) 13 graph.write_png('decision_tree.png') 14 Image(graph.create_png()) – krakowi Aug 29 '19 at 13:55

1 Answers1

1

I just tried the code in the tutorial and I get a nice decision tree figure. I downloaded the dataset from here.

Please try the following code. I tested it with both python-3 and python-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: Output figure:

Achintha Ihalage
  • 2,310
  • 4
  • 20
  • 33