Principal component analysis for multi-spectral data 30046e9b60cf47779b9e17309bd27373


Principal Component Analysis (PCA) is a popular technique for dimensionality reduction. It can be used to explore patterns in high-dimensional data and assist unsupervised learning.

Principal components are a series of linear combinations of the original variables, among which the first principal component accounts for the greatest variance within a dataset. Each subsequent principal component accounts for the next greatest possible variance and is uncorrelated with the previously defined components.

This technique is useful for understanding Sentinel-2 data as images are captured in 12 spectral bands but only 3 variables can be visualized in a RGB composite. PCA can also be applied to timeseries data to investigate temporal evolution patterns for different land cover types.


This notebook demonstrates a principal component analysis for Sentinel-2 multi-spectal data. Following steps are covered:

  1. Loading Sentinel-2 multi-spectral data.

  2. Applying PCA to transform and visualize data.

Getting started

To run this analysis, run all the cells in the notebook, starting with the “Load packages” cell.

Load packages

Import Python packages that are used for the analysis.

%matplotlib inline

import datacube
from sklearn.decomposition import PCA

import sys
sys.path.insert(1, '../Tools/')
from dea_tools.datahandling import load_ard
from dea_tools.plotting import rgb
from dea_tools.classification import sklearn_flatten, sklearn_unflatten

Connect to the datacube

Connect to the datacube so we can access DEA data.

dc = datacube.Datacube(app='Principal_component_analysis')

Analysis parameters

This section defines the analysis parameters, including

  • lat, lon: center lat/lon for the area of interest

  • buffer: the window size around the centre lat/lon for the area of interest

  • time_range: time period to be investigated

  • min_gooddata: minimum fraction of good-data in the image before it while be returned

  • bands: spectral bands to be explored

The default location is the Norman River, Qld.

To limit overall memory usage, if a larger analysis window or higher resolution is desired, the time period should be reduced accordingly.

lat, lon = -17.5687, 140.9653
buffer = 0.05
time_range = ('2019-12', '2020-03')
bands = [
    'nbart_blue', 'nbart_green', 'nbart_red', 'nbart_red_edge_1', 'nbart_red_edge_2',
    'nbart_red_edge_3', 'nbart_nir_2', 'nbart_swir_2', 'nbart_swir_3'
min_gooddata = 0.99

Loading cloud-masked Sentinel-2 multi-spectral data

# Define the query dict
query = {
    'time': time_range,
    'x': (lon - buffer, lon + buffer),
    'y': (lat + buffer, lat - buffer),
    'output_crs': 'epsg:3577',
    'resolution': (-20, 20),
    'group_by': 'solar_day',
    'measurements': bands
# Load the data
ds = load_ard(dc=dc,
              products=['ga_s2am_ard_3', 'ga_s2bm_ard_3'],
Finding datasets
Counting good quality pixels for each time step using fmask
Filtering to 6 out of 24 time steps with at least 99.0% good quality pixels
Loading 6 time steps
# Visualize data using selected input spectral bands
    bands=['nbart_swir_2', 'nbart_nir_2', 'nbart_red_edge_1'],

Applying PCA to transform and visualize data

To perform a PCA, data is first transformed into a numpy array that can be used by sklearn using the DEA function sklearn_flatten.

x = sklearn_flatten(ds)

A PCA model is generated with 3 principal components and fitted on the data.

pca = PCA(n_components=3)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with

We can investigate how much variance is accounted for in each principal component. In the default example, the first principal component accounts for a much high variance than the next two.

This step can help determine whether more principal components are needed.

print('Relative variance in principal components:',
Relative variance in principal components: [0.86241157 0.07656634 0.05398086]

The input data can now be transformed into this new reference space and rearranged into an xarray.Dataset compatible with our input data.

predict = pca.transform(x)
out = sklearn_unflatten(predict, ds)
out = out.to_dataset(dim=out.dims[0]).transpose('time', 'y', 'x')

Visualise PCA results

# Plot PCA bands
    bands=[2, 1, 0],
    percentile_stretch=[0.2, 0.90])

Additional information

License: The code in this notebook is licensed under the Apache License, Version 2.0. Digital Earth Australia data is licensed under the Creative Commons by Attribution 4.0 license.

Contact: If you need assistance, please post a question on the Open Data Cube Slack channel or on the GIS Stack Exchange using the open-data-cube tag (you can view previously asked questions here). If you would like to report an issue with this notebook, you can file one on GitHub.

Last modified: April 2023

Compatible datacube version:



Tags: sandbox compatible, NCI compatible, sentinel 2, rgb, sklearn_flatten, sklearn_unflatten, principal component analysis, statistics