Spark算子[15]:sample、takeSample 源码实例详解
来源:互联网 发布:杭州 大学生家教 知乎 编辑:程序博客网 时间:2024/06/05 09:50
sample
返回一个RDD[T]
源码:
/** * 返回此RDD的抽样子集。 * @note 这并不能保证提供的只是给定[[RDD]]的分数。 */def sample( withReplacement: Boolean, fraction: Double, seed: Long = Utils.random.nextLong): RDD[T] = { require(fraction >= 0, s"Fraction must be nonnegative, but got ${fraction}") withScope { require(fraction >= 0.0, "Negative fraction value: " + fraction) if (withReplacement) { new PartitionwiseSampledRDD[T, T](this, new PoissonSampler[T](fraction), true, seed) } else { new PartitionwiseSampledRDD[T, T](this, new BernoulliSampler[T](fraction), true, seed) } }}
参数:
1、withReplacement:元素可以多次抽样(在抽样时替换)
2、fraction:期望样本的大小作为RDD大小的一部分,
当withReplacement=false时:选择每个元素的概率;分数一定是[0,1] ;
当withReplacement=true时:选择每个元素的期望次数; 分数必须大于等于0。3、seed:随机数生成器的种子
案例:
建议第三个参数seed可以默认,不好把控。
(1)元素不可以多次抽样:withReplacement=false,每个元素被抽取到的概率为0.5:fraction=0.5
JavaRDD<Integer> rdd = sc.parallelize(Arrays.asList(6,2,8,4));JavaRDD<Integer> res = rdd.sample(false, 0.5);res.foreach(x -> System.out.print(x +" "));//结果:2 8 4
(2)元素可以多次抽样:withReplacement=true,每个元素被抽取到的期望次数为2:fraction=2
JavaRDD<Integer> rdd = sc.parallelize(Arrays.asList(6,2,8,4));JavaRDD<Integer> res = rdd.sample(true, 2);res.foreach(x -> System.out.print(x +" "));//结果:6 2 8 8 8 8 8 4
takeSample
返回一个Array[T];
该方法仅在预期结果数组很小的情况下使用,因为所有数据都被加载到driver的内存中。
源码:
def takeSample( withReplacement: Boolean, num: Int, seed: Long = Utils.random.nextLong): Array[T] = withScope {...}
参数:
1、withReplacement:元素可以多次抽样(在抽样时替换)
2、num:返回的样本的大小
3、seed:随机数生成器的种子
案例:
建议第三个参数seed可以默认,不好把控。
(1)当不可以多次抽样:withReplacement=false;样本个数num大于父本个数时,只能返回父本个数
JavaRDD<Integer> rdd = sc.parallelize(Arrays.asList(6, 2, 8, 4));List<Integer> res = rdd.takeSample(false, 8);System.out.println(res);//结果:[6, 8, 2, 4]
(2)当不可以多次抽样:withReplacement=false;样本个数num小于父本个数时,返回样本个数
JavaRDD<Integer> rdd = sc.parallelize(Arrays.asList(6, 2, 8, 4));List<Integer> res = rdd.takeSample(false, 3);System.out.println(res);//结果:[8, 4, 2]
(3)当可以多次抽样:withReplacement=true;样本个数num大于父本个数时,返回样本个数
JavaRDD<Integer> rdd = sc.parallelize(Arrays.asList(6, 2, 8, 4));List<Integer> res = rdd.takeSample(true, 8);System.out.println(res);//结果:[4, 6, 8, 2, 6, 2, 4, 2]
(4)当不可以多次抽样:withReplacement=true;样本个数num小于父本个数时,返回样本个数
JavaRDD<Integer> rdd = sc.parallelize(Arrays.asList(6, 2, 8, 4));List<Integer> res = rdd.takeSample(true, 3);System.out.println(res);//结果:[8, 8, 2]
- Spark算子[15]:sample、takeSample 源码实例详解
- spark--actions算子--takeSample
- Spark RDD中Transformation的mapValues、subtract、sample、takeSample详解
- Spark算子[10]:foldByKey、fold 源码实例详解
- Spark算子[12]:groupByKey、cogroup、join、lookup 源码实例详解
- Spark算子[13]:sortByKey、sortBy、二次排序 源码实例详解
- Spark算子[14]:top、takeOrdered 源码实例详解
- Spark算子[18]:saveAsTextFile、saveAsObjectFile 源码实例详解
- Spark算子[19]:saveAsHadoopFile、saveAsNewAPIHadoopFile 源码实例详解
- spark--transform算子--sample
- Spark算子[20]:saveAsHadoopDataset、saveAsNewAPIHadoopDataset 实例详解
- Spark SQL 算子实例
- Spark算子[16]:subtractByKey、join、rightOuterJoin、leftOuterJoin 实例详解
- Spark算子[17]:zip、zipPartitions、zipWithIndex、zipWithUniqueId 实例详解
- spark 算子combineByKey 详解
- spark 常用算子 详解
- Spark RDD算子源码解读
- spark中算子详解:aggregateByKey
- 自用PV
- 在java中如何键入一个数/字符/字符串等
- 25%的电量录像闪光灯不亮
- 指针基础
- 指针高级应用(一)
- Spark算子[15]:sample、takeSample 源码实例详解
- 指针高级应用(二)
- Hash-开放地址法
- Centos下“无法打开并写入文件”问题
- CPM论文翻译(CVPR2016)
- Java8-LongAccumulator
- 自定义圆形进度条
- Java8-ConcurrentUtils
- 启动tomat报错分析