Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Demo 1: Decision Trees


Learning Objectives

By the end of this notebook, you will be able to:

  1. Understand Decision Trees: Explain how a single decision tree makes predictions through recursive splitting to partition data into discrete classes, such as water and non-water.

  2. Transform Data for ML: Practice turning raw geospatial data into numbers and table formats so a computer can find patterns using code and math.

  3. Execute a Supervised Workflow: Perform an end-to-end supervised learning project where the algorithm learns classification rules from labeled examples to generate a predictive map

A. Introduction and Setup

Let’s start by importing the necessary libraries and setting up our environment for reproducible results.

✓ Libraries imported successfully!
✓ Random state set to: 42
✓ NumPy version: 2.0.2
✓ Rasterio version: 1.4.4

B. Decision Trees Interactive Demo

What is a Decision Tree?

A Decision Tree is a supervised learning algorithm that makes predictions by learning a series of if-then-else decision rules from data. Think of it like a flowchart:

Is NDVI > 0.3?
├─ Yes: Is NIR > 0.5?
│  ├─ Yes: Forest
│  └─ No: Grassland
└─ No: Is SWIR < 0.2?
   ├─ Yes: Water
   └─ No: Urban

Key Concepts:

  • Root Node: The first decision point (top of the tree)

  • Internal Nodes: Intermediate decision points

  • Leaf Nodes: Final predictions (bottom of the tree)

  • Splitting: How the algorithm decides which feature and threshold to use

  • Depth: Number of levels in the tree (deeper = more complex)

Let’s Build a Simple Example:

1. Data Loading and Feature Engineering

In this section, we load the multispectral satellite data and create proxy labels for water detection using spectral indices (NDVI and NDWI).

NDVI (Normalized Difference Vegetation Index):

  • Used to quantify vegetation greenness and is useful in understanding vegetation density.

  • Formula: NDVI = (NIR + Red) / (NIR − Red)

NDWI (Normalized Difference Water Index):

  • Used for delineating and monitoring content changes in surface water

  • Formula: NDWI = (Green + NIR) / (Green − NIR)

✓ Image Dimensions: 74x76
✓ Total Pixels: 5624
✓ Proxy Water Pixels: 3229

2. Model Training and Prediction

We will now train a Decision Tree classifier to recognize “water” based on the spectral signatures of the Green, Red, and NIR bands.

📊 Dataset Statistics:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Dataset shape:      (5624, 3) (Pixels, Bands)
Number of samples:  5624
Number of features: 3 (Green, Red, NIR)
Classes:            [0. 1.] (0: Non-water, 1: Water)
Class distribution: Non-Water: 2395, Water: 3229
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📊 Model Performance:
✓ Training Accuracy: 0.984
✓ Tree Depth:       3
✓ Number of Leaves: 7
✓ Full image prediction complete.

3. Visualization and Results

Finally, we compare our predicted water map against the True Color (RGB) reference image to evaluate the model’s performance.

<Figure size 2000x1000 with 3 Axes>

💡 TIP: Decision Trees are great for EO because they create clear 'if-then' rules
based on spectral thresholds, making the classification process easy to interpret.

Visualize the Tree Structure

Let’s look inside the tree to see the actual decision rules it learned.

<Figure size 2500x1200 with 1 Axes>

How to Read This Tree:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
• Each box is a node with a decision rule (e.g., 'NIR <= 120.5')
• 'gini' measures impurity (0 = pure, 0.5 = mixed)
• 'samples' shows how many training pixels reach this node
• 'value' shows pixel distribution [Non-Water, Water]
• Color intensity indicates class majority (darker Blue = Water, darker Orange = Non-Water)
• Leaf nodes (bottom) make the final classification for your map

Summary and Key Takeaways

Decision Trees

  • Learn hierarchical decision rules through recursive splitting

  • Create axis-aligned decision boundaries

  • Prone to overfitting if too deep

  • Easy to interpret and visualize

Advantages:

  • Easy to understand and visualize

  • No data normalization needed

  • Handles non-linear relationships

  • Feature importance easily extracted

  • Fast prediction

Limitations:

  • Overfitting: Can memorize training data

  • High variance: Small data changes → big tree changes

  • Instability: Greedy algorithm (local optima)

  • Bias: Favor features with many levels


References

  1. CoPhil EO AI/ML Training Programme. (2025). Core Concepts of AI/ML for Earth Observation. https://skotsopoulos-del.github.io/cophil-training-v1.0/day1/sessions/session2.html


End of Demo 1 Notebook