datadabllp

E-commerce Product Recommendations FAISS

Jul 19th, 2024
3,955
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  1. import faiss
  2. import numpy as np
  3.  
  4. # Assume we have product embeddings (vectors) for all our products
  5. product_embeddings = np.random.random((1000000, 128)).astype('float32')  # 1M products, 128-dimensional embeddings
  6.  
  7. # Create an index
  8. index = faiss.IndexFlatL2(128)  # Using L2 distance
  9.  
  10. # Add vectors to the index
  11. index.add(product_embeddings)
  12.  
  13. # Let's say a user is viewing a product with this embedding
  14. query_embedding = np.random.random((1, 128)).astype('float32')
  15.  
  16. # Search for similar products
  17. k = 5  # Number of similar products to retrieve
  18. distances, indices = index.search(query_embedding, k)
  19.  
  20. print(f"Indices of similar products: {indices}")
  21. print(f"Distances to similar products: {distances}")
  22.  
Advertisement
Add Comment
Please, Sign In to add comment