Whole process of GEE principal component analysis

. background

Principal component analysis is an important method of data dimensionality reduction. At present, there is no complete GEE code and tutorial on Chinese websites. My graduation thesis also uses the principal component method, so I have feelings for it, so I wrote this blog.

1. Introduction

Principal component analysis is to recombine many relevant data indicators into a new group of indicators. The newly formed indicators are not related to each other, and the first several principal components can represent most of the information of the original data.

In GEE, there may be a large number of bands. At this time, we can consider using principal component analysis to generate only two or three principal components to reduce the follow-up workload.

2. Code ideas

3. Practical operation

3.1 data screening and preprocessing

This step is mainly to select the sentry image of the study area (Lezhi county, Ziyang City, Sichuan Province), and mosaic and cut the screened data according to the administrative boundary

//Filter data
var sentImages = ee.ImageCollection(Sentinel)
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 10))
.filterDate("2021-08-01", "2021-08-08")

//Mosaic and crop data
var sentmosaic = sentImages.mosaic();
var sentImage = sentmosaic.clip(LeZhiXian);

//Load study area image layer
Map.addLayer(sentImage, trueColor, "Lezhi true color");

3.2 data screening and preprocessing

Select the original image band for principal component analysis, and set the resolution of the principal component analysis image. Pretreatment (covariance reduction, etc.) was performed before principal component analysis.

//Band selection for principal component analysis
var bands=["B1","B2","B3","B4","B5","B6","B7","B8","B9","B11","B12"]
sentImage =sentImage.select(bands)

// Parameter settings input to the principal component function
var region = LeZhiXian;
var image =  sentImage.select(bands);
var scale = 10;
var bandNames = image.bandNames();

//Data average
var meanDict = image.reduceRegion({
    reducer: ee.Reducer.mean(),
    geometry: region,
    scale: scale,
    maxPixels: 1e9
var means = ee.Image.constant(meanDict.values(bandNames));
var centered = image.subtract(means);

3.3 principal component analysis

This part of the code is copied from the official document of Google earth engine. For the input image, it is used to convert it into an array, calculate the orthogonal matrix, calculate the principal component load and so on.

//Principal component analysis function
var getPrincipalComponents = function(centered, scale, region) {
    // Convert image to one-dimensional array
    var arrays = centered.toArray();

    // Calculate correlation coefficient matrix
    var covar = arrays.reduceRegion({
      reducer: ee.Reducer.centeredCovariance(),
      geometry: region,
      scale: scale,
      maxPixels: 1e9
    // Gets the array covariance result and converts it to an array.
    // Covariance between bands
    var covarArray = ee.Array(covar.get('array'));
    // Perform feature analysis and segment values and vectors.
    var eigens = covarArray.eigen();
    // P vector length of eigenvalue
    var eigenValues = eigens.slice(1, 0, 1);
    //Calculate principal component load
    var eigenValuesList = eigenValues.toList().flatten()
    var total = eigenValuesList.reduce(ee.Reducer.sum())
    var percentageVariance = eigenValuesList.map(function(item) {
      return (ee.Number(item).divide(total)).multiply(100).format('%.2f')
    print("Load ratio of each principal component", percentageVariance)  
    // PxP matrix, whose eigenvector is row.
    var eigenVectors = eigens.slice(1, 1);
    // Convert image to 2D array
    var arrayImage = arrays.toArray(1);
    //Left multiply image array using eigenvector matrix
    var principalComponents = ee.Image(eigenVectors).matrixMultiply(arrayImage);

    // The square root of the eigenvalue is converted into a P-band image.
    var sdImage = ee.Image(eigenValues.sqrt())
    //Convert PC to P-band image and standardize it by SD.
      // Throw an unnecessary dimension, [[]] - > [].
      // Make the single band array image a multi band image, [] - > image.
      // Normalize the PC via SDs.
    return principalComponents
//Principal component analysis was carried out to obtain the analysis results
var pcImage = getPrincipalComponents(centered, scale, region);
// Visualization of main components
Map.addLayer(pcImage, {bands: ['pc3', 'pc2', 'pc1'], min: -2, max: 2}, 'Sentinel 2 - PCA');

3.4 data export

Select the principal component band to be exported. The first three bands are exported here, because the cumulative contribution rate of the first three bands exceeds 95%, which is completely enough.

//Select the band to export
var pcImage_output =pcImage.select(['pc1', 'pc2', 'pc3'])
//derived function
  image: pcImage_output,
  description: 'LeZhiXian_Sentinel_PAC',
  scale: 10,
  maxPixels: 1e10

Click Run to download the data, and the expected image can be obtained in about 10 minutes in the research area of a county.

Import the downloaded image into arcgis or envi for the analysis you need. It is mentioned here that in deep learning, principal component analysis can also be used to process multi band images to obtain three bands for training and prediction.

4. Test link


Posted on Wed, 10 Nov 2021 12:32:07 -0500 by benmay.org