Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 38 ยท Content-based Filtering

A Content-based Recommender

Build a complete content-based recommender: item vectors, a user profile from liked items, cosine scoring, excluding seen items, and explaining each recommendation.

A content-based recommender answers one question: "what is similar to what this user already liked?" It needs no other users, so it works for a brand-new item the moment you have its description. This lesson assembles the whole thing: item vectors, a user profile, scoring, filtering and explanations.

You already have the pieces: TF-IDF vectors for items and cosine similarity. The new ingredient is the user profile.

The recipe

  1. Build one vector per item from its content.
  2. Build one vector per user by combining the vectors of items they liked.
  3. Score every unseen item by its cosine similarity to the user's vector.
  4. Remove items the user already has, and return the top k.
flowchart LR
    A["Item text and tags"] --> B["Item vectors"]
    C["User history"] --> D["User profile vector"]
    B --> D
    B --> E["Score = cosine"]
    D --> E
    E --> F["Remove seen, take top k"]

The profile lives in the same space as the items, so one similarity call scores everything.

A small catalog and a user

Python Items, user history, profile

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.

Averaging the liked items' vectors places the profile "between" them. Because rows are unit length, a dot product equals cosine similarity, which keeps scoring to one matrix multiplication.

Using ratings to weight the profile

A user who rated one item 5 and another 2 should not weight them equally. Two common options:

  • Likes only: use items rated at or above the user's own average.
  • Weighted average: weight each item by rating - user_mean, so dislikes push the profile away.
Python Rating-weighted profile

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.

Here the low ratings for the two romance titles pull the profile away from "paris" and "romantic", so any unseen romance item would score below zero, while the space-action titles rise to the top (item 7 first, then item 5).

Explaining recommendations

Content-based models are easy to explain, which users and product teams like. Two explanation styles:

  • Because you liked X: show the liked item with the highest similarity to the recommendation.
  • Top terms: show the words contributing most to the score.
Python Why was this recommended?

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.

Practical details

  • Seen items: always filter them out, or list them in a "watch again" shelf. Do not recommend them as new.
  • Rating threshold: for implicit feedback every interaction counts as a like; weight by dwell time or purchase value if you have it.
  • Updating: the profile is a mean, so you can update it incrementally when the user acts, without retraining anything.
  • Scale: score with a sparse matrix product for millions of items, or use an approximate nearest neighbor index.

Content-based and collaborative methods fail in different places. Content-based handles new items; collaborative finds surprising matches. Later you will combine them in a hybrid.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a user profile in content-based filtering?

    It lives in the same space as the item vectors, so one similarity computation scores all items.
  2. 2. Why is a new item easy to recommend with this method?

    Content-based scoring uses only the item's features, so a brand-new item can be scored immediately.
  3. 3. What does weighting by rating minus the user's mean achieve?

    Below-average ratings give negative weights, steering the profile away from features the user disliked.
  4. 4. Which step prevents recommending something the user already has?

    Filter the user's known items out of the candidate list.

Summary

  • Build item vectors, average liked items into a profile, score by cosine, drop seen items.
  • Weighting by rating minus the user's mean lets dislikes shape the profile.
  • Explanations come almost for free: the most similar liked item and the shared terms.
  • It handles new items but only recommends more of the same; see the limits next.

Next: User Profiles and Feature Weighting