Baseline Predictor
Baseline Predictor
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+biwhere 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).
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
Log in to take notes on this problem
Accepts: array
Accepts: array
Baseline Predictor
Baseline Predictor
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+biwhere 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).
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
Log in to take notes on this problem
Accepts: array
Accepts: array