Generate png output from decision tree

OS: Windows 7 +

import pandas as pd
import numpy as np
from sklearn import *
from sklearn.tree import export_graphviz
from mlxtend.plotting import plot_confusion_matrix
import matplotlib.pyplot as plt
df = pd.read_csv("https://raw.githubusercontent.com/abulbasar/data/master/credit-default.csv")
target = "default"
y = np.where(df[target] == 2, 1, 0)
X = df.copy() # features ... label is not included
del X[target]
X = pd.get_dummies(X, drop_first=True) # handle catergorical variables ... one hot encoding
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y
                            , test_size = 0.3, random_state = 1)
pipe = pipeline.Pipeline([
    #("poly", preprocessing.PolynomialFeatures(degree=5, include_bias=False)),
    #("scaler", preprocessing.StandardScaler()),
    ("est", tree.DecisionTreeClassifier(max_depth=3))
])
                                      
pipe.fit(X_train, y_train)
y_train_pred = pipe.predict(X_train)
y_test_pred = pipe.predict(X_test)
print("accuracy", metrics.accuracy_score(y_test, y_test_pred),
"\nprecision", metrics.precision_score(y_test, y_test_pred),
"\nrecall", metrics.recall_score(y_test, y_test_pred))
plot_confusion_matrix(metrics.confusion_matrix(y_test, y_test_pred))
est = pipe.steps[-1][-1]
export_graphviz(est, out_file = "tree.dot", feature_names = X.columns, filled=True)

Install graphviz using MSI on windows machine

https://graphviz.gitlab.io/_pages/Download/Download_windows.html

For the dot command to work, make sure you include the dot executable in the environment PATH variable. To do this, open command prompt and run the following command.

c:/> set PATH=%PATH%;”C:\Program Files (x86)\Graphviz2.38\bin\”

c:/> dot -Tpng tree.dot -o tree.png