如何修复 for 循环不迭代的函数。 Python 中的推荐系统

How to fix function with for loop not iterating. Recommendation system in Python

我正在使用 python 库 Surprise 开发推荐系统。 我正在尝试创建一个函数来遍历完整的电影列表,并根据我调用该函数时定义的用户 ID 预测它们的排名值。

这是 Python3 库惊喜和 SVD 算法。数据集来自开源MovieLens:Link to the dataset

from surprise import Dataset
from surprise import Reader
from surprise import SVD
import pandas as pd

reader = Reader(line_format='user item rating timestamp', sep='\t')
data = Dataset.load_from_file('./dataset/ml-100k/u.data', reader=reader)
movies = pd.read_csv('./dataset/ml-100k/u.item',sep='|', encoding='latin-1', usecols=[0,1], names=['iid', 'name'])
algorithm = SVD()
trainset = data.build_full_trainset()
algorithm.fit(trainset)

def prediction(uid):
    ratings=[]
    for iid in movies['iid']:
        pred = algorithm.predict(uid, iid)
        ratings.append(pred[3])
    return ratings

prediction(321)

我希望该函数为函数调用中给定的每个项目和单个用户构建评分列表(预测值 [3])。

[3.52986,
 3.69845,
 2.98954,
 3.00545,
 3.84254,
 ...]

但实际输出的是一个列表,其长度等于电影列表的长度(这很好),但具有相同的评级一遍又一遍地重复。

 [3.52986,
 3.52986,
 3.52986,
 3.52986,
 3.52986,
 3.52986,
 3.52986,
 3.52986,
 ...]

所以看到实际输出我认为 for 循环有问题,它迭代并附加结果但在每次迭代中使用相同的 iid。我正在尝试创建一个函数,其中 uid 保持固定但 iid 每次迭代都会发生变化。

编辑:按照建议我在循环中添加了 print(iid) 并且它正确地迭代打印所有 iid。但它在 pred = algorithm.predict(uid, iid) line

中不起作用

循环正常。看来我对 SVD 算法的 return 结果使用了不正确的方法。 同时,我发现以下代码产生的结果与我使用自己的函数寻找的结果有些相似。

感谢大家的时间和努力。

   from collections import defaultdict
   testset = trainset.build_anti_testset()
   predictions = algorithm.test(testset)

   def get_top_n(predictions, n=10):
   top_n = defaultdict(list)
   for uid, iid, true_r, est, _ in predictions:
       top_n[uid].append((iid, est))
   for uid, user_ratings in top_n.items():
       user_ratings.sort(key=lambda x: x[1], reverse=True)
       top_n[uid] = user_ratings[:n]
   return top_n

   top_n = get_top_n(predictions, n=10)

   for uid, user_ratings in top_n.items():
   print(uid, [iid for (iid, _) in user_ratings])
196 ['408', '127', '190', '187', '318', '64', '191', '357', '169', '272']
186 ['496', '165', '515', '923', '318', '313', '64', '223', '530', '478']
22 ['22', '205', '357', '100', '169', '923', '64', '269', '69', '285']
244 ['474', '127', '408', '483', '285', '12', '1142', '134', '493', '480']
166 ['318', '174', '408', '98', '483', '64', '480', '114', '169', '511']
298 ['64', '169', '12', '478', '114', '272', '166', '408', '513', '923']
115 ['408', '168', '114', '285', '483', '919', '169', '480', '179', '189']