Skip to content

Instantly share code, notes, and snippets.

@GabsGear
Created April 15, 2019 15:52
Show Gist options
  • Save GabsGear/a52a66c6504e420a36072ac8858fa77d to your computer and use it in GitHub Desktop.
Save GabsGear/a52a66c6504e420a36072ac8858fa77d to your computer and use it in GitHub Desktop.
testSet = trainingSet.build_anti_testset()
predictions = knn.test(testSet)
from collections import defaultdict
def get_top3_recommendations(predictions, topN = 3):
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment