Skip to content

Instantly share code, notes, and snippets.

@susanli2016
Created January 16, 2022 08:34
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 susanli2016/ca72c229c49de7ff004527c2f9a4b3ac to your computer and use it in GitHub Desktop.
Save susanli2016/ca72c229c49de7ff004527c2f9a4b3ac to your computer and use it in GitHub Desktop.
item_id = 1319
n_similar = 10
item_vecs = model.item_factors
customer_vecs = model.user_factors
item_norms = np.sqrt((item_vecs * item_vecs).sum(axis=1))
scores = item_vecs.dot(item_vecs[item_id]) / item_norms
top_idx = np.argpartition(scores, -n_similar)[-n_similar:]
similar = sorted(zip(top_idx, scores[top_idx] / item_norms[item_id]), key=lambda x: -x[1])
for item in similar:
idx, score = item
print(grouped_df.Description.loc[grouped_df.item_id == idx].iloc[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment