Mapreduce conditional probability

how to make probability clusters inside my reducer using mappers;

I am trying to implement the “stripes” and “pairs” approach on Hadoop for the following tasks, but I would like to know how to make communication between several cartographers and how to do probability-oriented aggregations inside my reducer.

  • The occurrences of each pair of elements, the account (A, B) = # transactions contains both A and B, and the conditional probability Prob (B | A) = Count (A, B) / Count (A).
  • Associations of each triple of elements, the graph (A, B, C) = # transactions contains both A and B, and the conditional probability Prob (A | B, C) = Count (A, B, C) / Count (B, C )
  • Each line records a transaction (a set of items that are bought together): the input is transactional data with the following format:

    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 ================================================= == ======================================= **

+4
source share
1 answer

, mappers..., - . , , , .

, , , , . , , , ( )

, , . , , .

mapper 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

, , . python . , , , , , , .

0
source

Source: https://habr.com/ru/post/1532935/


All Articles