Creating Decision Trees with Supertree

shawncasserly
3 min readSep 21, 2024

--

Supertree is a relatively new(to me) Python package that helps to visualize and interpret decision trees for developers.

pip install supertree

Here’s the link to the package and more information. The creator has a ton of examples and documentation to look through!
Supertree Github

Please keep in mind this is my first article. I am no expert in writing or Data Science, so there is plenty room for me to grow in this. Any feedback is much appreciated!

While diving into Jupyter Notebooks, I’ve been exploring various Machine Learning (ML) concepts, starting with decision trees. These models are often recommended for beginners due to their intuitive structure. Using Supertree has significantly improved my understanding of how to choose the right attributes for splitting the tree and allowed me to visualize it in a clear, accessible way.

What are decision trees and when should I use them?

The what:

Decision trees are a type of machine learning algorithm used for classification and regression tasks. They work by splitting data into branches based on feature values, creating a tree-like model of decisions. Each internal node represents a decision based on a feature, each branch represents the outcome of that decision, and each leaf node represents a final classification or prediction. Imagine a flowchart where each question you answer leads you down a different path until you reach a final decision. Each step in the tree asks a yes/no question about your data, and the answers guide you to the end result.

The when:

Decision trees are great to use when you need a clear and easy-to-understand model for making decisions. For example, if you’re trying to decide whether to play outside based on the weather, a decision tree can help. It might ask questions like “Is it sunny?” and “Is it warm?” to guide you to a decision. Use them when you want to see how different factors influence the outcome, as the tree structure makes it easy to visualize.

Using Supertree and code example

## This example is from supertree GitHub.
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris
from supertree import SuperTree # <- import supertree

# Load the iris dataset
iris = load_iris()
X, y = iris.data, iris.target

# Train model
model = DecisionTreeClassifier()
model.fit(X, y)

# Initialize supertree
super_tree = SuperTree(model, X, y, iris.feature_names, iris.target_names)

# show tree in your notebook
super_tree.show_tree()

Supertree is an invaluable tool that enables users to explore any segment of a tree structure, whether at the broader top level or the more detailed end nodes. Its customizable color palettes enhance accessibility, particularly for those with color vision deficiencies. While the example utilizing the iris dataset is straightforward and easy to comprehend, I’m eager to apply Supertree to some other projects I have in mind!

Thank you for reading this! I welcome any feedback or suggestions to help me further my learning in this dynamic and evolving field of data science!

--

--

shawncasserly
shawncasserly

Written by shawncasserly

Analytics and Automation Professional, interests in Data Science/Engineering