In this article, I am going to show you how to plot the decision trees generated by XGBoost models. First, we have to install graphviz (both python library and executable files)

!pip install graphviz
!apt-get install graphviz

When the graphviz library is installed, we can train an XGBoost model (in this example, I am going to train it using the Titanic dataset).

from xgboost import XGBClassifier
from xgboost import plot_tree
#(...) other imports

#(...) loading the dataset and data preprocessing

model = XGBClassifier()
model.fit(X_train, y_train, verbose=True, eval_set=[(X_test, y_test)])

To display the trees, we have to use the plot_tree function provided by XGBoost.

It is important to change the size of the plot because the default one is not readable. The num_trees indicates the tree that should be drawn not the number of trees, so when I set the value to two, I get the second tree generated by XGBoost.

plot_tree(model, num_trees=1)
fig = plt.gcf()
fig.set_size_inches(30, 15)

Want to build AI systems that actually work?

Download my expert-crafted GenAI Transformation Guide for Data Teams and discover how to properly measure AI performance, set up guardrails, and continuously improve your AI solutions like the pros.

Older post

Smoothing time series in Python using Savitzky–Golay filter

Smoothing Bitcoin price time-series

Newer post

Human bias in A/B testing

Underpowered tests, true negative, and ignored tests results

Are you looking for an experienced AI consultant? Do you need assistance with your RAG or Agentic Workflow?
Book a Quick Consultation, send me a message on LinkedIn. Book a Quick Consultation or send me a message on LinkedIn

>