-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollaborativeFiltering.py
69 lines (54 loc) · 2.5 KB
/
CollaborativeFiltering.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import numpy as np
class CollaborativeFiltering:
def __init__(self, user_item_matrix, user_map, article_map):
"""
Initialize with user-item matrix and mappings.
:param user_item_matrix: Sparse matrix (users x items)
:param user_map: Mapping from user IDs to matrix indices
:param article_map: Mapping from article IDs to matrix indices
"""
self.user_item_matrix = user_item_matrix
self.user_map = user_map
self.article_map = article_map
self.user_similarity = None
def compute_similarity(self):
"""Compute user-user cosine similarity."""
self.user_similarity = cosine_similarity(self.user_item_matrix)
def recommend(self, user_id, top_n=5):
"""
Generate article recommendations for a given user.
:param user_id: ID of the user for whom to generate recommendations.
:param top_n: Number of top recommendations to generate.
:return: List of recommended article IDs.
"""
if user_id not in self.user_map:
print(f"User ID {user_id} not found in the dataset.")
return []
user_idx = self.user_map[user_id]
# Calculate scores for all articles
user_similarities = self.user_similarity[user_idx]
scores = user_similarities.dot(self.user_item_matrix).flatten()
# Zero out scores for articles the user has already interacted with
interacted_items = self.user_item_matrix[user_idx].indices
scores[interacted_items] = 0
# Get top N article indices
top_article_indices = scores.argsort()[-top_n:][::-1]
# Map indices back to article IDs
idx_to_article = {v: k for k, v in self.article_map.items()}
recommendations = [idx_to_article[idx] for idx in top_article_indices]
return recommendations
def cosine_similarity(matrix):
"""
Compute the cosine similarity between users using NumPy.
:param matrix: A sparse user-item interaction matrix.
:return: Cosine similarity matrix.
"""
# Convert the matrix to dense (NumPy array) if it is sparse
matrix = matrix.toarray() if hasattr(matrix, 'toarray') else matrix
# Compute dot product of the matrix with its transpose
dot_product = np.dot(matrix, matrix.T)
# Compute the norms (magnitude) of each row in the matrix
norms = np.linalg.norm(matrix, axis=1)
# Compute cosine similarity matrix
cosine_sim = dot_product / (norms[:, None] * norms[None, :])
return cosine_sim