Phân tích giá trị riêng

Biến đổi thành phần chính (PC) (còn gọi là biến đổi Karhunen-Loeve) là một phép xoay phổ lấy dữ liệu hình ảnh có tương quan phổ và xuất dữ liệu không tương quan. Biến đổi PC thực hiện việc này bằng cách đưa ma trận tương quan băng đầu vào vào dạng ma trận đối xứng qua phép phân tích Eigen. Để thực hiện việc này trong Earth Engine, hãy sử dụng trình giảm độ biến thiên đồng thời trên hình ảnh mảng và lệnh eigen() trên mảng độ biến thiên đồng thời thu được. Hãy xem xét hàm sau đây cho mục đích đó (ví dụ về hàm này trong ứng dụng có sẵn dưới dạng tập lệnh Trình soạn thảo mãsổ tay Colab).

var getPrincipalComponents = function(centered, scale, region) {
  // Collapse the bands of the image into a 1D array per pixel.
  var arrays = centered.toArray();

  // Compute the covariance of the bands within the region.
  var covar = arrays.reduceRegion({
    reducer: ee.Reducer.centeredCovariance(),
    geometry: region,
    scale: scale,
    maxPixels: 1e9
  });

  // Get the 'array' covariance result and cast to an array.
  // This represents the band-to-band covariance within the region.
  var covarArray = ee.Array(covar.get('array'));

  // Perform an eigen analysis and slice apart the values and vectors.
  var eigens = covarArray.eigen();

  // This is a P-length vector of Eigenvalues.
  var eigenValues = eigens.slice(1, 0, 1);
  // This is a PxP matrix with eigenvectors in rows.
  var eigenVectors = eigens.slice(1, 1);

  // Convert the array image to 2D arrays for matrix computations.
  var arrayImage = arrays.toArray(1);

  // Left multiply the image array by the matrix of eigenvectors.
  var principalComponents = ee.Image(eigenVectors).matrixMultiply(arrayImage);

  // Turn the square roots of the Eigenvalues into a P-band image.
  var sdImage = ee.Image(eigenValues.sqrt())
      .arrayProject([0]).arrayFlatten([getNewBandNames('sd')]);

  // Turn the PCs into a P-band image, normalized by SD.
  return principalComponents
      // Throw out an an unneeded dimension, [[]] -> [].
      .arrayProject([0])
      // Make the one band array image a multi-band image, [] -> image.
      .arrayFlatten([getNewBandNames('pc')])
      // Normalize the PCs by their SDs.
      .divide(sdImage);
};

Hãy xem trang Môi trường Python để biết thông tin về API Python và cách sử dụng geemap để phát triển tương tác.

import ee
import geemap.core as geemap
def get_principal_components(centered, scale, region):
  # Collapse bands into 1D array
  arrays = centered.toArray()

  # Compute the covariance of the bands within the region.
  covar = arrays.reduceRegion(
      reducer=ee.Reducer.centeredCovariance(),
      geometry=region,
      scale=scale,
      maxPixels=1e9,
  )

  # Get the 'array' covariance result and cast to an array.
  # This represents the band-to-band covariance within the region.
  covar_array = ee.Array(covar.get('array'))

  # Perform an eigen analysis and slice apart the values and vectors.
  eigens = covar_array.eigen()

  # This is a P-length vector of Eigenvalues.
  eigen_values = eigens.slice(1, 0, 1)
  # This is a PxP matrix with eigenvectors in rows.
  eigen_vectors = eigens.slice(1, 1)

  # Convert the array image to 2D arrays for matrix computations.
  array_image = arrays.toArray(1)

  # Left multiply the image array by the matrix of eigenvectors.
  principal_components = ee.Image(eigen_vectors).matrixMultiply(array_image)

  # Turn the square roots of the Eigenvalues into a P-band image.
  sd_image = (
      ee.Image(eigen_values.sqrt())
      .arrayProject([0])
      .arrayFlatten([get_new_band_names('sd')])
  )

  # Turn the PCs into a P-band image, normalized by SD.
  return (
      # Throw out an an unneeded dimension, [[]] -> [].
      principal_components.arrayProject([0])
      # Make the one band array image a multi-band image, [] -> image.
      .arrayFlatten([get_new_band_names('pc')])
      # Normalize the PCs by their SDs.
      .divide(sd_image)
  )

Dữ liệu đầu vào của hàm là một hình ảnh có giá trị trung bình bằng 0, một tỷ lệ và một vùng để thực hiện phân tích. Xin lưu ý rằng trước tiên, hình ảnh đầu vào cần được chuyển đổi thành hình ảnh mảng 1 chiều, sau đó giảm bằng ee.Reducer.centeredCovariance(). Mảng được trả về bằng phép rút gọn này là ma trận hiệp phương sai-phương sai đối xứng của dữ liệu đầu vào. Sử dụng lệnh eigen() để lấy các giá trị riêng và vectơ riêng của ma trận hiệp phương sai. Ma trận do eigen() trả về chứa các giá trị riêng ở vị trí thứ 0 của trục 1. Như đã trình bày trong hàm trước, hãy sử dụng slice() để tách các giá trị riêng và các vectơ riêng. Mỗi phần tử dọc theo trục 0 của ma trận eigenVectors là một vectơ riêng. Như trong ví dụ về mũ có tua (TC), hãy thực hiện phép biến đổi bằng cách nhân ma trận arrayImage với các vectơ riêng. Trong ví dụ này, mỗi phép nhân vectơ riêng sẽ dẫn đến một PC.