ee.FeatureCollection.randomColumn

向集合添加一列确定性伪随机数。输出是双精度浮点数。使用“均匀”分布(默认)时,输出值在 [0, 1] 范围内。使用“正态”分布时,输出的 μ=0,σ=1,但没有明确的限制。

用法返回
FeatureCollection.randomColumn(columnName, seed, distribution, rowKeys)FeatureCollection
参数类型详细信息
this:collectionFeatureCollection要向其中添加随机列的输入集合。
columnName字符串,默认值:“random”要添加的列的名称。
seed长整数,默认值:0生成随机数时使用的种子。
distribution字符串,默认值:“uniform”要生成的随机数的分布类型;可以是“均匀”或“正态”之一。
rowKeys列表,可选应唯一且可重复地标识集合元素的属性列表,用于生成随机数字。默认值为 [system:index]。

示例

Code Editor (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 API 以及如何使用 geemap 进行交互式开发,请参阅 Python 环境页面。

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())