User-Based CF Prediction
User-Based CF Prediction
User-based collaborative filtering predicts a target user's rating for an item by computing a weighted average of ratings from similar users. The intuition is that users who agreed in the past will agree in the future. Only users with positive similarity are considered, and their ratings are weighted by how similar they are to the target user.
Given a list of similarity scores and corresponding ratings from neighbor users who have rated the target item, compute the predicted rating.
Algorithm
Filter to users with positive similarity, then compute the weighted average:
r^=∑u:su>0su∑u:su>0su⋅ruIf no user has positive similarity, return 0.0.
Examples
Input:
similarities = [0.9, 0.8, 0.3], ratings = [4, 5, 2]
Output:
3.95
All similarities are positive. Weighted average: (0.9×4 + 0.8×5 + 0.3×2) / (0.9 + 0.8 + 0.3) = 7.9 / 2.0 = 3.95.
Input:
similarities = [0.8, -0.2, 0.6], ratings = [5, 1, 3]
Output:
4.143
User with similarity -0.2 is excluded. Weighted average: (0.8×5 + 0.6×3) / (0.8 + 0.6) = 5.8 / 1.4 ≈ 4.143.
Hint 1
Loop through similarities and ratings together (zip them). For each pair where similarity > 0, add similarity × rating to the numerator and similarity to the denominator. Divide at the end.
Hint 2
Initialize num = 0 and den = 0. For each (sim, rating) pair: if sim > 0, num += sim * rating and den += sim. If den is 0 after the loop, return 0.0. Otherwise return num / den.
Requirements
- Only include users whose similarity is strictly greater than 0
- Compute the predicted rating as the similarity-weighted average of their ratings
- Return 0.0 if no user has positive similarity
- Return the prediction as a float
Constraints
- similarities and ratings have the same length (at least 1)
- Similarities can be negative, zero, or positive
- Ratings are positive numbers
- Return a float
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Accepts: array
User-Based CF Prediction
User-Based CF Prediction
User-based collaborative filtering predicts a target user's rating for an item by computing a weighted average of ratings from similar users. The intuition is that users who agreed in the past will agree in the future. Only users with positive similarity are considered, and their ratings are weighted by how similar they are to the target user.
Given a list of similarity scores and corresponding ratings from neighbor users who have rated the target item, compute the predicted rating.
Algorithm
Filter to users with positive similarity, then compute the weighted average:
r^=∑u:su>0su∑u:su>0su⋅ruIf no user has positive similarity, return 0.0.
Examples
Input:
similarities = [0.9, 0.8, 0.3], ratings = [4, 5, 2]
Output:
3.95
All similarities are positive. Weighted average: (0.9×4 + 0.8×5 + 0.3×2) / (0.9 + 0.8 + 0.3) = 7.9 / 2.0 = 3.95.
Input:
similarities = [0.8, -0.2, 0.6], ratings = [5, 1, 3]
Output:
4.143
User with similarity -0.2 is excluded. Weighted average: (0.8×5 + 0.6×3) / (0.8 + 0.6) = 5.8 / 1.4 ≈ 4.143.
Hint 1
Loop through similarities and ratings together (zip them). For each pair where similarity > 0, add similarity × rating to the numerator and similarity to the denominator. Divide at the end.
Hint 2
Initialize num = 0 and den = 0. For each (sim, rating) pair: if sim > 0, num += sim * rating and den += sim. If den is 0 after the loop, return 0.0. Otherwise return num / den.
Requirements
- Only include users whose similarity is strictly greater than 0
- Compute the predicted rating as the similarity-weighted average of their ratings
- Return 0.0 if no user has positive similarity
- Return the prediction as a float
Constraints
- similarities and ratings have the same length (at least 1)
- Similarities can be negative, zero, or positive
- Ratings are positive numbers
- Return a float
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Accepts: array