How to fix the code, Recommendation system in python

Viewed 116

the distribution from the output of this code is not matching with the exact distribution 2: https://i.stack.imgur.com/H9lxC.png

dataset available @ http://grouplens.org/datasets/movielens/1m/

Dscription: Recommender system using collaborative filtering - userbased collaborative filgering (here used)

using Surprise python library

input: movilens dataset, (users, items, ratings)

output: top-15 predicted ratings for every user

the code am using is:

from surprise import Dataset, evaluate
from surprise import KNNBasic
from collections import defaultdict
from surprise import Reader
import os


file_path = os.path.expanduser('ratings.txt')
reader = Reader(line_format='user item rating ', sep=' ')
data=Dataset.load_from_file(file_path,reader)
trainingset=data.build_full_trainset()
sim_options={'name':'pearson','min_support':5,'user_based':True}
algo = KNNBasic(k=25,min_k=5,sim_options=sim_options,verbose=True)
algo.fit(trainingset)
testset=trainingset.build_anti_testset()
predictions=algo.test(testset)

def get_top15_recommendations(predictions, topN=15):
    top_recs = defaultdict(list)
    for uid, iid, true_r, est, _ in predictions:
        top_recs[uid].append((iid, est))

    for uid, user_ratings in top_recs.items():
        user_ratings.sort(key=lambda x: x[1], reverse=True)
        top_recs[uid] = user_ratings[:topN]
    return top_recs

 top15_recommendations = get_top15_recommendations(predictions)
 f=open("threshold.txt",'w')
 for uid, user_ratings in top15_recommendations.items():
     f.write(str(uid)),f.write(""),
     f.write(str(user_ratings[-1])),f.write("\n")  

'''

0 Answers
Related