TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Rating Normalization

Recommender Systems
Easy

Rating normalization removes differences in how generously or strictly users assign ratings. Given a user-item matrix where 0 represents an unrated item, subtract each user's mean observed rating from that user's rated entries. Keep every unrated entry at 0.

For each user, compute the mean of the nonzero ratings:

rˉu=1∣Iu∣∑i∈Iurui\bar{r}_u = \frac{1}{|\mathcal{I}_u|}\sum_{i \in \mathcal{I}_u}r_{ui}rˉu​=∣Iu​∣1​i∈Iu​∑​rui​

Here, u identifies a user, i identifies an item, r_{ui} is the user's rating for that item, and the rated-item set contains every item rated by user u.

Normalize each matrix entry:

r^ui={rui−rˉuif rui≠00otherwise\hat{r}_{ui} = \begin{cases} r_{ui} - \bar{r}_u & \text{if } r_{ui} \ne 0 \\ 0 & \text{otherwise} \end{cases}r^ui​={rui​−rˉu​0​if rui​=0otherwise​

Return the normalized matrix as a list of user rows rounded to six decimals for display.

Loading visualization...

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.333333, -1.333333, 0.0, 2.666667]]

Explanation: Each nonzero rating is shifted by its user’s mean, while unrated zeros remain zero.

Input: matrix = [[2, 4, 6]]

Output: [[-2.0, 0.0, 2.0]]

Hint 1

Collect the nonzero ratings from one row before computing its mean.

Hint 2

Subtract that mean only from rated entries and keep unrated entries at zero.

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
Try Similar Problems
Mean Rating ImputationAdjusted Cosine SimilarityZscore StandardizationMatrix NormalizationBaseline Predictor

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.
PrevNext

Rating Normalization

Recommender Systems
Easy

Rating normalization removes differences in how generously or strictly users assign ratings. Given a user-item matrix where 0 represents an unrated item, subtract each user's mean observed rating from that user's rated entries. Keep every unrated entry at 0.

For each user, compute the mean of the nonzero ratings:

rˉu=1∣Iu∣∑i∈Iurui\bar{r}_u = \frac{1}{|\mathcal{I}_u|}\sum_{i \in \mathcal{I}_u}r_{ui}rˉu​=∣Iu​∣1​i∈Iu​∑​rui​

Here, u identifies a user, i identifies an item, r_{ui} is the user's rating for that item, and the rated-item set contains every item rated by user u.

Normalize each matrix entry:

r^ui={rui−rˉuif rui≠00otherwise\hat{r}_{ui} = \begin{cases} r_{ui} - \bar{r}_u & \text{if } r_{ui} \ne 0 \\ 0 & \text{otherwise} \end{cases}r^ui​={rui​−rˉu​0​if rui​=0otherwise​

Return the normalized matrix as a list of user rows rounded to six decimals for display.

Loading visualization...

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.333333, -1.333333, 0.0, 2.666667]]

Explanation: Each nonzero rating is shifted by its user’s mean, while unrated zeros remain zero.

Input: matrix = [[2, 4, 6]]

Output: [[-2.0, 0.0, 2.0]]

Hint 1

Collect the nonzero ratings from one row before computing its mean.

Hint 2

Subtract that mean only from rated entries and keep unrated entries at zero.

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
Try Similar Problems
Mean Rating ImputationAdjusted Cosine SimilarityZscore StandardizationMatrix NormalizationBaseline Predictor

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.