Reducer 是在 Earth Engine 中匯總時間、空間、波段、陣列和其他資料結構資料的方式。ee.Reducer 類別會指定資料匯總方式。這個類別中的縮減器可以指定用於匯總的簡單統計資料 (例如最小值、最大值、平均值、中位數、標準差等),或輸入資料的更複雜摘要 (例如直方圖、線性迴歸、清單)。縮減作業可能會在時間 (imageCollection.reduce())、空間 (image.reduceRegion()、image.reduceNeighborhood())、波段 (image.reduce()) 或 FeatureCollection 的屬性空間 (featureCollection.reduceColumns() 或以 aggregate_ 開頭的 FeatureCollection 方法) 進行。
Reducer 有輸入和輸出內容
Reducer 會接收輸入資料集,並產生單一輸出內容。如果將單一輸入縮減器套用至多波段圖片,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());
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 的縮減器會產生 Image 輸出。由於輸出內容會解讀為像素值,因此您必須使用具有數值輸出的縮減器來縮減 ImageCollection (toList() 或 histogram() 等縮減器無法運作)。
Reducer 使用加權輸入內容
根據預設,像素值的縮減會依遮罩加權,但您可以變更這項行為 (請參閱加權一節)。遮罩等於 0 的像素不會用於縮減。
合併化簡器
如果想對相同輸入內容套用多個歸納器,建議combine()歸納器,以提高效率。具體來說,在 sharedInputs 設為 true 的縮減器上呼叫 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);
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 輸出內容。這項行為也適用於圖片輸出,輸出波段名稱會附加縮減器名稱。
如果同時使用未加權輸入內容和加權輸入內容的縮減器,所有加權輸入內容都必須位於所有未加權輸入內容之前。