Baseline Rating: Global Mean Plus User and Item Biases

Recommender SystemsMedium+15 XP+15

Baseline Rating: Global Mean Plus User and Item Biases

Before any collaborative filtering, you can predict a surprising amount from three numbers: how high ratings run in general, whether this user is generous, and whether this item is well liked.

r^ui=μ+bu+bi\hat{r}_{ui} = \mu + b_u + b_ir^ui​=μ+bu​+bi​

where μ is the global mean rating, and the two biases are each a deviation from it:

  • b_u = (this user's mean rating) − μ
  • b_i = (this item's mean rating) − μ

Task: write baseline_predict(ratings, user, item) returning the prediction, rounded to 4 decimal places.

  • ratings is a list of rows, one per user, one column per item, with None for unrated.
  • μ is the mean over every known rating in the matrix.
  • The user's mean is over the items that user rated; the item's mean is over the users who rated that item. Skip the Nones in both.
  • The target user has rated at least one item and the target item has at least one rating, so both means exist.

The subtractions are what makes this additive. Writing it as (user mean) + (item mean) would count the global level twice and give predictions roughly double what they should be — expressing each part as a deviation is what lets you add them to one shared baseline.

This is the model that wins more of the Netflix Prize than people expect. A generous user rating a well-liked film genuinely does rate it high, and nothing about their particular taste was needed to know that. Which is why serious systems predict the baseline first and apply collaborative filtering to the residual — leaving the hard model to explain only what the easy one couldn't.

Examples

ratings = [[5, 3, None], [4, None, 2], [None, 2, 3]]
user = 0
item = 2
returns 3.3333
ratings = [[5, None], [1, 2]]
user = 0
item = 1
returns 4.3333

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run