Graphviz does not show the picture on PyCharm

Viewed 186

I am building a decision tree model using sklearn.

I think the model works just fine, but I have no idea why it is not showing the picture automatically, as matplotlib's show() function would. Does this have something to do with the setting? below is the code:

import pandas as pd
from sklearn import tree
from sklearn.model_selection import train_test_split
from sklearn.tree import export_graphviz
import graphviz

data = pd.read_excel('file location')

target_vars = ['variable1','variable2','variable3']
X = pd.DataFrame()
for i in target_vars:
    X[i]=data[i]

y = data['outcome']

X_tn, X_te, y_tn, y_te = train_test_split(X, y, random_state=0)

regr = tree.DecisionTreeClassifier(criterion='entropy', max_depth=5)

regr.fit(X_tn,y_tn)

y_pred = regr.predict(X_te)
accuracy = (y_pred==y_te).mean()


print('Model Accuracy: ', accuracy)

export_graphviz(regr, out_file='tree.dot', class_names=['1','0'],
                feature_names=target_vars, impurity = True, filled = True)

with open('tree.dot') as f:
    dot_graph = f.read()
graphviz.Source(dot_graph)
0 Answers
Related