Created
April 15, 2019 15:52
-
-
Save GabsGear/a52a66c6504e420a36072ac8858fa77d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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