公告:所有在
2025 年 4 月 15 日之前注册使用 Earth Engine 的非商业项目都必须
验证是否符合非商业性质的资格条件,才能继续使用 Earth Engine。
特征值分析
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
主成分 (PC) 转换(也称为 Karhunen-Loeve 转换)是一种光谱旋转,可获取光谱相关的图像数据并输出不相关的数据。PC 转换通过特征值分析对输入频段相关性矩阵进行对角化来实现这一点。如需在 Earth Engine 中执行此操作,请对数组图像使用协方差减小器,并对生成的协方差数组使用 eigen()
命令。
请考虑使用以下函数来实现此目的(应用中的示例可作为 Code Editor 脚本和 Colab 笔记本使用)。
Code Editor (JavaScript)
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);
};
Python 设置
如需了解 Python API 以及如何使用 geemap
进行交互式开发,请参阅
Python 环境页面。
import ee
import geemap.core as geemap
Colab (Python)
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)
)
该函数的输入是均值为零的图片、缩放比例和要执行分析的区域。请注意,输入图像需要先转换为 1 维数组图像,然后使用 ee.Reducer.centeredCovariance()
进行缩减。此求和运算返回的数组是输入的对称方差-协方差矩阵。
使用 eigen()
命令获取协方差矩阵的特征值和特征向量。eigen()
返回的矩阵包含 1 轴的第 0 个位置上的特征值。如前面的函数所示,使用 slice()
来分隔特征值和特征向量。特征向量矩阵沿 0 轴的每个元素都是特征向量。与流苏帽 (TC) 示例中一样,通过将 arrayImage
与特征向量相乘的矩阵乘法来执行转换。
在此示例中,每次特征向量乘法都会产生一个 PC。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-25。
[null,null,["最后更新时间 (UTC):2025-07-25。"],[[["\u003cp\u003eThe principal components (PC) transform decorrelates image data by diagonalizing the input band correlation matrix.\u003c/p\u003e\n"],["\u003cp\u003eEarth Engine uses a covariance reducer and the \u003ccode\u003eeigen()\u003c/code\u003e command to perform the PC transform.\u003c/p\u003e\n"],["\u003cp\u003eThe provided function takes a mean-centered image, scale, and region to compute and return principal components.\u003c/p\u003e\n"],["\u003cp\u003eThe input image is converted to a 1D array, and covariance is calculated using \u003ccode\u003eee.Reducer.centeredCovariance()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eEigenvalues and eigenvectors are extracted, and the image is transformed by matrix multiplication with eigenvectors.\u003c/p\u003e\n"]]],["The provided content outlines how to perform a principal component (PC) transform in Earth Engine. This involves using a covariance reducer on an array image and applying the `eigen()` command to the resulting covariance array. Key actions include: converting input imagery to a 1D array, computing band covariance, performing eigen analysis to get eigenvalues and eigenvectors, transforming via matrix multiplication, and normalizing by standard deviations derived from the eigenvalues to obtain the PCs.\n"],null,["# Eigen Analysis\n\nThe [principal\ncomponents (PC) transform](http://en.wikipedia.org/wiki/Principal_component_analysis) (also known as the Karhunen-Loeve transform) is a\nspectral rotation that takes spectrally correlated image data and outputs uncorrelated\ndata. The PC transform accomplishes this by diagonalizing the input band correlation\nmatrix through Eigen-analysis. To do this in Earth Engine, use a covariance reducer on an\narray image and the `eigen()` command on the resultant covariance array.\nConsider the following function for that purpose (an example of it in application is\navailable as a\n[Code Editor script](https://code.earthengine.google.com/30c0e509da3a644fc4fea031b7649f87)\nand a [Colab notebook](https://github.com/google/earthengine-community/blob/master/guides/linked/Earth_Engine_PCA.ipynb)).\n\n### Code Editor (JavaScript)\n\n```javascript\nvar getPrincipalComponents = function(centered, scale, region) {\n // Collapse the bands of the image into a 1D array per pixel.\n var arrays = centered.toArray();\n\n // Compute the covariance of the bands within the region.\n var covar = arrays.reduceRegion({\n reducer: ee.Reducer.centeredCovariance(),\n geometry: region,\n scale: scale,\n maxPixels: 1e9\n });\n\n // Get the 'array' covariance result and cast to an array.\n // This represents the band-to-band covariance within the region.\n var covarArray = ee.Array(covar.get('array'));\n\n // Perform an eigen analysis and slice apart the values and vectors.\n var eigens = covarArray.eigen();\n\n // This is a P-length vector of Eigenvalues.\n var eigenValues = eigens.slice(1, 0, 1);\n // This is a PxP matrix with eigenvectors in rows.\n var eigenVectors = eigens.slice(1, 1);\n\n // Convert the array image to 2D arrays for matrix computations.\n var arrayImage = arrays.toArray(1);\n\n // Left multiply the image array by the matrix of eigenvectors.\n var principalComponents = ee.Image(eigenVectors).matrixMultiply(arrayImage);\n\n // Turn the square roots of the Eigenvalues into a P-band image.\n var sdImage = ee.Image(eigenValues.sqrt())\n .arrayProject([0]).arrayFlatten([getNewBandNames('sd')]);\n\n // Turn the PCs into a P-band image, normalized by SD.\n return principalComponents\n // Throw out an an unneeded dimension, [[]] -\u003e [].\n .arrayProject([0])\n // Make the one band array image a multi-band image, [] -\u003e image.\n .arrayFlatten([getNewBandNames('pc')])\n // Normalize the PCs by their SDs.\n .divide(sdImage);\n};\n```\nPython setup\n\nSee the [Python Environment](/earth-engine/guides/python_install) page for information on the Python API and using\n`geemap` for interactive development. \n\n```python\nimport ee\nimport geemap.core as geemap\n```\n\n### Colab (Python)\n\n```python\ndef get_principal_components(centered, scale, region):\n # Collapse bands into 1D array\n arrays = centered.toArray()\n\n # Compute the covariance of the bands within the region.\n covar = arrays.reduceRegion(\n reducer=ee.Reducer.centeredCovariance(),\n geometry=region,\n scale=scale,\n maxPixels=1e9,\n )\n\n # Get the 'array' covariance result and cast to an array.\n # This represents the band-to-band covariance within the region.\n covar_array = ee.Array(covar.get('array'))\n\n # Perform an eigen analysis and slice apart the values and vectors.\n eigens = covar_array.eigen()\n\n # This is a P-length vector of Eigenvalues.\n eigen_values = eigens.slice(1, 0, 1)\n # This is a PxP matrix with eigenvectors in rows.\n eigen_vectors = eigens.slice(1, 1)\n\n # Convert the array image to 2D arrays for matrix computations.\n array_image = arrays.toArray(1)\n\n # Left multiply the image array by the matrix of eigenvectors.\n principal_components = ee.Image(eigen_vectors).matrixMultiply(array_image)\n\n # Turn the square roots of the Eigenvalues into a P-band image.\n sd_image = (\n ee.Image(eigen_values.sqrt())\n .arrayProject([0])\n .arrayFlatten([get_new_band_names('sd')])\n )\n\n # Turn the PCs into a P-band image, normalized by SD.\n return (\n # Throw out an an unneeded dimension, [[]] -\u003e [].\n principal_components.arrayProject([0])\n # Make the one band array image a multi-band image, [] -\u003e image.\n .arrayFlatten([get_new_band_names('pc')])\n # Normalize the PCs by their SDs.\n .divide(sd_image)\n )\n```\n\nThe input to the function is a mean zero image, a scale and a region over which to\nperform the analysis. Note that the input imagery first needs to be converted to a 1-D\narray image and then reduced using `ee.Reducer.centeredCovariance()`. The\narray returned by this reduction is the symmetric variance-covariance matrix of the input.\nUse the `eigen()` command to get the eigenvalues and eigenvectors of the\ncovariance matrix. The matrix returned by `eigen()` contains the eigenvalues\nin the 0-th position of the 1-axis. As shown in the earlier function, use `slice()`\nto separate the eigenvalues and the eigenvectors. Each element along the 0-axis of the\neigenVectors matrix is an eigenvector. As in the\n[tasseled cap (TC) example](/earth-engine/guides/arrays_array_images), perform the\ntransformation by matrix multiplying the `arrayImage` by the eigenvectors.\nIn this example, each eigenvector multiplication results in a PC."]]