Skip to content

Instantly share code, notes, and snippets.

@KevinLiao159
Last active November 5, 2018 06:20
Show Gist options
  • Save KevinLiao159/41df1538e21cd8cfbbbe768fc90f0265 to your computer and use it in GitHub Desktop.
Save KevinLiao159/41df1538e21cd8cfbbbe768fc90f0265 to your computer and use it in GitHub Desktop.
The "make_recommendations" method from my KNN recommender
def make_recommendations(self, fav_movie, n_recommendations):
"""
make top n movie recommendations
Parameters
----------
fav_movie: str, name of user input movie
n_recommendations: int, top n recommendations
"""
# get data
movie_user_mat_sparse, hashmap = self._prep_data()
# get recommendations
raw_recommends = self._inference(
self.model, movie_user_mat_sparse, hashmap,
fav_movie, n_recommendations)
# print results
reverse_hashmap = {v: k for k, v in hashmap.items()}
print('Recommendations for {}:'.format(fav_movie))
for i, (idx, dist) in enumerate(raw_recommends):
print('{0}: {1}, with distance '
'of {2}'.format(i+1, reverse_hashmap[idx], dist))
@KevinLiao159
Copy link
Author

For entire source code of KNN recommender, please visit this page

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment