非監督式分類 (分群)

ee.Clusterer 套件會在 Earth Engine 中處理無監督分類 (或叢集)。這些演算法目前是根據 Weka 中同名的演算法建立。如要進一步瞭解每個 Clusterer,請參閱參考文件

叢集器的使用方式與 Earth Engine 中的分類器相同。一般而言,建立叢集的流程如下:

  1. 將具有數值屬性的特徵組合起來,以便找出叢集。
  2. 將聚合器例項化。視需要設定參數。
  3. 使用訓練資料訓練分群器。
  4. 將聚合器套用至圖片或地圖項目集合。
  5. 為叢集加上標籤。

訓練資料是 FeatureCollection,其中包含將輸入至叢集器的屬性。與分類器不同,Clusterer 沒有輸入類別值。與分類器一樣,訓練和套用步驟的資料應具有相同的值數量。當訓練好的分群器套用至圖片或表格時,會為每個像素或特徵指派整數叢集 ID。

以下是建構及使用 ee.Clusterer 的簡單範例:

程式碼編輯器 (JavaScript)

// Define a region in which to generate a segmented map.
var region = ee.Geometry.Rectangle(29.7, 30, 32.5, 31.7);

// Load a Landsat composite for input.
var input = ee.ImageCollection('LANDSAT/COMPOSITES/C02/T1_L2_32DAY')
  .filterDate('2001-05', '2001-06')
  .first()
  .clip(region);

// Display the sample region.
Map.setCenter(31.5, 31.0, 8);
Map.addLayer(ee.Image().paint(region, 0, 2), {}, 'region');

// Make the training dataset.
var training = input.sample({
  region: region,
  scale: 30,
  numPixels: 5000
});

// Instantiate the clusterer and train it.
var clusterer = ee.Clusterer.wekaKMeans(15).train(training);

// Cluster the input using the trained clusterer.
var result = input.cluster(clusterer);

// Display the clusters with random colors.
Map.addLayer(result.randomVisualizer(), {}, 'clusters');

Python 設定

請參閱「 Python 環境」頁面,瞭解 Python API 和如何使用 geemap 進行互動式開發。

import ee
import geemap.core as geemap

Colab (Python)

# Define a region in which to generate a segmented map.
region = ee.Geometry.Rectangle(29.7, 30, 32.5, 31.7)

# Load a Landsat composite for input.
input = (
    ee.ImageCollection('LANDSAT/COMPOSITES/C02/T1_L2_32DAY')
    .filterDate('2001-05', '2001-06')
    .first()
    .clip(region)
)

# Display the sample region.
m = geemap.Map()
m.set_center(31.5, 31.0, 8)
m.add_layer(ee.Image().paint(region, 0, 2), {}, 'region')

# Make the training dataset.
training = input.sample(region=region, scale=30, numPixels=5000)

# Instantiate the clusterer and train it.
clusterer = ee.Clusterer.wekaKMeans(15).train(training)

# Cluster the input using the trained clusterer.
result = input.cluster(clusterer)

# Display the clusters with random colors.
m.add_layer(result.randomVisualizer(), {}, 'clusters')
m

注意事項:

  • 相同的輸入內容應一律產生相同的輸出內容,但重新排序輸入內容可能會改變結果。
  • 只要訓練資料包含 10 個頻帶 * 10 萬個資料點,就可能發生記憶體不足錯誤。
  • 蜘蛛網可能需要很長的時間才能完成,而且可能會產生大量叢集。
  • 輸出叢集及其 ID 取決於演算法和輸入內容。