Demystifying Decision Trees
Last Updated on March 30, 2023 by Editorial Team
Author(s): Andrea Ianni
Originally published on Towards AI.
Explained from scratch, step by step

Some time ago, I found myself having to explain the tree-based algorithms to a person who was into mathematics… but with zero knowledge of data science. So, I decided to ignore the classic toy datasets and started completely from scratch, from a bunch of 2-dimensional points.
So, after some basic imports
import pandas as pdfrom sklearn import treeimport seaborn as sns
I drew 6 points on a sheet: 4 blue, and 2 orange:
X = [[0, 0], [1, 1], [2,1], [1,2], [2,2], [0.5, 1.5]]Y = [0, 1, 1, 1, 1, 0]example = pd.DataFrame(X)example["target"] = Ydisplay(example)colors = example["target"].map({0:"orange", 1:"blue"})sns.scatterplot(data=example, x=0, y=1, c=colors, s=200)
It is a… Read the full blog for free on Medium.
Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming a sponsor.
Published via Towards AI
Towards AI Academy
We Build Enterprise-Grade AI. We'll Teach You to Master It Too.
15 engineers. 100,000+ students. Towards AI Academy teaches what actually survives production.
Start free — no commitment:
→ 6-Day Agentic AI Engineering Email Guide — one practical lesson per day
→ Agents Architecture Cheatsheet — 3 years of architecture decisions in 6 pages
Our courses:
→ AI Engineering Certification — 90+ lessons from project selection to deployed product. The most comprehensive practical LLM course out there.
→ Agent Engineering Course — Hands on with production agent architectures, memory, routing, and eval frameworks — built from real enterprise engagements.
→ AI for Work — Understand, evaluate, and apply AI for complex work tasks.
Note: Article content contains the views of the contributing authors and not Towards AI.