Reducer 概览

在 Earth Engine 中,Reducer 是对时间、空间、波段、数组和其他数据结构中的数据进行汇总的方法。ee.Reducer 类指定数据的汇总方式。此类中的缩减器可以指定用于汇总的简单统计信息(例如最小值、最大值、平均值、中位数、标准差等),也可以指定输入数据的更复杂汇总(例如直方图、线性回归、列表)。缩减可能发生在时间 (imageCollection.reduce())、空间(image.reduceRegion()、image.reduceNeighborhood())、波段 (image.reduce()) 或 FeatureCollection 的属性空间(featureCollection.reduceColumns() 或以 aggregate_ 开头的 FeatureCollection 方法)上。

Reducer 具有输入和输出

Reducer 接受输入数据集并生成单个输出。如果将单输入 reducer 应用于多波段图像,Earth Engine 会自动复制该 reducer,并将其分别应用于每个波段。因此,输出图像的波段数与输入图像相同;输出中的每个波段都是输入数据中相应波段的像素缩减。某些化简器接受输入数据集的元组。这些 reducer 不会自动为每个波段复制。例如,ee.Reducer.LinearRegression() 会按特定顺序获取多个预测变量数据集(表示回归中的自变量)(请参阅回归缩减器)。

某些 reducer 会产生多个输出,例如 ee.Reducer.minMax()、ee.Reducer.histogram() 或 ee.Reducer.toList()。例如:

代码编辑器 (JavaScript)

// Load and filter the Sentinel-2 image collection.
var collection = ee.ImageCollection('COPERNICUS/S2_HARMONIZED')
    .filterDate('2016-01-01', '2016-12-31')
    .filterBounds(ee.Geometry.Point([-81.31, 29.90]));

// Reduce the collection.
var extrema = collection.reduce(ee.Reducer.minMax());

Python 设置

如需了解 Python API 和如何使用 geemap 进行交互式开发,请访问 Python 环境页面。

import ee
import geemap.core as geemap

Colab (Python)

# Load and filter the Sentinel-2 image collection.
collection = (
    ee.ImageCollection('COPERNICUS/S2_HARMONIZED')
    .filterDate('2016-01-01', '2016-12-31')
    .filterBounds(ee.Geometry.Point([-81.31, 29.90]))
)

# Reduce the collection.
extrema = collection.reduce(ee.Reducer.minMax())

这将产生一个输出,其波段数量是输入波段数量的两倍,其中输出中的波段名称会在波段名称后附加“_min”或“_max”。

输出类型应与计算结果相匹配。例如,应用于 ImageCollection 的 reducer 具有 Image 输出。由于输出会被解读为像素值,因此您必须使用具有数值输出的缩减器来缩减 ImageCollection(toList() 或 histogram() 等缩减器不起作用)。

Reducer 使用加权输入

默认情况下,像素值的缩减会按其掩膜进行加权,但此行为可以更改(请参阅加权部分)。掩膜等于 0 的像素不会用于缩减。

合并缩减器

如果您打算对相同的输入应用多个化简器,最好combine()这些化简器,以提高效率。具体而言,在 sharedInputs 设置为 true 的 reducer 上调用 combine() 将导致仅对数据进行一次传递。例如,如需计算图片中像素的平均值和标准差,您可以使用如下代码:

代码编辑器 (JavaScript)

// Load a Landsat 8 image.
var image = ee.Image('LANDSAT/LC08/C02/T1/LC08_044034_20140318');

// Combine the mean and standard deviation reducers.
var reducers = ee.Reducer.mean().combine({
  reducer2: ee.Reducer.stdDev(),
  sharedInputs: true
});

// Use the combined reducer to get the mean and SD of the image.
var stats = image.reduceRegion({
  reducer: reducers,
  bestEffort: true,
});

// Display the dictionary of band means and SDs.
print(stats);

Python 设置

如需了解 Python API 和如何使用 geemap 进行交互式开发,请访问 Python 环境页面。

import ee
import geemap.core as geemap

Colab (Python)

# Load a Landsat 8 image.
image = ee.Image('LANDSAT/LC08/C02/T1/LC08_044034_20140318')

# Combine the mean and standard deviation reducers.
reducers = ee.Reducer.mean().combine(
    reducer2=ee.Reducer.stdDev(), sharedInputs=True
)

# Use the combined reducer to get the mean and SD of the image.
stats = image.reduceRegion(reducer=reducers, bestEffort=True)

# Display the dictionary of band means and SDs.
display(stats)

在输出中,请注意,reducer 的名称已附加到输入名称中,以区分 reducer 输出。此行为也适用于图片输出,其输出波段名称会附加化简器的名称。

如果您要将使用非加权输入的化简器和使用加权输入的化简器组合在一起,则所有加权输入都必须位于所有非加权输入之前。