Skip to content

Instantly share code, notes, and snippets.

@susanli2016
Created January 16, 2022 08:56
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/931e0d151cd92fe48c2f91c0947472a8 to your computer and use it in GitHub Desktop.
Save susanli2016/931e0d151cd92fe48c2f91c0947472a8 to your computer and use it in GitHub Desktop.
def recommend(customer_id, sparse_customer_item, customer_vecs, item_vecs, num_items=10):
customer_interactions = sparse_customer_item[customer_id,:].toarray()
customer_interactions = customer_interactions.reshape(-1) + 1
customer_interactions[customer_interactions > 1] = 0
rec_vector = customer_vecs[customer_id,:].dot(item_vecs.T).toarray()
min_max = MinMaxScaler()
rec_vector_scaled = min_max.fit_transform(rec_vector.reshape(-1,1))[:,0]
recommend_vector = customer_interactions * rec_vector_scaled
item_idx = np.argsort(recommend_vector)[::-1][:num_items]
descriptions = []
scores = []
for idx in item_idx:
descriptions.append(grouped_df.Description.loc[grouped_df.item_id == idx].iloc[0])
scores.append(recommend_vector[idx])
recommendations = pd.DataFrame({'description': descriptions, 'score': scores})
return recommendations
customer_vecs = sparse.csr_matrix(model.user_factors)
item_vecs = sparse.csr_matrix(model.item_factors)
# Create recommendations for customer with id 2
customer_id = 2
recommendations = recommend(customer_id, sparse_customer_item, customer_vecs, item_vecs)
print(recommendations)
@ofendri
Copy link

ofendri commented Oct 26, 2022

Hello Susan, I'm writing my bachelor thesis in recommender systems for e-commerce. I have had some issues trying to run this code on my dataframe. Is there a way to contact you and ask for some help to understand it better please? :) I'd really appreciate that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment