Skip to content

Instantly share code, notes, and snippets.

@elena-sharova
Last active July 31, 2022 11:45
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/567d003abdad819331eb91b38115a48a to your computer and use it in GitHub Desktop.
Save elena-sharova/567d003abdad819331eb91b38115a48a to your computer and use it in GitHub Desktop.
word2vec get similar product
def get_similar_product(model:Word2Vec, df:pd.DataFrame, product_id:int)->pd.DataFrame:
"""
Parameters
----------
model : instance of Word2Vec
df : dataframe with preprocessed data
product_id : int
unique product id for which we need to find similar product ids
Returns
-------
dataframe with similar products
"""
try:
sim_product = model.wv.most_similar(positive=[str(product_id)])
return df.loc[df['product_id'].isin([int(word[0])
for word in sim_product])][[
'category_code',
'brand',
'product_id']].drop_duplicates()
except KeyError:
return f"Cannot find the specified product with id {product_id}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment