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

Hadoop mapreduce条件概率 如何使用映射器在我的reducer中进行概率聚合;,hadoop,probability,reducers,mappers,Hadoop,Probability,Reducers,Mappers,我尝试在Hadoop上实现“条带”方法和“成对”方法,用于以下任务,但我想知道如何在多个映射器之间进行通信,以及如何在我的reducer中进行面向概率的聚合 每对项目的共现,Count(A,B)=事务的A和B都包含,条件概率Prob(B | A)=Count(A,B)/Count(A) 交易的计数(A,B,C)=#项的每三个项的共现同时包含A和B,条件概率Prob(A | B,C)=计数(A,B,C)/计数(B,C) 每行记录一笔交易(一起购买的一组项目): 输入数据集是具有以下格式的事务数据

我尝试在Hadoop上实现“条带”方法和“成对”方法,用于以下任务,但我想知道如何在多个映射器之间进行通信,以及如何在我的reducer中进行面向概率的聚合

  • 每对项目的共现,Count(A,B)=事务的A和B都包含,条件概率Prob(B | A)=Count(A,B)/Count(A)
  • 交易的计数(A,B,C)=#项的每三个项的共现同时包含A和B,条件概率Prob(A | B,C)=计数(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 =======================================================================================**


    • 对你的问题的简短回答是,你不会在地图绘制者之间直接沟通。。。这与map-reduce计算模式背道而驰。相反,您需要构造算法,以便map阶段输出的键值可以由reducer阶段以智能方式使用和聚合

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

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

      python中用于hadoop流的映射程序代码

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