-
Notifications
You must be signed in to change notification settings - Fork 253
/
recommendation.py
27 lines (21 loc) · 993 Bytes
/
recommendation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
def load_data():
# Load interaction data
data = pd.read_csv('user_interactions.csv')
return data
def build_model(data):
# Create user-item interaction matrix
user_item_matrix = data.pivot_table(index='user_id', columns='item_id', values='interaction', aggfunc='count', fill_value=0)
# Compute cosine similarity between users
user_similarity = cosine_similarity(user_item_matrix)
return user_item_matrix, user_similarity
def recommend_items(user_id, user_item_matrix, user_similarity, num_recommendations=5):
user_idx = user_item_matrix.index.get_loc(user_id)
similar_users = list(enumerate(user_similarity[user_idx]))
similar_users = sorted(similar_users, key=lambda x: x[1], reverse=True)
recommendations = []
for user, score in similar_users:
if user != user_idx:
similar_user_items = user_item_matrix.iloc