Skip to content

Instantly share code, notes, and snippets.

@nahidalam
Created July 9, 2020 17:54
Show Gist options
  • Save nahidalam/7070e9cbcbed2dc84751d1aee35951c1 to your computer and use it in GitHub Desktop.
Save nahidalam/7070e9cbcbed2dc84751d1aee35951c1 to your computer and use it in GitHub Desktop.
#lets assume an user ID 200
TEST_USER_ID = 200
#get the embedding of this user
user_embedding = user_model.predict([TEST_USER_ID]).reshape(1,-1)[0]
#create the KNN model
from sklearn.neighbors import KNeighborsClassifier
clf = KNeighborsClassifier(n_neighbors=11)
clf.fit(MOVIE_EMBEDDING_LIST, knn_train_label)
def recommend_movies(embedding):
distances, indices = clf.kneighbors(embedding.reshape(1, -1), n_neighbors=10)
indices = indices.reshape(10,1)
df_indices = pd.DataFrame(indices, columns = ['movie_id'])
return df_indices.merge(movies,on='movie_id',how='inner',suffixes=['_u', '_m'])['title']
# get the recommendation
recommend_movies(user_embedding)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment