Rating Normalization
Rating Normalization
Rating normalization (mean-centering) is a critical preprocessing step in collaborative filtering. Different users rate on different scales - a generous user might rate everything 4-5 stars, while a strict user rates 1-3 stars. Mean-centering removes this bias by subtracting each user's average rating from their scores.
Given a user-item rating matrix where 0 indicates an unrated item, normalize each user's ratings by subtracting that user's mean rating. Unrated entries (0) should remain 0.
Algorithm
For each user (row), compute the mean of their non-zero ratings and subtract it:
rˉu=∣Iu∣1i∈Iu∑rui,r^ui=⎩⎨⎧rui−rˉu0if rui=0otherwisewhere \mathcal{I}_u is the set of items rated by user u.
Examples
Input:
matrix = [[5, 3, 0, 1], [4, 0, 0, 1], [1, 1, 0, 5]]
Output:
[[2.0, 0.0, 0.0, -2.0], [1.5, 0.0, 0.0, -1.5], [-1.333, -1.333, 0.0, 2.667]]
User 0 rated items 0,1,3 with mean 3.0. User 1 rated items 0,3 with mean 2.5. User 2 rated items 0,1,3 with mean 7/3 ≈ 2.333. Each rating is shifted by subtracting the user's mean.
Input:
matrix = [[2, 4, 6]]
Output:
[[-2.0, 0.0, 2.0]]
Single user with mean 4.0. Ratings become 2-4=-2, 4-4=0, 6-4=2.
Hint 1
For each row, filter out zeros to get the rated values. Compute their mean. Then iterate over the row: if a value is non-zero, subtract the mean; otherwise keep it as 0.0.
Hint 2
Handle the edge case where a user has no ratings (all zeros) by keeping the entire row as zeros. Use a list comprehension: [v - mean if v != 0 else 0.0 for v in row].
Requirements
- For each user, compute the mean of their non-zero (rated) entries
- Subtract the user's mean from each of their rated entries
- Leave unrated entries (0) as 0.0
- If a user has no ratings, all entries remain 0.0
- Return the normalized matrix as a list of lists of floats
Constraints
- matrix is a non-empty list of lists (user × item)
- Values are non-negative numbers; 0 means unrated
- Return a list of lists of floats
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Rating Normalization
Rating Normalization
Rating normalization (mean-centering) is a critical preprocessing step in collaborative filtering. Different users rate on different scales - a generous user might rate everything 4-5 stars, while a strict user rates 1-3 stars. Mean-centering removes this bias by subtracting each user's average rating from their scores.
Given a user-item rating matrix where 0 indicates an unrated item, normalize each user's ratings by subtracting that user's mean rating. Unrated entries (0) should remain 0.
Algorithm
For each user (row), compute the mean of their non-zero ratings and subtract it:
rˉu=∣Iu∣1i∈Iu∑rui,r^ui=⎩⎨⎧rui−rˉu0if rui=0otherwisewhere \mathcal{I}_u is the set of items rated by user u.
Examples
Input:
matrix = [[5, 3, 0, 1], [4, 0, 0, 1], [1, 1, 0, 5]]
Output:
[[2.0, 0.0, 0.0, -2.0], [1.5, 0.0, 0.0, -1.5], [-1.333, -1.333, 0.0, 2.667]]
User 0 rated items 0,1,3 with mean 3.0. User 1 rated items 0,3 with mean 2.5. User 2 rated items 0,1,3 with mean 7/3 ≈ 2.333. Each rating is shifted by subtracting the user's mean.
Input:
matrix = [[2, 4, 6]]
Output:
[[-2.0, 0.0, 2.0]]
Single user with mean 4.0. Ratings become 2-4=-2, 4-4=0, 6-4=2.
Hint 1
For each row, filter out zeros to get the rated values. Compute their mean. Then iterate over the row: if a value is non-zero, subtract the mean; otherwise keep it as 0.0.
Hint 2
Handle the edge case where a user has no ratings (all zeros) by keeping the entire row as zeros. Use a list comprehension: [v - mean if v != 0 else 0.0 for v in row].
Requirements
- For each user, compute the mean of their non-zero (rated) entries
- Subtract the user's mean from each of their rated entries
- Leave unrated entries (0) as 0.0
- If a user has no ratings, all entries remain 0.0
- Return the normalized matrix as a list of lists of floats
Constraints
- matrix is a non-empty list of lists (user × item)
- Values are non-negative numbers; 0 means unrated
- Return a list of lists of floats
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array