Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 38 ยท Getting Started

Your First Recommender: A Popularity Baseline

Build your first recommender in Python: count-based, average-rating and Bayesian-weighted popularity lists, trending with time decay, and why they set the baseline.

The simplest recommender shows everyone the same list: the most popular items. It sounds too naive to matter, yet it is the baseline every other model must beat, the fallback for new users, and often the first thing shipped. Building it also teaches you the data handling you will reuse for the rest of the course.

In this lesson you create a small synthetic ratings dataset with pandas, then rank items three ways: by count, by average rating, and by a Bayesian weighted rating that fixes the problems of the first two. Finally you add time decay for a "trending" list.

A synthetic ratings table

Real data comes as a long table: one row per interaction with a user id, an item id and a rating. We generate one with a fixed seed so results are reproducible. Users have hidden taste for five genres, items have a main genre and a quality offset; ratings combine both with noise.

Python Create the ratings data

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

We will reuse this recipe in several lessons. Each lesson is self-contained, so the code is repeated rather than imported.

By count

Count how many users interacted with each item. This measures popularity in the literal sense, and it is the safest signal for implicit data such as clicks.

By average rating

Average the ratings. It measures quality as people experienced it, but it is fragile: an item rated 5 by a single user beats an item rated 4.7 by 500 users.

By weighted (Bayesian) rating

Pull each item's average toward the global average, more strongly when the item has few ratings:

score = (v / (v + m)) * R + (m / (v + m)) * C

Here v is the item's number of ratings, R its mean rating, C the global mean, and m a prior strength (how many "pseudo-ratings" at the global mean we pretend to have). With few ratings, the score stays near C; with many, it approaches R.

Python Count, mean and Bayesian rankings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at the second table: the best "mean" items usually have very few ratings, which is noise rather than quality. The weighted list is more trustworthy because tiny samples are shrunk toward the global mean. You will see the same shrinkage idea again in Shrinkage and Neighborhood Tuning.

Recommending from the list

To recommend for a user, take the ranked list and remove items they already rated.

Python A popularity recommender function

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that an unknown user (a cold-start user) still gets a sensible answer. That is the biggest practical strength of popularity.

"Most popular of all time" favors old items forever. For news, videos or shops you want what is hot now. Give each interaction a weight that shrinks with age, for example 0.5 ** (age_days / half_life), and sum the weights per item.

Python Trending score with half-life

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The "old-hit" has the most interactions overall but the lowest trending score. Tune the half-life to the domain: hours for news, weeks for movies.

Limits of popularity

  • No personalization. Every user sees the same list.
  • Rich get richer. Showing popular items makes them more popular.
  • Poor discovery. Niche items almost never appear.

Yet it is a baseline you must record. In later lessons, we compare personalized models against it.

When a new model "improves accuracy" by a tiny margin over popularity, ask whether the gain is worth the complexity. Often a better data pipeline helps more than a fancier algorithm.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is ranking items by raw average rating unreliable?

    Small samples are noisy. Without adjustment, items with very few ratings can top the list.
  2. 2. In the weighted rating formula, what does the prior strength m control?

    A larger m means items need more ratings before their own mean dominates the global mean.
  3. 3. What does a half-life of 7 days mean in a trending score?

    Weight = 0.5 ** (age / half_life), so it halves each half-life period.
  4. 4. What is the main strength of a popularity recommender?

    It needs only aggregate counts, so unknown users still get a reasonable list. It does not personalize.

Summary

  • Interaction data is a long table of (user, item, rating) rows; seed random data for reproducibility.
  • Rank by count (popularity), mean (quality), or a Bayesian weighted rating that shrinks small samples.
  • A recommender function ranks, then removes items the user already saw.
  • Time-decayed weights give a trending list.
  • Popularity is a strong, non-personalized baseline and a cold-start fallback.

Next: Explicit and Implicit Feedback