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.

Comparing Machine Learning Classifiers for Crop Mapping in the Philippines

This notebook compares six machine learning classifiers — Minimum Distance, K-Nearest Neighbors (KNN), Support Vector Machines (SVM), Decision Trees (CART), Random Forest, and Gradient Tree Boosting — for crop mapping in the Philippines using Google Earth Engine (GEE) and Python.

The exercise uses Sentinel-2 satellite imagery and training areas digitized over a Philippine study area to map five crop / land cover classes:

Class IDClass Name
0Rice Paddies
1Corn / Maize
2Other Vegetation / Trees
3Built-up / Bare Land
4Water / Fishponds

Requirements

  • An active Google Earth Engine account

  • Authenticated Earth Engine Python API (instructions here)

  • The geemap Python package for visualization

Default Study Area

The default study area is Pangasinan, a province in Central Luzon known for rice, corn, and fishpond production — covering all five target classes. You can change this in the Configuration step below.

Step 1: Initialize and Authenticate Earth Engine

Import the Earth Engine API and authenticate your session before using any GEE functions.

Initialize the Earth Engine API with your GEE project ID. Replace 'your-ee-project-id' with your actual project ID. On first run, you will be prompted to authenticate via your Google account.

Step 2: Import Libraries

Import additional libraries needed for visualization.

Step 3: Configuration — Edit Before Running

Edit the settings in the cell below before running the rest of the notebook.


Place your boundary and training area files in a data/ folder next to this notebook:

ml-gee/
├── ml-gee-ph.ipynb
└── data/
    ├── boundary.geojson        ← Study area polygon
    └── training_areas.geojson  ← Training polygons with field 'Cl_Id'

Supported formats: .geojson or .shp (must be in WGS84 / EPSG:4326).

Your training areas file must have an integer field named Cl_Id with values:

ValueClass
0Rice Paddies
1Corn / Maize
2Other Vegetation / Trees
3Built-up / Bare Land
4Water / Fishponds

Option B — GEE Administrative Boundary (USE_LOCAL_FILES = False)

Uses the FAO GAUL Level 1 (Province) dataset from GEE for the boundary. You must also provide a GEE asset path for your training areas.

Step 4: Load Study Area Boundary and Training Areas

Load the study area boundary and training areas using the option configured above.

Step 5: Prepare Sentinel-2 Imagery

Preprocess Sentinel-2 Surface Reflectance imagery for the study area. Cloud and cirrus pixels are removed using the QA60 band. Images within the configured date range are filtered by cloud cover percentage and a median composite is generated and clipped to the boundary.

The default date range covers the dry season (January–April) in Central Luzon — a period with lower cloud cover and active dry-season rice cultivation in Pangasinan.

Step 6: Prepare Training Data

The training areas are rasterized using the Cl_Id class field. The composite image is sampled at those rasterized locations using stratified random sampling to ensure proportional representation of each crop class.

Class IDClass NameColor
0Rice PaddiesYellow
1Corn / MaizeLight Green
2Other Vegetation / TreesDark Green
3Built-up / Bare LandGrey
4Water / FishpondsBlue

The sampled dataset is split 70% for training and 30% for validation.

Step 7: Define and Train Machine Learning Classifiers

Six classifiers are initialized and a helper function train_classifier is defined to train any of them on the specified dataset and bands:

  1. Minimum Distance — assigns each pixel to the nearest class centroid in feature space

  2. K-Nearest Neighbors (KNN) — classifies based on the k=5 most similar training samples

  3. Support Vector Machine (SVM) — RBF kernel with gamma=0.5; finds optimal class boundaries

  4. Decision Tree (CART) — builds a single tree of if/then rules from training data

  5. Random Forest — ensemble of 100 decision trees; robust to noise and overfitting

  6. Gradient Tree Boosting (GTB) — 50 trees built sequentially; often highest accuracy

Step 8: Define the Classification Function

The train_run_classification function selects, trains, and applies a chosen classifier to the input features, then clips the result to the boundary.

Crop class color palette:

ColorClass
🟡 Yellow #F5D33ARice Paddies
🟢 Light Green #90EE90Corn / Maize
🌲 Dark Green #228B22Other Vegetation / Trees
⬜ Grey #808080Built-up / Bare Land
🔵 Blue #1E90FFWater / Fishponds

Step 9: Run All Classifiers

Train all six classifiers and generate classified crop maps.

Step 10: Display Crop Maps

Each classified crop map is displayed with a legend showing the five crop / land cover classes.

Minimum Distance — Crop Map

Display the crop map derived from the Minimum Distance classifier.

K-Nearest Neighbors (KNN) — Crop Map

Display the crop map derived from the KNN classifier.

Support Vector Machine (SVM) — Crop Map

Display the crop map derived from the SVM (RBF kernel) classifier.

Decision Tree (CART) — Crop Map

Display the crop map derived from the Decision Tree (CART) classifier.

Random Forest — Crop Map

Display the crop map derived from the Random Forest classifier.

Gradient Tree Boosting — Crop Map

Display the crop map derived from the Gradient Tree Boosting classifier.

Step 11: Classification Accuracy Assessment

Evaluate the accuracy of each classifier using the 30% validation set held back during the train/test split.

The compute_metrics function applies the trained classifier to the validation data and computes:

  • Error Matrix — shows predicted vs. actual class counts

  • User Accuracy (Precision per class) — how reliable is the map for each class?

  • Producer Accuracy (Recall per class) — how well is each class detected?

  • Overall Accuracy — percentage of correctly classified pixels