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.
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.
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.
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.
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].
Sign in to take notes on this problem
Accepts: array
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.
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.
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.
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.
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].
Sign in to take notes on this problem
Accepts: array