Collaborative filtering prediction
Your task
Implement a single user-based collaborative-filtering prediction. You are given ratings, a dict mapping user id (string) to a dict of {item id (string): rating (number)}; a target user u; and an item i. Predict u's rating for i as the cosine-weighted average of the ratings other users gave to i, weighted by their cosine similarity to u (cosine over the items both users have rated; 0 if they share no items). Only include neighbors who have rated i and whose similarity to u is strictly positive. If the sum of used similarities is 0 (no positive-similarity neighbor rated i), return 0.0. Return the predicted rating rounded to 6 decimals.
Examples
predict_rating(ratings: dict, u: str, i: str) -> floatExample 1
Input
predict_rating({"u": {"a": 5, "b": 3}, "v": {"a": 4, "b": 2, "c": 5}, "w": {"a": 1, "c": 1}}, "u", "c")Output
3.091836