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 different scikit-learn classifiers for land cover mapping

Introduction

This lab introduces participants to the application of machine learning algorithms for land cover classification using satellite imagery. Land cover classification is a fundamental task in remote sensing, supporting a wide range of applications including environmental monitoring, urban planning, agriculture, and natural resource management.

In this lab, we explore and compare the performance of four commonly used machine learning classifiers: k-nearest neighbor (KNN), support vector machines (SVM), decision trees, and random forest. These classifiers are implemented using the Scikit-Learn library.

Dataset:

  • Sentinel-2 image: S2_JFM_2025.tif (Jan–Mar 2025 composite)

  • Training areas: training_polygons.geojson (7 land cover classes)

Goal

By the end of this lab, participants will have a foundational understanding of how to apply and evaluate basic machine learning algorithms for classifying land cover types from Earth observation data.

Imports and Setup

Install libraries

Install any additional libraries not installed by default.

Import libraries

Import the necessary libraries (pandas, numpy, scikit-learn, rasterio, geopandas, etc.).

Define paths and variables

Define the paths to the Sentinel-2 image and training polygon GeoJSON. We also define the spectral bands used as input features, the class labels, and the visualization palette.

Load the Sentinel-2 Imagery

We use rasterio to open the .tif file and inspect its properties. A true-color composite (B4, B3, B2) is displayed for a quick visual check.

Load and Prepare Training Data

Training data is provided as polygon geometries in training_polygons.geojson. We use rasterio.mask to sample pixel values from the Sentinel-2 image within each polygon. Each sampled pixel becomes one training sample, with its spectral band values as features and the polygon’s Cl_Id as the class label.

Exploratory Data Analysis (EDA)

EDA is a critical initial step in the geospatial ML workflow. It uses statistical summaries and visualizations to gain insights into the dataset before applying any classification algorithm.

Class distribution

First, create a count plot to show the distribution of training samples per land cover class.

Scatter plot

Create a scatter plot of two spectral bands colored by class. This helps identify how well different classes separate in feature space.

Pairwise scatter plots

Pairwise scatter plots visualize how each pair of spectral bands relates to one another across classes, helping to spot patterns and assess feature separability.

3D scatter plot

A 3D scatter plot visualizes three bands simultaneously (Blue B2, Red B4, NIR B8), revealing cluster separation that may not be visible in 2D.

t-SNE projection

t-SNE reduces the high-dimensional spectral feature space to 2D, helping to visualize cluster structure and class separability across all bands at once.

Box plots

Box plots show the distribution of reflectance values per band for each land cover class, helping to identify which bands are most discriminative.

Train and Evaluate Multiple Classifiers

We train four classifiers: KNN, SVM, Decision Tree, and Random Forest, then compare their performance using accuracy, confusion matrices, and classification reports.

Model validation

Compare each model’s performance using accuracy, classification report, and confusion matrix.

Land Cover Classification

We use the Random Forest classifier (typically the best performer) to predict a class label for every pixel in the Sentinel-2 image.

i. Read the full image stack

ii. Reshape for prediction

Scikit-Learn expects a 2D array where each row is a pixel and each column is a band. Reshape from (bands, height, width) → (height×width, bands).

iii. Predict and reshape back

Run the Random Forest classifier on every pixel and reshape the result back to the original image dimensions.

Visualize the land cover map

Display the resulting map with the class color palette.

Export the Land Cover Map

Save the classification result as a GeoTIFF, preserving the original CRS and spatial transform.

👩🏽‍💻 TEST IT OUT

What are ways that you can improve the results of your model?