前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Google Earth Engine(GEE)——协方差、特征值、特征向量主成分分析(部分)

Google Earth Engine(GEE)——协方差、特征值、特征向量主成分分析(部分)

作者头像
此星光明
发布2024-02-02 08:17:24
1920
发布2024-02-02 08:17:24
举报

主成分(PC)的变换(又称为Karhunen-Loeve变换)是一种光谱转动所需要的光谱相关的图像数据,并输出非相关数据。PC 变换通过特征分析对输入频带相关矩阵进行对角化来实现这一点。要在 Earth Engine 中执行此操作,请在阵列图像上使用协方差缩减器并eigen()在结果协方差阵列上使用该命令。为此目的考虑以下函数(这是完整示例的一部分 ):

先看函数:

eigen()特征向量

代码语言:javascript
复制
计算 A 行 A 列的二维方形数组的实数特征向量和特征值。返回一个包含 A 行和 A+1 列的数组,其中每一行在第一列中包含一个特征值,在其余 A 列中包含相应的特征向量。行按特征值降序排列。
代码语言:javascript
复制
此实现使用来自 https://ejml.org 的 DecompositionFactory.eig()。

Computes the real eigenvectors and eigenvalues of a square 2D array of A rows and A columns. Returns an array with A rows and A+1 columns, where each row contains an eigenvalue in the first column, and the corresponding eigenvector in the remaining A columns. The rows are sorted by eigenvalue, in descending order.

This implementation uses DecompositionFactory.eig() from https://ejml.org.

Arguments:

this:input (Array):

代码语言:javascript
复制
输入(数组):
用于计算特征值分解的二维方形数组。

A square, 2D array from which to compute the eigenvalue decomposition.

Returns: Array

ee.Reducer.centeredCovariance()

代码语言:javascript
复制
创建一个 reducer,将一些长度相同的一维数组减少到 NxN 形状的协方差矩阵。警告:此reducer 要求数据以均值为中心。

Creates a reducer that reduces some number of 1-D arrays of the same length N to a covariance matrix of shape NxN. WARNING: this reducer requires that the data has been mean centered.

No arguments.
Returns: Reducer

matrixMultiply(image2)矩阵乘法

代码语言:javascript
复制
返回 image1 和 image2 中每个匹配的波段对的矩阵乘法 A*B。如果 image1 或 image2 只有 1 个波段,则将其用于另一个图像中的所有波段。如果图像具有相同数量的波段,但名称不同,则它们按自然顺序成对使用。输出波段以两个输入中较长的命名,或者如果它们的长度相等,则按 image1 的顺序命名。输出像素的类型是输入类型的并集。

Returns the matrix multiplication A*B for each matched pair of bands in image1 and image2. If either image1 or image2 has only 1 band, then it is used against all the bands in the other image. If the images have the same number of bands, but not the same names, they're used pairwise in the natural order. The output bands are named for the longer of the two inputs, or if they're equal in length, in image1's order. The type of the output pixels is the union of the input types.

Arguments:

this:image1 (Image):

The image from which the left operand bands are taken.

image2 (Image):

The image from which the right operand bands are taken.

Returns: Image

代码:

代码语言:javascript
复制
构建一个函数,包含协方差计算,转换数组
var getPrincipalComponents = function(centered, scale, region) {
  // 将图像的波段折叠成每个像素的一维阵列。
  var arrays = centered.toArray();

  // 计算区域内波段的协方差。
  var covar = arrays.reduceRegion({
    reducer: ee.Reducer.centeredCovariance(),
    geometry: region,
    scale: scale,
    maxPixels: 1e9
  });

  // 获取“数组”协方差结果并转换为数组。
  // 这表示区域内的带间协方差。
  var covarArray = ee.Array(covar.get('array'));

  // 执行特征分析并将值和向量分开。
  var eigens = covarArray.eigen();

  // 这是特征值的 P 长度向量。
  var eigenValues = eigens.slice(1, 0, 1);
  // 这是一个在行中具有特征向量的 PxP 矩阵。
  var eigenVectors = eigens.slice(1, 1);

  // 将数组图像转换为二维数组以进行矩阵计算。
  var arrayImage = arrays.toArray(1);

  // 左乘图像数组乘以特征向量矩阵。
  var principalComponents = ee.Image(eigenVectors).matrixMultiply(arrayImage);

  // 将特征值的平方根转换为 P 波段图像。
  var sdImage = ee.Image(eigenValues.sqrt())
      .arrayProject([0]).arrayFlatten([getNewBandNames('sd')]);

  // 将 PC 转换为 P 波段图像,由 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);
};

// 这个函数基本上涵盖了主成分分析和归一化的过程
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-02-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Arguments:
  • Returns: Array
  • No arguments.
  • Returns: Reducer
  • Arguments:
  • Returns: Image
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档