Mar-15-2020, 01:35 AM
Hi I wrote the following code for a decision tree.
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
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 foundI 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