Item-Based CF Prediction
Item-Based CF Prediction
Item-based collaborative filtering predicts a user's rating for an unrated item by looking at how the user rated similar items. The prediction is a weighted average of the user's existing ratings, where the weights are the similarities between the target item and each rated item. This approach is the backbone of Amazon's "customers who bought this also bought" recommendations.
Given a user's ratings for all items (0 means unrated), a vector of similarities between the target item and every other item, and the target item index, predict the rating.
Algorithm
r^=∑i=t,si>0si∑i=t,si>0si⋅riwhere t is the target item, r_i is the user's rating for item i, and s_i is the similarity between item i and the target. Only items with positive similarity and non-zero rating contribute. Return 0.0 if no items qualify.
Examples
Input:
user_ratings = [5, 0, 3, 0, 4], item_similarities = [0.8, 0.0, 0.9, 0.1, 0.5], target = 1
Output:
3.9545
Skip target=1. Item 0: sim=0.8, rating=5. Item 2: sim=0.9, rating=3. Item 3: rating=0 (skip). Item 4: sim=0.5, rating=4. Prediction = (0.85 + 0.93 + 0.5*4) / (0.8+0.9+0.5) = 8.7/2.2 = 3.9545.
Input:
user_ratings = [2, 4, 6], item_similarities = [0.5, 0.0, 0.5], target = 1
Output:
4.0
Items 0 and 2 both have sim=0.5. Prediction = (0.52 + 0.56) / (0.5+0.5) = 4.0/1.0 = 4.0. Equal similarities produce a simple average.
Hint 1
Loop through all items. Skip the target index, skip items with rating 0, and skip items with non-positive similarity. For qualifying items, accumulate sim * rating in the numerator and sim in the denominator.
Hint 2
After the loop, if the denominator is 0 (no qualifying items), return 0.0. Otherwise return numerator / denominator.
Requirements
- Skip the target item itself when computing the prediction
- Only consider items with positive similarity and non-zero rating
- Compute the similarity-weighted average of qualifying ratings
- Return 0.0 if no items qualify
Constraints
- user_ratings and item_similarities have the same length
- 0 in user_ratings means unrated
- target is a valid index
- Return a float
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Accepts: array
Accepts: number
Item-Based CF Prediction
Item-Based CF Prediction
Item-based collaborative filtering predicts a user's rating for an unrated item by looking at how the user rated similar items. The prediction is a weighted average of the user's existing ratings, where the weights are the similarities between the target item and each rated item. This approach is the backbone of Amazon's "customers who bought this also bought" recommendations.
Given a user's ratings for all items (0 means unrated), a vector of similarities between the target item and every other item, and the target item index, predict the rating.
Algorithm
r^=∑i=t,si>0si∑i=t,si>0si⋅riwhere t is the target item, r_i is the user's rating for item i, and s_i is the similarity between item i and the target. Only items with positive similarity and non-zero rating contribute. Return 0.0 if no items qualify.
Examples
Input:
user_ratings = [5, 0, 3, 0, 4], item_similarities = [0.8, 0.0, 0.9, 0.1, 0.5], target = 1
Output:
3.9545
Skip target=1. Item 0: sim=0.8, rating=5. Item 2: sim=0.9, rating=3. Item 3: rating=0 (skip). Item 4: sim=0.5, rating=4. Prediction = (0.85 + 0.93 + 0.5*4) / (0.8+0.9+0.5) = 8.7/2.2 = 3.9545.
Input:
user_ratings = [2, 4, 6], item_similarities = [0.5, 0.0, 0.5], target = 1
Output:
4.0
Items 0 and 2 both have sim=0.5. Prediction = (0.52 + 0.56) / (0.5+0.5) = 4.0/1.0 = 4.0. Equal similarities produce a simple average.
Hint 1
Loop through all items. Skip the target index, skip items with rating 0, and skip items with non-positive similarity. For qualifying items, accumulate sim * rating in the numerator and sim in the denominator.
Hint 2
After the loop, if the denominator is 0 (no qualifying items), return 0.0. Otherwise return numerator / denominator.
Requirements
- Skip the target item itself when computing the prediction
- Only consider items with positive similarity and non-zero rating
- Compute the similarity-weighted average of qualifying ratings
- Return 0.0 if no items qualify
Constraints
- user_ratings and item_similarities have the same length
- 0 in user_ratings means unrated
- target is a valid index
- Return a float
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Accepts: array
Accepts: number