Predict a missing rating with item-based collaborative filtering.
The idea: a user will rate a new item the way they rated items similar to it. Two items are similar when the same users rate them in the same way.
You get a ratings matrix R of shape (n_users, n_items), where 0 means
"not rated". To predict user u's rating of item i:
i and the column
of each other item j, using the full columns (missing ratings count as 0).j != i that user u has rated.prediction = sum_j sim(i, j) * R[u, j] / sum_j |sim(i, j)|
If the denominator is 0, return 0.0. If an item column is all zeros, its
similarity to everything is 0.
def predict_rating(R, user, item):
Input
R = np.array([[5, 3, 0],
[4, 0, 4],
[1, 1, 5],
[0, 4, 4]])
predict_rating(R, user=0, item=2)
Output
3.88067791
Reasoning
User 0 rated item 0 with 5 and item 1 with 3. Item 2's similarity to item 0
is 0.4292 and to item 1 is 0.5455. The prediction is
(0.4292 * 5 + 0.5455 * 3) / (0.4292 + 0.5455) = 3.881, closer to 3 because
item 1 is more like item 2.
0 meaning missing.item, even if it exists.Check runs the visible cases against a fresh kernel. Submit runs the hidden ones too.
⇧↵run cell