Content-Based Filtering
The first personalized recommender — if you liked an item, you will like items that look like it. Content-based filtering lives entirely in the ICM: normalise the rows, build an item–item similarity matrix, sparsify it with KNN, and score new items by a similarity-weighted sum of your own ratings.
01 · Similar in, similar out
The content-based idea
Compare items by their attributes — and recommend the neighbours of what you liked.
The principle is disarmingly simple: a user who expressed a preference for an item is likely to like similar items. It needs only one ingredient — a way to measure how similar two items are by their content. That content is the ICM from Chapter 1: rows are items, columns are attributes, if item has attribute . Content-based filtering never looks at other users — it reads only the target user’s own ratings plus the ICM.
New items are not a problem
A brand-new movie has no ratings, so collaborative methods (Ch. 5) are blind to it — but it does have attributes the moment it is added. Content-based filtering can recommend it on day one. The flip side: it keeps recommending more of the same — the low-diversity filter bubble from Chapter 3.
02 · Counting shared attributes
Measuring item-item similarity
Start with the dot product; normalise it into a cosine.
Take two item rows of the ICM. Their dot product counts the attributes they share — large means very similar. But the dot product is unbounded: an item with many attributes scores high against everything. The fix is the cosine similarity, which divides by the vector lengths to land in .
Similarity explorer · two movies, one cosine
The dot product is the count of shared attributes; the cosine divides it by the product of the two norms. Raising the shrink term t demotes pairs with few attributes.
Formal Definition 4.1 — cosine & shrink
For item attribute rows :
03 · From pairs to a matrix
The similarity matrix & K-nearest-neighbours
Compute every pair once, then throw most of it away.
Doing this for every pair gives the similarity matrix (items × items). With cosine it is symmetric, and its diagonal is removed — an item is trivially identical to itself, which would swamp everything. Two problems remain: is dense (expensive), and its many small values are mostly noise. The cure is K-nearest-neighbours: in each row, keep only the largest similarities and zero the rest. The matrix becomes sparse, fast, and cleaner — though no longer symmetric, since can be in ‘s top-K without the reverse.
Similarity matrix · slide K to sparsify
The cosine similarity of every movie pair. Slide K to keep only each row's top-K neighbours — too small starves the recommender, too large lets noisy weak similarities back in.
K is a hyper-parameter you must tune
Too small ⇒ not enough data to estimate a score (unstable quality). Too big ⇒ weak, noisy similarities leak back in. The relationship between and quality is an inverted-U, so you find it by evaluating (Chapter 3), not by guessing.
04 · Turning similarity into a ranking
From similarity to recommendations
One matrix multiply: your ratings, weighted by item similarity.
To score item for user , take a similarity-weighted sum of the user’s own ratings: items the user liked that are similar to pull its score up. In matrix form this is the whole model: . For the Top-N task we rank by that score; for rating prediction we normalise by the total similarity so the result stays on the rating scale.
Content-based recommender · scores & explanations
Only unseen items are scored, by a similarity-weighted sum of the user's own ratings. Each row names the rated item that contributed most — the recommendation explains itself.
Alice rated: TopGun (5), MI (4), Inter (5), Martian (4), Aveng (4)
Formal Definition 4.2 — scoring
With the row-normalised ICM, (zero diagonal, optionally KNN-sparsified) and computes every user’s scores at once.
05 · Not all attributes are equal
Improving the ICM: weights & TF-IDF
A binary ICM treats “Romance” and “released in 2012” as equally meaningful. They’re not.
So far the ICM is binary, so every attribute counts the same. But rare, specific attributes are more informative than common ones. You could assign weights by hand, or let TF-IDF do it automatically: .
- Term Frequency — if an item has many attributes, each weighs less.
- Inverse Document Frequency — an attribute shared by every item has ; a rare one scores high.
On our 8-movie catalog, the most discriminative attributes are the ones only one film has — Thriller and Musical — with ; the most common (Action, SciFi, Romance, each in three films) score lowest at .
Why it helps
Re-weighting the ICM by TF-IDF before computing cosine means two films sharing a rare attribute (both Musicals) are judged far more similar than two sharing a common one (both Action). The similarity matrix becomes much more discriminating — for free, with no manual tuning.
06 · Exam intel
What the exam tests
Compute a cosine (with and without a shrink term) from two attribute vectors; explain KNN and the role of ; compute an IDF; and state what CB reads (ICM) vs what CF reads (URM).
Worked question — cosine and shrunk similarity
Item has attributes {Action, Adventure, Cruise}; item has {Action, Thriller, Cruise}.
Compute the cosine, then the shrunk similarity with .
- Dot = 2 (shared: Action, Cruise). Norms .
- Cosine .
- Shrunk — the shrink term halves a 3-attribute pair’s similarity.
Traps: confusing CB (similarity from the ICM) with CF (similarity from the URM); forgetting to remove the diagonal of , or assuming a KNN matrix stays symmetric; believing larger is always better.
07 · Exam · past papers
Past-paper questions
Past paper AT-Sample · 5 pts — a learned content-based model (matrix + loss)
Q. Describe a purely content-based model with machine learning: write it in matrix and summation notation with a loss function; the underlying idea; the constraints and why; and the predicted-ratings equation.
Model answer. Idea. Recommend items whose content is similar to what the user liked, but learn the item-item weight matrix from data (its support restricted to content-similar pairs, from the ICM — that is what keeps it content-based). Model. ; . Loss. (reconstruct the URM from itself through learned similarities, with L2+L1 regularisation). Constraints. — otherwise the trivial reconstructs perfectly but generalises nothing (an item predicting itself = leakage); for interpretability, sparsity for efficiency. This is exactly the SLIM idea of Chapter 6, restricted to content-similar pairs.
Past paper Practice Exam 3 · CBF idea, TF-IDF, model + loss
Q. The main idea behind CBF and how it recommends; how TF-IDF weights attributes; and the CBF model in matrix notation with its loss.
Model answer. CBF builds an item-item similarity from item attributes (the ICM ), then recommends items similar to those the user liked — a similarity-weighted sum of the user’s own ratings. It needs no other users, so it handles new items and is explainable. TF-IDF: weight each attribute by with ; an attribute carried by every item has , rare ones score high. Model & loss. With the TF-IDF-weighted, row-normalised ICM, (zero diagonal, KNN-sparsified) and ; the learned variant minimises with .
08 · Self-check
Three questions before you move on
Content-based item similarity is computed from:
Why keep only the top-K similarities per row (KNN) instead of the full similarity matrix?
An attribute that appears in every item in the catalogue has a TF-IDF inverse-document-frequency of:
09 · Recap
One-screen summary
Chapter 04 — load-bearing ideas
- CB reads the ICM: item-to-item similarity from shared attributes, via cosine (with an optional shrink term).
- Build , then sparsify with KNN: keep each item’s top-K neighbours; is a tuned hyper-parameter (inverted-U quality).
- Score = : a similarity-weighted sum of the user’s own ratings. Explainable, handles new items — but low diversity.
- TF-IDF sharpens attribute weights: kills ubiquitous attributes.