Problems
Loading...
1 / 1

Baseline Predictor

Recommender Systems
Hard

The baseline predictor is the starting point for collaborative filtering. It captures the global average rating, how much each user deviates from the average (user bias), and how much each item deviates from the average (item bias). In the Netflix Prize competition, this simple model alone explained a large portion of rating variance before any collaborative filtering was applied.

Given a ratings matrix (0 means unrated) and a list of (user, item) pairs, compute the baseline prediction for each pair.

Algorithm

bu=ruμbi=riμr^ui=μ+bu+bib_u = \overline{r_u} - \mu \\ b_i = \overline{r_i} - \mu \\ \hat{r}_{ui} = \mu + b_u + b_i

where mu is the global mean of all non-zero ratings, b_u is the user bias (user's mean rating minus mu), and b_i is the item bias (item's mean rating minus mu).

Loading visualization...

Examples

Input:

matrix = [[5, 3, 0], [4, 0, 1], [0, 1, 5]], pairs = [[0, 2], [2, 0]]

Output:

[3.833, 4.333]

Global mean mu = (5+3+4+1+1+5)/6 = 3.167. User 0 bias = (5+3)/2 - 3.167 = 0.833. Item 2 bias = (1+5)/2 - 3.167 = -0.167. Prediction for (0,2) = 3.167 + 0.833 + (-0.167) = 3.833.

Input:

matrix = [[5, 0], [0, 3]], pairs = [[0, 1], [1, 0]]

Output:

[4.0, 4.0]

mu = 4.0. User 0 bias = 5-4=1, user 1 bias = 3-4=-1. Item 0 bias = 5-4=1, item 1 bias = 3-4=-1. Both predictions: 4+1+(-1) = 4+(-1)+1 = 4.0.

Hint 1

First collect all non-zero ratings to compute mu. Then for each user, average their non-zero ratings and subtract mu. Same for each item (average the non-zero entries in that column and subtract mu).

Hint 2

Be careful to skip zeros (unrated entries) in all mean calculations. For the final prediction, simply add: mu + user_biases[u] + item_biases[i].

Requirements

  • Compute the global mean from all non-zero ratings
  • Compute each user's bias as their mean rating minus the global mean
  • Compute each item's bias as its mean rating minus the global mean
  • For each (user, item) pair, return mu + user_bias + item_bias
  • Return a list of floats

Constraints

  • Matrix has at least one non-zero rating
  • 0 means unrated (do not include in mean calculations)
  • Return a list of floats
  • Time limit: 300 ms
Try Similar Problems