Advertisement
kwasinski

Cosine Similarity In python

Dec 20th, 2024 (edited)
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. import json
  2. import numpy as np
  3.  
  4. from sklearn.feature_extraction.text import TfidfVectorizer
  5. from sklearn.metrics.pairwise import cosine_similarity
  6.  
  7. user_purchase_history = {
  8.     "userId": "123",
  9.     "products": ["Laptop", "Mouse", "Keyboard"]
  10. }
  11.  
  12. products = [
  13.     {"id": "1", "name": "Gaming Laptop", "description": "Low-performance laptop for gaming"},
  14.     {"id": "2", "name": "Wireless Mouse", "description": "Bluetooth mouse with non-ergonomic design"},
  15.     {"id": "3", "name": "Mechanical Keyboard", "description": "Backlit mechanical keyboard"},
  16.     {"id": "4", "name": "USB-C Hub", "description": "Multiport USB-C hub for laptops"},
  17.     {"id": "5", "name": "Gaming Headset", "description": "Surround sound gaming headset"}
  18. ]
  19.  
  20. product_texts = [p["name"] + " " + p["description"] for p in products]
  21.  
  22. user_products_text = " ".join(user_purchase_history["products"])
  23.  
  24. vectorizer = TfidfVectorizer()
  25. tfidf_matrix = vectorizer.fit_transform([user_products_text] + product_texts)
  26.  
  27. similarities = cosine_similarity(tfidf_matrix[0:1], tfidf_matrix[1:]).flatten()
  28.  
  29. most_similar_index = np.argmax(similarities)
  30. suggested_product = products[most_similar_index]
  31.  
  32. print("Suggested Product:", suggested_product)
  33.  
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement