waseoke commited on
Commit
0182b00
·
verified ·
1 Parent(s): a65b2d6

Create calculate_cosine_similarity.py

Browse files
Files changed (1) hide show
  1. calculate_cosine_similarity.py +12 -0
calculate_cosine_similarity.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sklearn.metrics.pairwise import cosine_similarity
2
+ import numpy as np
3
+
4
+ def calculate_cosine_similarity(user_embedding, product_embeddings, product_ids, top_n=5):
5
+ user_embedding = user_embedding.reshape(1, -1)
6
+ product_embeddings = np.array(product_embeddings)
7
+
8
+ similarities = cosine_similarity(user_embedding, product_embeddings).flatten()
9
+ top_indices = similarities.argsort()[::-1][:top_n]
10
+ recommendations = [(product_ids[i], similarities[i]) for i in top_indices]
11
+
12
+ return recommendations