Spark之takeSample执行算子

takeSample

返回一个Array[T]; 
该方法仅在预期结果数组很小的情况下使用,因为所有数据都被加载到driver的内存中。

源码

def takeSample(

 withReplacement: Boolean,

 num: Int,

 seed: Long = Utils.random.nextLong): Array[T] = withScope {...}

 

withReplacement:元素可以多次抽样(在抽样时替换)

num:返回的样本的大小

seed:随机数生成器的种子(因为不好把控,建议不写默认)

 

(1)当不可以多次抽样:withReplacement=false;样本个数num大于父本个数时,只能返回父本个数

scala> val a = sc.parallelize(Array(2,5,6,8,9))
a: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[79] at parallelize at <console>:24

scala> a.takeSample(false,8)
res41: Array[Int] = Array(8, 5, 6, 2, 9)

 

(2)当不可以多次抽样:withReplacement=false;样本个数num小于父本个数时,返回样本个数

scala> a.takeSample(false,3)
res42: Array[Int] = Array(6, 8, 9)

(3)当可以多次抽样:withReplacement=true;样本个数num大于父本个数时,返回样本个数

scala> a.takeSample(true,10)
res43: Array[Int] = Array(9, 9, 9, 5, 2, 2, 8, 2, 8, 6)

(4)当可以多次抽样:withReplacement=true;样本个数num小于父本个数时,返回样本个数

scala> a.takeSample(true,3)
res44: Array[Int] = Array(5, 8, 2)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值