import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
# Sample ratings data (user-item matrix)
ratings = np.array([
[5, 3, 4, 4, 0], # User 1
[1, 0, 5, 0, 4], # User 2
[0, 3, 0, 4, 0], # User 3
[5, 0, 4, 3, 5] # User 4
])
# Calculate item-item similarity matrix using cosine similarity
item_similarity = cosine_similarity(ratings.T)
# Function to generate item recommendations for a given item
def get_item_recommendations(item_id, top_n=3):
item_scores = item_similarity[item_id]
top_items = np.argsort(item_scores)[-top_n-1:-1][::-1]
return top_items
# Example usage:
item_id = 2 # Item ID for which recommendations are needed
recommendations = get_item_recommendations(item_id, top_n=3)
print(f"Top recommendations for Item {item_id}: {recommendations}")
No comments:
Post a Comment