Skip to content
DeepForgeby svx
All problems
MediumML Fundamentalsproj-026

Collaborative Filtering Prediction

Problem

Predict a missing rating with user-based collaborative filtering.

For the target user, average the ratings other users gave the item, weighted by their cosine similarity to the target: sum(sim * rating) / sum(abs(sim)) over users with a nonzero rating, excluding the target user. Return 0.0 when no other user qualifies.

Hint

Only neighbors who actually rated the item contribute to the weighted average.

Test cases

case 1
in: [[[5,0],[4,4]],[[1,0.5],[0.5,1]],0,1]
expected: 4
case 2
in: [[[3,0],[0,5]],[[1,0],[0,1]],0,1]
expected: 0
case 3
in: [[[0,0],[2,0],[0,4]],[[1,0.8,0.2],[0.8,1,0.5],[0.2,0.5,1]],0,0]
expected: 2
case 4
in: [[[4,0],[0,0]],[[1,0],[0,1]],0,1]
expected: 0
Starter codePython
def cf_predict(matrix, similarity, user, item):
    # Your code here
    pass

4 test cases · run your code against them in the editor