EasyRecommender Systems

Top-K Recommendations

Recommender Systems

Easy

Problem

The final step in most recommender systems is selecting the top-K items to present to a user. Given predicted scores for all items and a set of items the user has already rated, the system must return the K highest-scoring items that the user has not yet seen.

Given a list of predicted scores (one per item), a collection of already-rated item indices, and a count K, return the indices of the top-K unrated items sorted by descending score.

Algorithm

  1. Filter out items that appear in the rated set
  2. Sort the remaining items by their predicted score in descending order
  3. Return the indices of the top K items

Return at most k unrated indices ordered by descending score; break ties by smaller index.

Theory

Top-K recommendations are the K highest-scoring items presented to a user. The recommendation system computes a score for each candidate item, ranks them by score, and returns the top K.

R_K(u) = \text{argmax}_{S \subset I, |S|=K} \sum_{i \in S} \text{score}(u, i)

In practice, this is simply: sort items by score, take the top K.


The Scoring Function

Different algorithms use different scoring functions:

Collaborative filtering:

\text{score}(u, i) = \hat{r}_{ui} = \text{predicted rating}

Content-based:

\text{score}(u, i) = \text{sim}(\text{user profile}, \text{item features})

Matrix factorization:

\text{score}(u, i) = \mathbf{p}_u^T \mathbf{q}_i

Hybrid:

\text{score}(u, i) = \alpha \cdot \text{CF score} + \beta \cdot \text{content score}


The Selection Process

Step 1: Identify candidate items

Step 2: Compute scores for candidates

Step 3: Sort by score (descending)

Step 4: Select top K

Step 5: Optionally apply business rules or diversity constraints


Worked Example

User U's candidate items with predicted scores:

Sorted by score:

C (4.8) > F (4.7) > A (4.5) > D (4.1) > E (3.9) > B (3.2)

Top-3 recommendations:

{C, F, A}


Filtering Candidates

Not all items should be candidates:

Already consumed:

Remove items the user has already rated/purchased/watched.

Availability:

Only include in-stock items, currently streaming content, etc.

Business rules:

Exclude items that violate policies, age restrictions, regional licensing.

Recency:

Only include items from the last N days/months.


Choosing K

K depends on context:

User expectations:

Users expect to see enough options but not be overwhelmed.

Screen real estate:

K is often determined by UI layout.


Ties in Scoring

When multiple items have identical scores:

Random tie-breaking:

Randomly select among tied items.

Secondary sort:

Break ties using popularity, recency, or another criterion.

Consistent ordering:

Use item ID for deterministic results (useful for debugging).


Computational Efficiency

Naive approach: Score all items, sort all, take top K.

Optimized: Use partial sort or heap.

Approximate: For very large catalogs, use approximate nearest neighbor search (LSH, FAISS).


Candidate Generation + Re-Ranking

Two-stage approach for large catalogs:

Stage 1: Candidate generation

Quickly identify ~100-1000 promising items using simple models or indexes.

Stage 2: Re-ranking

Score candidates with a more sophisticated model, select top K.

This balances accuracy and efficiency.


Diversity in Top-K

Pure top-K by score may lack diversity:

Problem: Top-K might be 10 action movies if user likes action.

Solution: Diversification

Balance relevance with diversity:

R_K = \text{argmax}_{S: |S|=K} \sum_{i \in S} \text{score}(i) + \lambda \cdot \text{diversity}(S)

MMR (Maximal Marginal Relevance):

Iteratively select items that are relevant but different from already selected items.


Exploration vs Exploitation

Exploitation:

Recommend items with highest predicted scores.

Exploration:

Recommend some uncertain items to learn more about user preferences.

Epsilon-greedy:

With probability \epsilon, recommend random items; otherwise, top-K by score.

Thompson sampling:

Sample from posterior distribution of scores.


Position Bias

Users are more likely to interact with items at the top of the list.

Implication:

Position 1 gets more clicks than position 10, regardless of relevance.

Consideration:

When evaluating, position bias in historical data can confound results.

Mitigation:

Occasionally randomize positions or use inverse propensity scoring.


Evaluation of Top-K

Relevance metrics:

Beyond accuracy:


Top-K vs Rating Prediction

Rating prediction task:

Predict the exact rating \hat{r}_{ui}.

Top-K recommendation task:

Rank items correctly; exact score does not matter.

An algorithm can have poor RMSE but good ranking (and vice versa).

Evaluation should match the task:

For top-K, use ranking metrics (precision, recall, NDCG), not RMSE.


Personalization Quality

Top-K should be personalized:

Different users should get different recommendations based on their preferences.

Sanity check:

If all users get the same top-K, the system is essentially non-personalized (popularity-based).

Measure personalization:

Compute average overlap between users' top-K lists. Lower overlap = more personalization.


Online vs Offline Top-K

Offline (batch):

Precompute top-K for each user periodically.

Online (real-time):

Compute top-K at request time.

Hybrid:

Precompute candidates, re-rank online with recent context.


Cold Start in Top-K

New user:

No personalized scores available.

New item:

No collaborative signals.


Business Constraints

Real systems have constraints beyond pure relevance:

Inventory:

Do not recommend out-of-stock items.

Freshness:

Promote new content.

Sponsored items:

Insert paid placements.

Fairness:

Ensure diverse content creators get exposure.

Top-K selection must balance relevance with these constraints.

Examples

Example 1

Input
scores = [3.5, 1.2, 4.8, 2.1, 5.0], rated_indices = [0, 2], k = 2
Output
[4, 3]
Explanation
After removing items 0 and 2, items 4 and 3 have the two highest scores.

Example 2

Input
scores = [1.0, 3.0, 2.0], rated_indices = [], k = 2
Output
[1, 2]

Hints

  1. Build score-index pairs only for indices absent from rated_indices.
  2. Sort by negative score and then index before taking the first k pairs.

Requirements

Constraints

Starter Code

def top_k_recommendations(scores: list, rated_indices: list, k: int) -> list:
    """
    Returns the highest-scoring unrated item indices.
    """
    # Write code here
    pass

Test Cases

CaseMatches
Basic filtering and sortingpublic
No rated itemspublic