【问题标题】:mapreduce conditional probabilitymapreduce 条件概率
【发布时间】:2014-04-29 14:56:23
【问题描述】:

如何在我的 reducer 中使用映射器进行概率聚合;

我正在尝试在 Hadoop 上为以下任务实现“条纹”方法和“配对”方法,但我想知道如何在多个映射器之间进行通信以及如何在内部进行面向概率的聚合我的减速机。

  • 每对物品的共现,Count (A, B)=# of transactions 同时包含A和B,条件概率Prob(B|A)=Count(A,B)/Count(A )。
  • 每个三元组项目的共现次数,Count (A,B,C) =# of transactions 包含 A 和 B,条件概率 Prob(A|B,C)=Count(A,B, C)/计数(B,C)
  • 每行记录一次交易(一起购买的一组物品): 输入数据集是具有以下格式的事务数据:

    25 52 164 240 274 328 368 448 538 561 630 687 730 775 825 834 39 120 124 205 401 581 704 814 825 834 35 249 674 712 733 759 854 950 39 422 449 704 825 857 895 937 954 964 15 229 262 283 294 352 381 708 738 766 853 883 966 978 26 104 143 320 569 620 798 7 185 214 350 529 658 682 782 809 849 883 947 970 979 227 390 71 192 208 272 279 280 300 333 496 529 530 597 618 674 675 720 855 914 932 ==================================================== ======================================**

【问题讨论】:

    标签: hadoop probability reducers mappers


    【解决方案1】:

    对您的问题的简短回答是,您不直接在映射器之间进行通信......这违背了 map-reduce 计算模式。相反,您需要构建您的算法,以便您的 map 阶段输出的键值可以被您的 reducer 阶段以一种智能的方式使用和聚合。

    从您在问题中的背景信息可以清楚地看出,计算您感兴趣的条件概率实际上只是一种计算练习。这里通常的模式是在一个 map reduce pass 中完成所有计数,然后获取这些输出并在之后划分适当的数量(尝试将它们放入 map-reduce pass 会增加不必要的复杂性)

    您实际上只需要一个数据结构来跟踪您要计数的事物。如果速度是必须的,您可以使用一组具有隐式索引的数组来做到这一点,但是根据单个哈希图进行说明很简单。因为我们不感兴趣

    用于hadoop流的python映射器代码

    import sys
    output={}
    
    
    for line in sys.stdin:
       temp=line.strip().split('\t')
       # we should sort the input so that all occurrences of items x and y appear with x before y
       temp.sort()
       # count the occurrences of all the single items
       for item in temp:
          if item in output:
             output[item]+=1
          else:
             output[item]=1
    
    
       #count the occurrences of each pair of items
       for i in range(len(temp)):
          for j in range(i+1,len(temp)):
             key=temp[i]+'-'+temp[j]
             if key in output:
                output[key]+=1
             else:
                output[key]=1
       #you can triple nest a loop if you want to count all of the occurrences of each 3 item pair, but be warned the number of combinations starts to get large quickly
       #for 100 items as in your example there would be 160K combinations
    
    
    #this point of the program will be reached after all of the data has been streamed in through stdin
    #output the keys and values of our output dictionary with a tab separating them
    for data in output.items():
       print data[0]+'\t'+data[1]
    
    #end mapper code
    

    reducer 的代码现在与所有如此多产的单词计数示例相同。可以在here 找到一个带有 map-reduce 流的 python 代码示例。 map-reduce 程序的输出将是一行,其中的键描述已计数的内容以及每个项目的出现次数以及所有对的键和出现次数,然后您可以从那里编写程序计算你感兴趣的条件概率。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-12
      • 2016-08-11
      • 1970-01-01
      • 2017-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多