Leon
Leon

Reputation: 429

Plot a Single XGBoost Decision Tree

I am using method on https://machinelearningmastery.com/visualize-gradient-boosting-decision-trees-xgboost-python/ to plot a XGBoost 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()

As I got 150 features,the plot looks quite small for all split points,how to draw a clear one or save in local place or any other ways/ideas could clearly show this ‘tree’ is quite appreciated enter image description here

Upvotes: 23

Views: 52372

Answers (5)

pplonski
pplonski

Reputation: 5839

According to the artcile 4 ways to visualize tree from Xgboost there are following ways to visualize single tree from Xgboost:

  • using matplotlib and xgboost.plot_tree() package,
  • export to graphiviz (.dot file)
  • visualization using dtreeviz package
  • visualization using supetree package

The first three methods are based on graphiviz library. The supertree is using D3.js library to make interactive visualization of single decision tree from Xgboost. It works very nice in Python notebooks :)

Here is an example Python code for supertree:

from supertree import SuperTree

st = SuperTree(
    model, 
    X, 
    y
)
# Visualize the tree
st.show_tree(which_tree=1)

Example visualization: xgboost tree visualization

Upvotes: 1

Learning is a mess
Learning is a mess

Reputation: 8277

To add to Serk's answer, you can also resize the figure before displaying it:

# ...
plot_tree(model)
plt.gcf().set_size_inches(18.5, 10.5)
plt.show()

Upvotes: 6

morienor
morienor

Reputation: 379

You can try using the to_graphviz method instead - for me it results in a much more clear picture.

xgb.to_graphviz(xg_reg, num_trees=0, rankdir='LR')

However, most likely you will have issues with the size of that output.

In this case follow this: How can I specify the figsize of a graphviz representation of a decision tree?

Upvotes: 6

confused_zebra
confused_zebra

Reputation: 136

I found this workaround on github, which also gives better images with the drawback that you have to open the .png file after.

xgb.plot_tree(bst, num_trees=2)
fig = matplotlib.pyplot.gcf()
fig.set_size_inches(150, 100)
fig.savefig('tree.png')

Upvotes: 5

Serk
Serk

Reputation: 452

I had the same problem recently and the only way I found is by trying diffent figure size (it can still be bluery with big figure. For exemple, to plot the 4th tree, use:

fig, ax = plt.subplots(figsize=(30, 30))
xgb.plot_tree(model, num_trees=4, ax=ax)
plt.show()

To save it, you can do

plt.savefig("temp.pdf")

Also, each tree seperates two classes so you have as many tree as class.

Upvotes: 23

Related Questions