Skip to content

Instantly share code, notes, and snippets.

@LouisdeBruijn
Last active August 28, 2019 12:03
Show Gist options
  • Save LouisdeBruijn/2a79695ca3633b80154427226c2bf9df to your computer and use it in GitHub Desktop.
Save LouisdeBruijn/2a79695ca3633b80154427226c2bf9df to your computer and use it in GitHub Desktop.
Similar users and movies
def most_similar_items(item_id, n_similar=10):
'''computes the most similar items'''
with open('model.sav', 'rb') as pickle_in:
model = pickle.load(pickle_in)
similar, _ = zip(*model.similar_items(item_id, n_similar)[1:])
return map_movies(similar)
def most_similar_users(user_id, n_similar=10):
'''computes the most similar users'''
sparse_user_item = load_npz("sparse_user_item.npz")
with open('model.sav', 'rb') as pickle_in:
model = pickle.load(pickle_in)
# similar users gives back [(users, scores)]
# we want just the users and not the first one, because that is the same as the original user
similar, _ = zip(*model.similar_users(user_id, n_similar)[1:])
# orginal users items
original_user_items = list(sparse_user_item[user_id].indices)
# # this maps back user_ids to their information, which is useful for visualisation
similar_users_info = map_users(similar)
# # now we want to add the items that a similar used has rated
for user_info in mapped:
# we create a list of items that correspond to the simillar user ids
# then compare that in a set operation to the original user items
# as a last step we add it as a key to the user information dictionary
user_info['items'] = set(list(sparse_user_item[user_info['user_id']].indices)) & set(original_user_items)
return similar_users_info
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment