Chuyển đến nội dung

Bài 14 · Collaborative Filtering

User-based Collaborative Filtering

Build user-based collaborative filtering from scratch: find similar users, predict ratings with a mean-centered weighted average, evaluate with RMSE and make top-N lists.

Collaborative filtering (CF) recommends using the behavior of many users and needs no item content at all. The user-based version rests on a human idea: people who agreed in the past will probably agree in the future. To predict what Maya will think of a film, find users whose ratings resemble Maya's and look at what they said.

This lesson builds user-based CF from scratch with NumPy, using the similarity measures you already know. You will predict held-out ratings, measure the error, and turn predictions into a top-N list.

The algorithm

To predict user u's rating for item i:

  1. Compute the similarity between u and every other user (over co-rated items).
  2. Keep the k nearest neighbors who actually rated item i and have positive similarity.
  3. Predict a weighted average of their ratings, mean-centered so generous and harsh raters are comparable:

r̂(u, i) = mean(u) + Σ sim(u, v) · (r(v, i) - mean(v)) / Σ |sim(u, v)|

flowchart LR
    A["Target user u"] --> B["Similarity to all users"]
    B --> C["Keep k nearest who rated item i"]
    C --> D["Weighted, mean-centered average"]
    D --> E["Predicted rating"]

Subtracting each neighbor's mean converts their rating into "how much above or below their usual".

A synthetic dataset with a hold-out

We create ratings from hidden tastes so a good method can actually find structure, hide 10% of the known ratings, and keep them for testing. The same recipe is reused in the next lessons.

Python Data and a global-mean baseline

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The prediction function

Python User-based k-NN predictions

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Compare against two baselines: always predicting the global mean, and predicting each user's own mean. If user-based CF cannot beat the user mean, the neighbors are adding only noise. In this run k-NN reaches an RMSE of about 0.83, against 0.88 for the global mean and 0.96 for the user mean. The parameter k controls the bias-variance trade-off: too small and a few odd neighbors dominate, too large and you average in people who are not really similar (see shrinkage and tuning).

Top-N recommendations

Rating prediction is rarely the end goal; you show a list. Predict a score for every item the user has not rated, sort, and take the top N.

Python A top-5 list for one user

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Strengths and weaknesses

  • Strengths: simple, no training, serendipitous (neighbors can suggest unexpected items), easy to explain ("people like you rated this high").
  • Weaknesses: the similarity matrix is users × users, which becomes huge; it needs overlapping ratings, so sparse data hurts; users change, so neighbors go stale; and it favors popular items.

User-based CF recipe

Find neighbors Similar rating patterns
Keep top k Only those who rated it
Weighted average Center by each user's mean

Predict, rank unseen items, show the top N.

1 / 2

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Why mean-center neighbors' ratings before averaging?

    A 4 from a harsh rater can mean more than a 4 from a generous one; deviations from each user's mean put them on one scale.
  2. 2. What happens if k is very large?

    More neighbors reduce variance but include weakly related users, so predictions flatten.
  3. 3. Which baselines should user-based CF beat?

    If you cannot beat simple means, the neighbor information is not adding value.
  4. 4. Why is user-based CF hard to scale to millions of users?

    Pairwise similarities between all users are expensive to compute and store.

Summary

  • User-based CF predicts a rating from the mean-centered ratings of the k most similar users who rated the item.
  • Evaluate against global-mean and user-mean baselines on held-out ratings.
  • Turn predictions into a ranked list of unseen items for top-N recommendation.
  • It is simple and explainable but costly at scale and weak on sparse data.

Next: Item-based Collaborative Filtering

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.