gpt4 book ai didi

python - 使用 Python 的 MRjob 生成前 10 个值的 MapReduce 作业

转载 作者:太空宇宙 更新时间:2023-11-04 03:01:06 25 4
gpt4 key购买 nike

我希望这个 map reduce 作业(下面的代码)输出评分最高的前 10 个产品。它一直给我以下错误信息:

it = izip(iterable, count(0,-1)) # 装饰类型错误:izip 参数 #1 必须支持迭代。

我认为它与我尝试应用的nlargest 函数有关。

有什么建议吗?

谢谢!

from mrjob.job import MRJob
from mrjob.step import MRStep
from heapq import nlargest


class MostRatedProduct(MRJob):

def steps(self):
return [
MRStep(mapper = self.mapper_get_ratings,
reducer = self.reducer_count_ratings),
MRStep(reducer = self.reducer_find_top10)
]


def mapper_get_ratings(self, _, line):
(userID, itemID, rating, timestamp) = line.split(',')
yield itemID, 1

def reducer_count_ratings(self, itemID, ratingCount):
yield None, (sum(ratingCount), itemID)

def top_10(self, ratingPair):
for ratingTotal, itemID in ratingPair:
top_rated = nlargest(10, ratingTotal)
for top_rated in ratingTotal:
return (ratingTotal, itemID)

def reducer_find_top10(self, key, ratingPair):
ratingTotal, itemID = self.top_10(ratingPair)
yield ratingTotal, itemID


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

最佳答案

使用 mrjob 库,您可以在 python 中执行相同的操作:-

#Write a Code to print the top 5 word - occurences

#Import Dependencies
from mrjob.job import MRJob
from mrjob.step import MRStep

class MRWordCount(MRJob):

def steps(self):
return [MRStep(mapper=self.mapper,reducer=self.reducer),MRStep(reducer = self.secondreducer)]

def mapper(self,_,lines):
words = lines.split()
for word in words:
yield word.lower(),1

def reducer(self,key,values):
yield None,('%04d'%int(sum(values)),key)

def secondreducer(self,key,values):
self.alist = []
for value in values:
self.alist.append(value)
self.blist = []
for i in range(5):
self.blist.append(max(self.alist))
self.alist.remove(max(self.alist))
for i in range(5):
yield self.blist[i]

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

关于python - 使用 Python 的 MRjob 生成前 10 个值的 MapReduce 作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40870854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com