# plot decision tree
from numpy import loadtxt
from xgboost import XGBClassifier
from xgboost import plot_tree
import matplotlib.pyplot as plt
# load data
dataset = loadtxt('pima-indians-diabetes.csv', delimiter=",")
# split data into X and y
X = dataset[:,0:8]
y = dataset[:,8]
# fit model no training data
model = XGBClassifier()
model.fit(X, y)
# plot single tree
plot_tree(model)
plt.show()
The plot_tree() function takes some parameters. You can plot specific graphs by specifying their index to the num_trees argument. For example, you can plot the 5th boosted tree in the sequence as follows:
plot_tree(model, num_trees=4)
1 | plot_tree(model, num_trees=4) |
You can also change the layout of the graph to be left to right (easier to read) by changing the rankdir argument as ‘LR’ (left-to-right) rather than the default top to bottom (UT). For example:
plot_tree(model, num_trees=0, rankdir='LR')
1 | plot_tree(model, num_trees=0, rankdir='LR') |
The result of plotting the tree in the left-to-right layout is shown below.
https://machinelearningmastery.com/visualize-gradient-boosting-decision-trees-xgboost-python/