sampleBy (DataFrame)

根据每个层中给出的分数返回分层样本,而不用替换。

Syntax

sampleBy(col: "ColumnOrName", fractions: Dict[Any, float], seed: Optional[int] = None)

参数

参数 类型 说明
col 列或 str 定义层的列。
fractions dict 每个层的采样分数。 如果未指定层,我们将其分数视为零。
seed int,可选 随机种子。

退货

表示分层示例的新 DataFrame。

示例

from pyspark.sql import functions as sf
dataset = spark.range(0, 100, 1, 5).select((sf.col("id") % 3).alias("key"))
sampled = dataset.sampleBy("key", fractions={0: 0.1, 1: 0.2}, seed=0)
sampled.groupBy("key").count().orderBy("key").show()
# +---+-----+
# |key|count|
# +---+-----+
# |  0|    4|
# |  1|    9|
# +---+-----+

dataset.sampleBy(sf.col("key"), fractions={2: 1.0}, seed=0).count()
# 33