Hadoop简单随机采样

博客介绍基于Hadoop的两种采样模式,即随机百分比采样和随机抽取固定容量样本。随机百分比采样是对每条数据用随机数生成器产生[0,1]随机数,与阈值比较决定取舍,再用恒等reducer合并;固定容量样本则用堆维护,最后通过单个reducer重新抽取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(注:内容来自《Hadoop数据分析》)

基于Hadoop的两种采样模式:百分比采样和N样本采样。

1.随机百分比采样:

从样本中随机抽取一个比例的样本,一种方法是对每条数据简单地使用随机数生成器来产生[0,1]上均匀分布的随机数,并将其与期望的阈值大小进行比较,小于阈值的留下,大于阈值的跳过。

mapper.py

import random
# class Mapper可参考上一篇博客
class PercentSampleMapper(Mapper):

    def __init__(self,*args,**kwargs):
        self.percentage = kwargs.pop("percentage")
        super(PercentSampleMapper,self).__init__(*args,**kwargs)

    def map(self):
        for _, val in self:
            if random.random() < self.percentage:
                self.emit(None,val)
    
    if __name__=='__main__':
        mapper = PercentSampleMapper(sys.stdin,percentage=0.2)
        mapper.map 

接着,使用一个恒等reducer合并数据

2.随机抽取固定容量样本:

仍然对每条数据产生一个[0,1]上的均匀分布,然后通过堆维护一个容量为n的样本。

import random,heapd

class SampleMapper(Mapper):

    def __init__(self,n,*args,**kwargs):
        self.n = n
        super(SampleMapper,self).__init__(*args,**kwargs)
    
    def map(self):
        heap = [0]*self.n

        for value in self:
            #heappushpop 如存在random.random()>heap中的某值,则将heap中最小值弹出,并将random.random()随机插入到heap
            heapd.heappushpop(heap,(random.random(),value))
            for item in heap:
                self.emit(None,item)

class SampleReducer(Mapper):
    
    def __init__(self,n,*args,**kwargs):
        self.n = n
        supper(SampleReducer,self).__init__(*args,**kwargs)

    def reduce(self):
        heap = [0]*self.n

        for _,value in self:
            for value in values:
                heapd.heappushpop(heap,make_tuple(value))

        for item in heap:
            self.emit(None,item[1])

该方法获得的样本数是mapper的数量的n倍,通过单个reducer对数据重新抽取。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值