Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Greph not displaying.
#1
Hi I wrote the following code for a decision tree.

import pandas as pd
import pydotplus
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
from sklearn.tree import export_graphviz  #Import scikit-learn graphviz to show the graph
from sklearn.externals.six import StringIO
from IPython.display import Image

df = pd.read_csv("diabetes.csv")

#split the dataset in  features and target variable
feature_cols = ['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI', 'DiabetesPedigreeFunction', 'Age'] # independant or feature columns
X = df[feature_cols]
y = df.Outcome # target variable

#Splitting the data in Testing and Traning set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=1) #70% training and 30% test

#Building The decision tree

#Creating a decision tree classierfier object
clf = DecisionTreeClassifier()

#Create a decision tree clasifier
clf = DecisionTreeClassifier(criterion="entropy")

#Training the classifier
clf = clf.fit(X_train,y_train)

#Predict the response for the dataset on the testing data
y_pred = clf.predict(X_test)

#Model accuracy, how often is the model classifier correct
print(metrics.accuracy_score(y_test, y_pred))


dot_data = StringIO()
export_graphviz(clf, out_file=dot_data,
                filled=True, rounded=True,
                special_characters=True,feature_names = feature_cols,class_names=['0','1'])
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png("diabetes.png")
Image(graph.create_png())
The code creates a .png file called diabetes but the file is empty. alos it's giving me the following errors

pydotplus.graphviz.InvocationException: GraphViz's executables not found
I am not getting why its doing that or how to resolve it.

I will really appreciate it if someone can point out why its not displaying anything in the .png file and how to solve it.

Thanks
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020