File size: 5,771 Bytes
dd33d52 | 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 180 181 182 183 184 185 186 187 | """
Unified evaluator for all three recommendation models.
Given a test split and trained models, generates recommendations for each
test user and computes the full metric suite.
Also generates comparison plots saved to data/outputs/figures/.
"""
from __future__ import annotations
import pickle
from pathlib import Path
from typing import Callable
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from tabulate import tabulate
from src.evaluation.metrics import compute_all_metrics
from src.utils.logging import get_logger
logger = get_logger(__name__)
RecommendFn = Callable[[int], list[tuple[int, float]]]
def evaluate_model(
recommend_fn: RecommendFn,
test_df: pd.DataFrame,
train_df: pd.DataFrame,
k_values: list[int] = (5, 10, 20),
top_k: int = 20,
) -> dict[str, float]:
"""
Evaluate a recommendation function on the test set.
For each test user:
- Ground truth = items in test_df for that user
- Exclude items already seen in train_df
Args:
recommend_fn: Function (user_idx) β list of (item_idx, score).
test_df: Test split with [user_idx, item_idx] (one positive per user).
train_df: Training split to build seen-items exclusion sets.
k_values: K cutoffs.
top_k: Maximum K to retrieve from the recommender.
Returns:
Dict of metric names β mean values across users.
"""
# Build per-user ground truths and seen sets
gt_map: dict[int, list[int]] = (
test_df.groupby("user_idx")["item_idx"]
.apply(list)
.to_dict()
)
all_recs: list[list[int]] = []
all_gts: list[list[int]] = []
test_users = list(gt_map.keys())
for uid in test_users:
try:
recs = recommend_fn(uid)
rec_items = [item_idx for item_idx, _ in recs]
except Exception as exc:
logger.warning("Recommendation failed for user %d: %s", uid, exc)
rec_items = []
all_recs.append(rec_items)
all_gts.append(gt_map[uid])
metrics = compute_all_metrics(all_recs, all_gts, k_values=list(k_values))
return metrics
def evaluate_all_models(
test_df: pd.DataFrame,
train_df: pd.DataFrame,
naive_recommend_fn: RecommendFn,
classical_recommend_fn: RecommendFn,
deep_recommend_fn: RecommendFn,
k_values: list[int] = (5, 10, 20),
top_k: int = 20,
output_dir: Path = Path("data/outputs"),
) -> pd.DataFrame:
"""
Evaluate all three models and produce comparison artifacts.
Args:
test_df: Test split.
train_df: Training split.
naive_recommend_fn: Callable for naive model.
classical_recommend_fn: Callable for classical model.
deep_recommend_fn: Callable for deep model.
k_values: K cutoffs.
top_k: Retrieval depth.
output_dir: Directory to save metrics CSV and plots.
Returns:
DataFrame with one row per model and metric columns.
"""
output_dir.mkdir(parents=True, exist_ok=True)
model_fns = {
"Naive (Popularity)": naive_recommend_fn,
"Classical (LightGBM)": classical_recommend_fn,
"Deep (Two-Tower)": deep_recommend_fn,
}
rows = []
for model_name, fn in model_fns.items():
logger.info("Evaluating %sβ¦", model_name)
metrics = evaluate_model(fn, test_df, train_df, k_values=k_values, top_k=top_k)
row = {"Model": model_name, **metrics}
rows.append(row)
for k, v in metrics.items():
logger.info(" %s: %.4f", k, v)
results_df = pd.DataFrame(rows)
# Save CSV
csv_path = output_dir / "metrics.csv"
results_df.to_csv(csv_path, index=False)
logger.info("Metrics saved β %s", csv_path)
# Save markdown table
md_table = tabulate(
results_df.round(4), headers="keys", tablefmt="github", showindex=False
)
md_path = output_dir / "metrics_table.md"
md_path.write_text(f"# Evaluation Results\n\n{md_table}\n")
logger.info("Metrics table saved β %s", md_path)
# Plot NDCG@K comparison
_plot_metric_comparison(results_df, k_values, output_dir / "figures" / "metrics_comparison.png")
return results_df
def _plot_metric_comparison(
results_df: pd.DataFrame,
k_values: list[int],
output_path: Path,
) -> None:
"""Plot Recall@K and NDCG@K bar charts for all models."""
output_path.parent.mkdir(parents=True, exist_ok=True)
models = results_df["Model"].tolist()
x = np.arange(len(models))
width = 0.25
colors = ["#3b82f6", "#f97316", "#10b981"]
fig, axes = plt.subplots(1, 2, figsize=(14, 5))
for ax, metric_prefix, ylabel in zip(
axes,
["NDCG@", "Recall@"],
["NDCG@K", "Recall@K"],
):
for i, k in enumerate(k_values):
col = f"{metric_prefix}{k}"
if col in results_df.columns:
values = results_df[col].values
bars = ax.bar(x + i * width, values, width, label=f"K={k}", color=colors[i % 3])
for bar, v in zip(bars, values):
ax.text(
bar.get_x() + bar.get_width() / 2,
bar.get_height() + 0.002,
f"{v:.3f}",
ha="center", va="bottom", fontsize=8,
)
ax.set_xticks(x + width)
ax.set_xticklabels(models, rotation=10, ha="right")
ax.set_ylabel(ylabel)
ax.set_title(ylabel)
ax.legend()
ax.set_ylim(0, 1)
plt.suptitle("Model Comparison β Test Set Metrics", fontsize=13)
plt.tight_layout()
fig.savefig(output_path, dpi=150)
plt.close(fig)
logger.info("Metrics comparison plot saved β %s", output_path)
|