先将总体的单位按某种特征分为若干次级总体(层),然后再从每一层内进行单纯随机抽样,组成一个样本的统计学计算方法叫做分层抽样。
与存在于spark.mllib
中的其它统计函数不同,分层采样方法sampleByKey
和sampleByKeyExact
可以在key-value
对的RDD
上执行。在分层采样中,可以认为key
是一个标签,
value
是特定的属性。例如,key
可以是男人或者女人或者文档id
,它相应的value
可能是一组年龄或者是文档中的词。sampleByKey
方法通过掷硬币的方式决定是否采样一个观察数据,
因此它需要我们忽视(pass over
)数据本身而只提供期望的数据大小。sampleByKeyExact
比每层使用sampleByKey
随机抽样需要更多的有意义的资源,但是它能使样本大小的准确性达到了99.99%
。
sampleByKeyExact()允许用户准确抽取f_k * n_k
个样本,
这里f_k
表示期望获取键为k
的样本的比例,n_k
表示键为k
的键值对的数量。下面是一个使用的例子:
import org.apache.spark.SparkContext
import org.apache.spark.SparkContext._
import org.apache.spark.rdd.PairRDDFunctions
val sc: SparkContext = ...
val data = ... // an RDD[(K, V)] of any key value pairs
val fractions: Map[K, Double] = ... // specify the exact fraction desired from each key
// Get an exact sample from each stratum
val approxSample = data.sampleByKey(withReplacement = false, fractions)
val exactSample = data.sampleByKeyExact(withReplacement = false, fractions)
当withReplacement
为true
时,采用PoissonSampler
取样器,当withReplacement
为false
使,采用BernoulliSampler
取样器。
def sampleByKey(withReplacement: Boolean,
fractions: Map[K, Double],
seed: Long = Utils.random.nextLong): RDD[(K, V)] = self.withScope {
val samplingFunc = if (withReplacement) {
StratifiedSamplingUtils.getPoissonSamplingFunction(self, fractions, false, seed)
} else {
StratifiedSamplingUtils.getBernoulliSamplingFunction(self, fractions, false, seed)
}
self.mapPartitionsWithIndex(samplingFunc, preservesPartitioning = true)
}
def sampleByKeyExact(
withReplacement: Boolean,
fractions: Map[K, Double],
seed: Long = Utils.random.nextLong): RDD[(K, V)] = self.withScope {
val samplingFunc = if (withReplacement) {
StratifiedSamplingUtils.getPoissonSamplingFunction(self, fractions, true, seed)
} else {
StratifiedSamplingUtils.getBernoulliSamplingFunction(self, fractions, true, seed)
}
self.mapPartitionsWithIndex(samplingFunc, preservesPartitioning = true)
}