公告:凡是在
2025 年 4 月 15 日前註冊使用 Earth Engine 的非商業專案,都必須
驗證非商業用途資格,才能繼續存取 Earth Engine。
縮減函式總覽
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
縮減函式是一種方法,可在 Earth Engine 中匯總時間、空間、頻帶、陣列和其他資料結構的資料。ee.Reducer
類別會指定資料的匯總方式。這個類別中的縮減器可指定用於匯總的簡單統計資料 (例如最小值、最大值、平均值、中位數、標準差等),或輸入資料的較複雜摘要 (例如直方圖、線性迴歸、清單)。縮減可能發生在時間 (imageCollection.reduce()
)、空間 (image.reduceRegion()
、image.reduceNeighborhood()
)、頻帶 (image.reduce()
) 或 FeatureCollection
的屬性空間 (featureCollection.reduceColumns()
或以 aggregate_
開頭的 FeatureCollection
方法)。
算術運算器會擷取輸入資料集,並產生單一輸出內容。當單一輸入縮減器套用至多頻帶影像時,Earth Engine 會自動複製縮減器,並分別套用至各個頻帶。因此,輸出圖片的頻帶數量與輸入圖片相同;輸出圖片中的每個頻帶,都是輸入資料中對應頻帶的像素減少量。部分縮減器會使用輸入資料集的元組。這些縮減器不會自動複製到每個頻帶。舉例來說,ee.Reducer.LinearRegression()
會以特定順序 (請參閱「迴歸縮減器」) 取用多個預測資料集 (代表迴歸中的獨立變數)。
某些縮減器會產生多個輸出,例如 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 環境」頁面,瞭解 Python API 和如何使用 geemap
進行互動式開發。
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()
這類縮減器無法運作)。
根據預設,系統會根據遮罩為像素值減數加權,但這項行為可以變更 (請參閱「加權」一節)。系統不會在縮減作業中使用遮罩值等於 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 環境」頁面,瞭解 Python API 和如何使用 geemap
進行互動式開發。
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 輸出內容。這項行為也適用於圖片輸出,輸出頻帶名稱會加上縮減器名稱。
如果您要結合使用無權重的輸入內容和使用權重輸入內容的 reducer,所有權重輸入內容必須置於所有無權重輸入內容之前。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-25 (世界標準時間)。
[null,null,["上次更新時間:2025-07-25 (世界標準時間)。"],[[["\u003cp\u003eReducers in Earth Engine aggregate data over time, space, bands, arrays, and other data structures, using methods like \u003ccode\u003ereduceRegion()\u003c/code\u003e and \u003ccode\u003ereduce()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eReducers can calculate simple statistics (e.g., mean, minimum) or more complex summaries (e.g., histograms, linear regressions) on images, image collections, and feature collections.\u003c/p\u003e\n"],["\u003cp\u003eWhen applied to multi-band images, single input reducers are automatically applied to each band, producing an output with the same number of bands.\u003c/p\u003e\n"],["\u003cp\u003eReducers can be combined using \u003ccode\u003ecombine()\u003c/code\u003e for efficient processing, allowing multiple calculations in a single pass over the data.\u003c/p\u003e\n"],["\u003cp\u003eBy default, pixel values in reductions are weighted by their mask, meaning pixels with a mask of 0 are excluded from the calculation.\u003c/p\u003e\n"]]],[],null,["# Reducer Overview\n\nReducers are the way to aggregate data over time, space, bands, arrays and other data\nstructures in Earth Engine. The `ee.Reducer` class specifies how data is\naggregated. The reducers in this class can specify a simple statistic to use for the\naggregation (e.g. minimum, maximum, mean, median, standard deviation, etc.), or a more complex\nsummary of the input data (e.g. histogram, linear regression, list). Reductions may occur over\ntime (`imageCollection.reduce()`), space (`image.reduceRegion()`,\n`image.reduceNeighborhood()`), bands (`image.reduce()`), or the\nattribute space of a `FeatureCollection` (`featureCollection.reduceColumns()`\nor `FeatureCollection` methods that start with `aggregate_`).\n\nReducers have inputs and outputs\n--------------------------------\n\n\nReducers take an input dataset and produce a single output. When a single input reducer is\napplied to a multi-band image, Earth Engine automatically replicates the reducer and applies\nit separately to each band. As a result, the output image has the same number of bands as the\ninput image; each band in the output is the reduction of pixels from the corresponding band in\nthe input data. Some reducers take tuples of input datasets. These reducers will not be\nautomatically replicated for each band. For example,\n`ee.Reducer.LinearRegression()` takes multiple predictor datasets (representing\nindependent variables in the regression) in a particular order (see\n[Regression reducers](/earth-engine/guides/reducers_regression)).\n\n\nSome reducers produce multiple outputs, for example `ee.Reducer.minMax()`,\n`ee.Reducer.histogram()` or `ee.Reducer.toList()`. For example:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Load and filter the Sentinel-2 image collection.\nvar collection = ee.ImageCollection('COPERNICUS/S2_HARMONIZED')\n .filterDate('2016-01-01', '2016-12-31')\n .filterBounds(ee.Geometry.Point([-81.31, 29.90]));\n\n// Reduce the collection.\nvar extrema = collection.reduce(ee.Reducer.minMax());\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\n# Load and filter the Sentinel-2 image collection.\ncollection = (\n ee.ImageCollection('COPERNICUS/S2_HARMONIZED')\n .filterDate('2016-01-01', '2016-12-31')\n .filterBounds(ee.Geometry.Point([-81.31, 29.90]))\n)\n\n# Reduce the collection.\nextrema = collection.reduce(ee.Reducer.minMax())\n```\n\n\nThis will produce an output with twice the number of bands of the inputs, where band names in\nthe output have '_min' or '_max' appended to the band name.\n\n\nThe output type should match the computation. For example, a reducer applied to an\n`ImageCollection` has an `Image` output. Because the output is\ninterpreted as a pixel value, you must use reducers with a numeric output to reduce an\n`ImageCollection` (reducers like `toList()` or\n`histogram()` won't work).\n\nReducers use weighted inputs\n----------------------------\n\n\nBy default, reductions over pixel values are weighted by their mask, though this behavior can\nbe changed (see the [Weighting section](/earth-engine/guides/reducers_weighting)). Pixels with mask\nequal to 0 will not be used in the reduction.\n\nCombining reducers\n------------------\n\n\nIf your intent is to apply multiple reducers to the same inputs, it's good practice to\n`combine()` the reducers for efficiency. Specifically, calling\n`combine()` on a reducer with `sharedInputs` set to\n`true` will result in only a single pass over the data. For example, to compute the\nmean and standard deviation of pixels in an image, you could use something like this:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Load a Landsat 8 image.\nvar image = ee.Image('LANDSAT/LC08/C02/T1/LC08_044034_20140318');\n\n// Combine the mean and standard deviation reducers.\nvar reducers = ee.Reducer.mean().combine({\n reducer2: ee.Reducer.stdDev(),\n sharedInputs: true\n});\n\n// Use the combined reducer to get the mean and SD of the image.\nvar stats = image.reduceRegion({\n reducer: reducers,\n bestEffort: true,\n});\n\n// Display the dictionary of band means and SDs.\nprint(stats);\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\n# Load a Landsat 8 image.\nimage = ee.Image('LANDSAT/LC08/C02/T1/LC08_044034_20140318')\n\n# Combine the mean and standard deviation reducers.\nreducers = ee.Reducer.mean().combine(\n reducer2=ee.Reducer.stdDev(), sharedInputs=True\n)\n\n# Use the combined reducer to get the mean and SD of the image.\nstats = image.reduceRegion(reducer=reducers, bestEffort=True)\n\n# Display the dictionary of band means and SDs.\ndisplay(stats)\n```\n\n\nIn the output, note that the names of the reducers have been appended to the names of the\ninputs to distinguish the reducer outputs. This behavior also applies to image outputs, which\nwill have the name of the reducer appended to output band names.\n\n\nIf you are combining reducers using unweighted inputs and reducers using weighted inputs, all\nweighted inputs must be before all unweighted inputs."]]