ee.FeatureCollection.randomColumn

將一列決定性的偽隨機數新增至集合。輸出內容為雙精度浮點數。使用「均勻」分佈 (預設) 時,輸出值會落在 [0, 1] 的範圍內。使用「正常」分佈函式時,輸出值的 μ 和 σ 分別為 0 和 1,但沒有明確的限制。

用量傳回
FeatureCollection.randomColumn(columnName, seed, distribution, rowKeys)FeatureCollection
引數類型詳細資料
this:collectionFeatureCollection要新增隨機資料欄的輸入集合。
columnName字串,預設值為「random」要新增的資料欄名稱。
seedLong,預設值:0產生隨機號碼時使用的種子。
distribution字串,預設值為「uniform」要產生的隨機數分布類型,可選值為「uniform」或「normal」。
rowKeys清單,選用屬性清單,應可用於唯一且可重複識別集合元素,用於產生隨機號碼。預設值為 [system:index]。

範例

程式碼編輯器 (JavaScript)

// FeatureCollection of power plants in Belgium.
var fc = ee.FeatureCollection('WRI/GPPD/power_plants')
            .filter('country_lg == "Belgium"');
print('N features in collection', fc.size());

// Add a uniform distribution random value column to the FeatureCollection.
fc = fc.randomColumn();

// Randomly split the collection into two sets, 30% and 70% of the total.
var randomSample30 = fc.filter('random < 0.3');
print('N features in 30% sample', randomSample30.size());

var randomSample70 = fc.filter('random >= 0.3');
print('N features in 70% sample', randomSample70.size());

Python 設定

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

import ee
import geemap.core as geemap

Colab (Python)

# FeatureCollection of power plants in Belgium.
fc = ee.FeatureCollection('WRI/GPPD/power_plants').filter(
    'country_lg == "Belgium"')
print('N features in collection:', fc.size().getInfo())

# Add a uniform distribution random value column to the FeatureCollection.
fc = fc.randomColumn()

# Randomly split the collection into two sets, 30% and 70% of the total.
random_sample_30 = fc.filter('random < 0.3')
print('N features in 30% sample:', random_sample_30.size().getInfo())

random_sample_70 = fc.filter('random >= 0.3')
print('N features in 70% sample:', random_sample_70.size().getInfo())