我是MapReduce的新手,我试图在MovieLens 100k数据集中找到电影的平均电影评论。我有一个工作程序,可以查找每部电影的平均评论,但是我只想对评论> 100的电影执行此操作。如何添加条件语句来执行此操作?

from mrjob.job import MRJob

class PopularMovieAvgReview(MRJob):
    def mapper(self, key, line):
        (userID, movieID, rating, timestamp) = line.split('\t')
        yield movieID, float(rating)

    def reducer(self, movieID, rating):
        total = 0
        numElements = 0
        for x in rating:
            total += x
            numElements += 1
        yield movieID, total / numElements

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

最佳答案

如果我理解正确,您想根据给出的等级数限制输出

def reducer(self, movieID, rating):
    total = 0
    numElements = 0
    for x in rating:
        total += x
        numElements += 1
    if numElements > 100:
        yield movieID, total / numElements

另外,您可以使用PySpark进行汇总,然后过滤评分金额

关于python - Python MapReduce如何添加条件语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59758807/

10-08 22:01