Repository navigation
Expand file tree
/
Copy pathrecommender.py
More file actions
179 lines (144 loc) · 6.4 KB
/
Copy pathrecommender.py
File metadata and controls
179 lines (144 loc) · 6.4 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# recommender.py
# Tiny item-item collaborative filter using cosine similarity.
# Usage examples:
# python recommender.py --generate-data
# python recommender.py --user U3 --top_n 5
# python recommender.py --user U3 --top_n 5 --method sklearn
import argparse
from pathlib import Path
from typing import Tuple
import numpy as np
import pandas as pd
# ----------------------------- Data utilities ----------------------------- #
def generate_toy_ratings(csv_path: Path,
n_users: int = 10,
n_items: int = 10,
density: float = 0.6,
random_state: int = 7) -> pd.DataFrame:
"""
Create a sparse long-form ratings table: columns[user_id, item_id, rating].
density ~ fraction of user-item pairs that have a rating.
"""
rng = np.random.default_rng(random_state)
users = [f"U{i}" for i in range(1, n_users + 1)]
items = [f"I{j}" for j in range(1, n_items + 1)]
rows = []
for u in users:
for it in items:
if rng.random() < density:
rating = int(rng.integers(1, 6)) # 1..5
rows.append((u, it, rating))
# Ensure at least one rating per user and per item
df = pd.DataFrame(rows, columns=["user_id", "item_id", "rating"])
for u in users:
if (df["user_id"] == u).sum() == 0:
it = rng.choice(items)
df.loc[len(df)] = (u, it, int(rng.integers(3, 6)))
for it in items:
if (df["item_id"] == it).sum() == 0:
u = rng.choice(users)
df.loc[len(df)] = (u, it, int(rng.integers(3, 6)))
csv_path.parent.mkdir(parents=True, exist_ok=True)
df.to_csv(csv_path, index=False)
return df
def load_ratings(csv_path: Path) -> pd.DataFrame:
if not csv_path.exists():
print(f"[info] {csv_path} not found; generating toy data...")
return generate_toy_ratings(csv_path)
df = pd.read_csv(csv_path)
expected_cols = {"user_id", "item_id", "rating"}
if not expected_cols.issubset(set(df.columns)):
raise ValueError(f"ratings.csv must have columns {expected_cols}")
return df
def to_user_item_matrix(df: pd.DataFrame) -> pd.DataFrame:
"""Pivot to a user-item matrix; fill NaNs with 0 for 'unrated'."""
mat = df.pivot_table(index="user_id", columns="item_id", values="rating", aggfunc="mean")
mat = mat.reindex(index=sorted(mat.index), columns=sorted(mat.columns))
return mat.fillna(0.0)
# --------------------------- Similarity functions ------------------------- #
def cosine_similarity_numpy(item_by_user: np.ndarray) -> np.ndarray:
"""
Compute cosine similarity between item vectors using NumPy.
item_by_user shape: (n_items, n_users)
Returns sim shape: (n_items, n_items)
"""
# Normalize rows (items)
norms = np.linalg.norm(item_by_user, axis=1, keepdims=True)
norms = np.where(norms == 0, 1e-12, norms)
X = item_by_user / norms
sim = X @ X.T
# zero self-similarity so it can't recommend the same item back
np.fill_diagonal(sim, 0.0)
return sim
def cosine_similarity_sklearn(item_by_user: np.ndarray) -> np.ndarray:
try:
from sklearn.metrics.pairwise import cosine_similarity
except ImportError:
raise SystemExit(
"[error] scikit-learn not installed. "
"Install it with: pip install scikit-learn"
)
sim = cosine_similarity(item_by_user)
np.fill_diagonal(sim, 0.0)
return sim
# ---------------------------- Recommendation core ------------------------ #
def recommend_for_user(user_id: str,
user_item: pd.DataFrame,
item_sim: np.ndarray,
top_n: int = 5) -> pd.DataFrame:
"""
Classic item-item scoring:
score(item_j) = sum_i sim(j, i) * rating_u(i)
Then normalize by the total similarity weight to avoid popularity bias.
"""
if user_id not in user_item.index:
raise KeyError(f"Unknown user_id '{user_id}'. Known: {list(user_item.index)}")
# shape: users x items
user_ratings = user_item.loc[user_id].to_numpy() # (n_items,)
rated_mask = user_ratings > 0 # bool (n_items,)
# sim is (n_items x n_items); multiply by user's ratings vector -> (n_items,)
raw_scores = item_sim @ user_ratings
# normalization by sum of absolute sim weights connected to user's rated items
weight = (np.abs(item_sim) @ rated_mask.astype(float)) # (n_items,)
scores = np.divide(raw_scores, np.maximum(weight, 1e-12))
# Don't recommend items the user already rated
scores[rated_mask] = -np.inf
items = list(user_item.columns)
top_idx = np.argsort(scores)[::-1][:top_n]
recs = pd.DataFrame({
"item_id": [items[i] for i in top_idx],
"score": [float(scores[i]) for i in top_idx]
})
return recs
# ----------------------------------- CLI --------------------------------- #
def build(args) -> Tuple[pd.DataFrame, np.ndarray]:
csv_path = Path(args.ratings)
df = load_ratings(csv_path)
user_item = to_user_item_matrix(df) # users x items
# transpose to items x users for similarity
item_by_user = user_item.to_numpy().T
if args.method == "sklearn":
sim = cosine_similarity_sklearn(item_by_user)
else:
sim = cosine_similarity_numpy(item_by_user)
return user_item, sim
def main():
parser = argparse.ArgumentParser(description="Tiny item-item recommender (cosine similarity).")
parser.add_argument("--ratings", default="data/ratings.csv", help="Path to ratings CSV")
parser.add_argument("--generate-data", action="store_true", help="Generate toy ratings and exit")
parser.add_argument("--user", default="U1", help="User ID to recommend for (e.g., U3)")
parser.add_argument("--top_n", type=int, default=5, help="How many items to recommend")
parser.add_argument("--method", choices=["numpy", "sklearn"], default="numpy",
help="Cosine similarity backend")
args = parser.parse_args()
if args.generate_data:
df = generate_toy_ratings(Path(args.ratings))
print(f"[ok] Generated {len(df)} ratings at {args.ratings}")
return
user_item, sim = build(args)
recs = recommend_for_user(args.user, user_item, sim, top_n=args.top_n)
print("\n=== Tiny Recommender ===")
print(f"User: {args.user}")
print(recs.to_string(index=False))
if __name__ == "__main__":
main()