How to fix function with for loop not iterating. Recommendation system in Python
21:45 08 Sep 2019

I'm working on a recommendation system with the python library Surprise. I'm trying to make a function that iterates over the full list of movies and predicts their ranking value against a user id which is defined in when I call the function.

This is for Python3 with the library surprise and the SVD algorithm. The dataset is from open source 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)

I expect the function to build a list of ratings (value [3] of prediction) for each item and a single user given in the function calling.

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

But the actual output is a list with length equal to the length of the list of movies (which is fine) but with the same rating repeated over and over.

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

So seing the actual output I think there is a problem with the for loop which iterates and appends the result but uses the same iid on each iteration. I'm trying to make a function where the uid stays fixed but the iid changes for each iteration.

EDIT: as suggested I added print(iid) inside the loop and it iterates correctly printing all the iids. But it doesn't work inside the pred = algorithm.predict(uid, iid) line

python pandas machine-learning data-science recommendation-engine