Skip to content

Instantly share code, notes, and snippets.

@GabsGear
Created April 15, 2019 15:57
Show Gist options
  • Save GabsGear/861f8ab67956f0203ea0b153bc82e5d4 to your computer and use it in GitHub Desktop.
Save GabsGear/861f8ab67956f0203ea0b153bc82e5d4 to your computer and use it in GitHub Desktop.
import os, io
def read_item_names():
"""Read the u.item file from MovieLens 100-k dataset and returns a
mapping to convert raw ids into movie names.
"""
file_name = (os.path.expanduser('~') +
'/.surprise_data/ml-100k/ml-100k/u.item')
rid_to_name = {}
with io.open(file_name, 'r', encoding='ISO-8859-1') as f:
for line in f:
line = line.split('|')
rid_to_name[line[0]] = line[1]
return rid_to_name
top3_recommendations = get_top3_recommendations(predictions)
rid_to_name = read_item_names()
for uid, user_ratings in top3_recommendations.items():
print(uid, [rid_to_name[iid] for (iid, _) in user_ratings])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment