Spaces:
Sleeping
Sleeping
Create calculate_cosine_similarity.py
Browse files
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
|