Untitled

mail@pastecode.io avatar
unknown
abc
2 years ago
674 B
4
Indexable
Never
%%file Ratings.py
from mrjob.job import MRJob
from mrjob.step import MRStep


class Ratings(MRJob):
    

    def steps(self):
          return [MRStep(mapper=self.mapper1, reducer=self.reducer1)]  
        
    def mapper1(self,_,line):
        (userID, movieID, rating, timestamp) = line.split('\t')
        yield movieID, rating
        
    def reducer1(self, key, values):
        rating_total = 0
        count = 0
        for i in values:
            rating_total += int(i)
            count += 1
            
        if count>=1:
            yield key, (rating_total / int(count))
            

if __name__ == '__main__':
    Ratings.run()