MediumRecommender Systems

Mean Rating Imputation

Recommender Systems · Data Processing

Medium

Problem

A user-item rating matrix is often sparse, with zero representing a missing rating. Mean imputation fills each missing entry using observed ratings from either the same user or the same item.

In user mode, replace each zero in a row with the mean of that row's nonzero ratings. In item mode, replace each zero in a column with the mean of that column's nonzero ratings. If a user or item has no observed ratings, use 0.0 as its mean. Return a new matrix and leave ratings_matrix unchanged.

Theory

Mean rating imputation is a technique used in recommender systems to fill in missing entries of a user-item rating matrix. When a user has not rated an item, we estimate what their rating might be based on available information - typically the mean rating given by that user, the mean rating received by that item, or some combination.


The Sparsity Problem in Recommender Systems

Rating matrices are extremely sparse:

Why so sparse?

Consequences:


Types of Mean Imputation

Global Mean

Replace all missing values with the average of all known ratings in the matrix:

\hat{r}_{ui} = \mu = \frac{1}{|\Omega|} \sum_{(u,i) \in \Omega} r_{ui}

Where \Omega is the set of observed ratings.

Pros: Simple, single value to compute Cons: Ignores user and item differences completely


User Mean

Replace missing values with the average rating given by that user:

\hat{r}_{ui} = \bar{r}_u = \frac{1}{|I_u|} \sum_{i \in I_u} r_{ui}

Where I_u is the set of items rated by user u.

Intuition: Some users are "harsh critics" (low average), others are "easy graders" (high average). User mean captures this tendency.

Pros: Captures user-specific rating scale Cons: Ignores item-specific quality


Item Mean

Replace missing values with the average rating received by that item:

\hat{r}_{ui} = \bar{r}_i = \frac{1}{|U_i|} \sum_{u \in U_i} r_{ui}

Where U_i is the set of users who rated item i.

Intuition: Some items are universally loved (high average), others are generally disliked (low average).

Pros: Captures item quality Cons: Ignores user preferences


Combined Imputation (User + Item Mean)

A more sophisticated approach combines user and item effects:

\hat{r}_{ui} = \mu + b_u + b_i

Where:

This baseline model accounts for both user leniency and item quality.


Worked Example: User Mean Imputation

Rating matrix (? indicates missing):

Step 1 - Calculate user means:

\bar{r}_{Alice} = \frac{4 + 5}{2} = 4.5

\bar{r}_{Bob} = \frac{2 + 3}{2} = 2.5

\bar{r}_{Carol} = \frac{4 + 5}{2} = 4.5

Step 2 - Impute missing values:

Interpretation: Alice and Carol are imputed with higher values because they tend to give higher ratings.


Worked Example: Item Mean Imputation

Same rating matrix:

Step 1 - Calculate item means:

\bar{r}_{Movie1} = \frac{4 + 2}{2} = 3.0

\bar{r}_{Movie2} = \frac{5 + 4}{2} = 4.5

\bar{r}_{Movie3} = \frac{3}{1} = 3.0

\bar{r}_{Movie4} = \frac{5}{1} = 5.0

Step 2 - Impute missing values:

Interpretation: Movie4 gets high imputed values because it received a high rating from Carol.


Handling Cold Start

New user (no ratings): Cannot compute user mean

New item (no ratings): Cannot compute item mean

Combined approach: When user or item mean is unavailable, use the available component plus global mean.


Bias Correction

Raw imputation can introduce systematic errors:

User mean bias: If users who rated an item are not representative, user mean imputation inherits their biases.

Item mean bias: If items a user rated are not representative, item mean imputation misses their true preferences.

Regularization: Add a shrinkage term that pulls estimates toward the global mean when few ratings are available:

\hat{r}_{ui} = \frac{n_u \cdot \bar{r}_u + \lambda \cdot \mu}{n_u + \lambda}

Where n_u is the number of ratings by user u and \lambda controls regularization strength.


Imputation vs Prediction

Imputation purpose: Fill the matrix for downstream algorithms that need complete data (e.g., SVD, PCA).

Prediction purpose: Estimate what rating a user would give to recommend items.

Mean imputation is often a preprocessing step, not the final recommendation model. More sophisticated methods (matrix factorization, collaborative filtering) build on imputed baselines.


Evaluation Considerations

Do not evaluate on imputed values: If you impute with user mean, predicting user mean will have zero error by construction.

Hold-out evaluation: Split observed ratings into train/test. Impute only training ratings. Evaluate on held-out test ratings.

Imputation affects model training: The quality of imputation impacts collaborative filtering algorithms that use the filled matrix.


Where Mean Rating Imputation Shows Up

Examples

Example 1

Input
ratings_matrix = [[5, 3, 0], [4, 0, 2], [0, 1, 5]], mode = "user"
Output
[[5, 3, 4.0], [4, 3.0, 2], [3.0, 1, 5]]
Explanation
Each zero is replaced by the mean of the nonzero ratings in its row.

Example 2

Input
ratings_matrix = [[5, 3, 0], [4, 0, 2], [0, 1, 5]], mode = "item"
Output
[[5, 3, 3.5], [4, 2.0, 2], [4.5, 1, 5]]

Hints

  1. For user mode, compute one mean from each row before replacing its zeros.
  2. For item mode, compute all column means before filling the copied matrix.

Requirements

Constraints

Starter Code

def mean_rating_imputation(ratings_matrix: list, mode: str) -> list:
    """
    Returns a copy with missing ratings replaced by user or item means.
    """
    # Write code here
    pass

Test Cases

CaseMatches
User meanpublic
Item meanpublic