Skip to content

Instantly share code, notes, and snippets.

@elena-sharova
Last active July 31, 2022 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elena-sharova/26f6c42a327ba0e40ba9557de27fb68c to your computer and use it in GitHub Desktop.
Save elena-sharova/26f6c42a327ba0e40ba9557de27fb68c to your computer and use it in GitHub Desktop.
word2vec get recommendations
def recommend_next_purchase(model: Word2Vec, df:pd.DataFrame, user_id:int)->pd.DataFrame:
"""
Parameters
----------
model : instance of Word2Vec
df : dataframe with preprocessed data
user_id : int
unique user id for whom we make recommendations
Returns
-------
dataframe with recommended products
"""
try:
# Find the products the user browsed
viewed_products = df.loc[df['user_id']==user_id]['product_id'].unique()
# Get recommendations for next purchase
output_words = model.predict_output_word([str(product) for product in viewed_products])
return df.loc[df['product_id'].isin([int(word[0])
for word in output_words])][[
'category_code',
'brand',
'product_id']].drop_duplicates()
except KeyError:
return f"Cannot find the specified user with id {user_id}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment