Recommend the most popular unseen items

Why it matters Cold-start users on any feed see a popularity list like this.

The baseline every recommender must beat: recommend what is popular that the user has not seen.

Write popular_unseen(interactions, user, k). interactions is a list of (user, item) pairs. Count interactions per item across all users, drop items this user already has, and return the top k item ids by count, breaking ties by smaller item id.

Signaturedef popular_unseen(interactions, user, k)

Example

interactions [(1, 10), (2, 10), (2, 11), (3, 12)], user 1, k 2 gives [11, 12].

Understand the idea

The most basic recommender says: show the user the items that are most popular overall, skipping anything they already have. It sounds too simple, but it is a strong baseline that fancy models must beat. Worked example: interactions [(1, 5), (2, 5), (2, 7), (3, 7), (3, 9), (4, 8)], user 2, k 2. Counts across all users are item 5: 2, item 7: 2, item 8: 1, item 9: 1. User 2 already has items 5 and 7, so they are removed. Items 8 and 9 both have count 1, a tie, so the smaller id comes first: the answer is [8, 9]. Rule: count interactions per item over every user, build the set of items this user has touched, drop those, sort the rest by count descending and then by item id ascending, and return the first k. Used in: cold-start recommendations for new users, fallback lists when a model is down, and as the first baseline in any recommender evaluation.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks all 3.

  • Sample 1: examplepopular_unseen([(1, 10), (2, 10), (2, 11), (3, 12)], 1, 2)
  • Sample 2: tiespopular_unseen([(1, 5), (2, 3), (3, 3), (4, 5), (1, 7)], 9, 3)
  • Test 3: everything seenpopular_unseen([(1, 1), (1, 2)], 1, 2)