【问题标题】:Movie recommender system: For every movie find the movie which the raters rated as well电影推荐系统:为每部电影找到评分者评分的电影
【发布时间】:2021-06-04 21:04:36
【问题描述】:

我有一个嵌套的电影分级字典:

{'约翰':{'星球大战': 1.0, “沉默”:2.0, '福雷斯特':3.0}, '玛丽亚':{'星球大战':南, “沉默”:1.0, '福雷斯特':2.0}, “迈克”:{“星球大战”:9.0, '沉默':南, '福雷斯特':nan},

现在我想创建一个函数,计算每部电影有多少人看过该电影(分数!= nan)也看过其他电影,然后按降序排列。

我希望这很清楚。 提前致谢

【问题讨论】:

  • 不确定您的预期输出格式是什么,您可以自定义示例代码(如下)以满足您的需求。

标签: python dictionary recommender-systems


【解决方案1】:

这将是我对这个排名系统的方法:

from collections import defaultdict

nan = 0    # to simplify the parsing...

dc = {'John' :{'Star Wars': 1.0, 'Silence': 2.0, 'Forrest' : 3.0},
      'Maria':{'Star Wars': 0, 'Silence': 1.0, 'Forrest':2.0},
       'Mike':{'Star Wars': 9.0, 'Silence': 0, 'Forrest': 0}}
       
scores = defaultdict(int)

for name, movies in dc.items():
   
    for movie, score in movies.items():
        if score:       # not 0
            scores[movie] += score
    
print(scores)
ranking = {k: v for k, v in sorted(scores.items(),
                    key=lambda item: -item[1])}
print(ranking)    # {'Star Wars': 10.0, 'Forrest': 5.0, 'Silence': 3.0}

【讨论】:

  • 如果这个答案有帮助,请点击复选标记!欢迎评论进一步澄清!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
相关资源
最近更新 更多