Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Principle:Recommenders team Recommenders SAR Recommendation Generation

From Leeroopedia


Knowledge Sources
Domains Recommender Systems, Collaborative Filtering, Top-K Recommendation
Last Updated 2026-02-10 00:00 GMT

Overview

SAR recommendation generation produces top-K personalized item recommendations for each user by computing the product of the user-affinity matrix and the item-similarity matrix, then selecting the highest-scoring items per user.

Description

After a SAR model has been trained (i.e., the user-affinity matrix A and item-similarity matrix S have been computed), generating recommendations is a straightforward matrix multiplication followed by a top-K selection:

  1. Scoring: Compute the full score matrix as scores = A x S. Each entry scores[u, i] represents the predicted affinity of user u for item i. This is a dense operation but is performed efficiently using sparse matrix multiplication since both A and S are sparse.
  2. Seen-item removal (optional): If the user should not be recommended items they have already interacted with, the scores for those items are set to negative infinity so they will not appear in the top-K.
  3. Top-K selection: For each user, select the K items with the highest scores. Optionally sort the results by score in descending order.

The output is a flat DataFrame with (user, item, predicted_score) tuples, containing at most K recommendations per user.

Usage

Use this technique after training a SAR model to produce a ranked list of item recommendations for users in a test set. Typical scenarios include:

  • Generating a top-10 recommendation list for each user to evaluate with ranking metrics (Precision@K, NDCG@K, etc.).
  • Producing personalized recommendations for deployment in a production system.
  • Comparing recommendation quality across different similarity metrics or model configurations.

Theoretical Basis

Scoring formula:

For user u and item i, the recommendation score is:

score(u, i) = sum over all items j: A[u, j] * S[j, i]

In matrix form:

Scores = A x S

Where:

  • A is the user-affinity matrix (|Users| x |Items|, sparse)
  • S is the item-similarity matrix (|Items| x |Items|, sparse)
  • Scores is the resulting score matrix (|Users| x |Items|)

Seen-item removal:

if remove_seen:
    for each user u:
        for each item i where A[u, i] > 0:
            Scores[u, i] = -infinity

This ensures that items the user has already rated are excluded from the recommendation list.

Top-K selection:

function recommend_k_items(Scores, K, sort):
    results = []
    for each user u:
        top_k_indices = argpartition(Scores[u, :], -K)[-K:]
        if sort:
            top_k_indices = sort_descending(top_k_indices, by=Scores[u, :])
        for i in top_k_indices:
            results.append((u, index_to_item[i], Scores[u, i]))
    return results

Using argpartition rather than full sort achieves O(N) average complexity for top-K selection per user, rather than O(N log N) for a full sort.

Score normalization (optional):

If the model was initialized with normalize=True, scores are scaled to the original rating range:

normalized_score(u, i) = rating_min + (score(u, i) / unity_score(u, i)) * (rating_max - rating_min)

Related Pages

Implemented By

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment