-
-
Save elena-sharova/26f6c42a327ba0e40ba9557de27fb68c to your computer and use it in GitHub Desktop.
word2vec get recommendations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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