Content-Based Recommendation and Cosine Similarity
Module 6 · Adding Machine Learning

Content-Based Recommendation and Cosine Similarity

Why recommendations use machine learning, the difference between content-based and collaborative filtering, how CountVectorizer turns genres and cast into vectors, and how cosine similarity ranks movies — with a worked example and the limits of bag-of-words.

Why Machine Learning for Recommendations?

You could hand-write rules: “if they liked an action movie, show action movies.” That breaks down fast. Machine learning — systems that learn patterns from data rather than following hand-written rules — earns its place in recommendation because of:

  • Personalization — suggestions tailored to each person’s actual choices.
  • Scale — catalogs with thousands of items and many attributes are too large to reason about by hand.
  • Hidden patterns — similarities that aren’t obvious, like shared casts across different genres.
  • Improvement over time — more interaction data means better suggestions, without rewriting rules.

Two Families of Recommenders

Content-based filteringCollaborative filtering
IdeaRecommend items similar to ones you likedRecommend what similar people liked
NeedsItem attributes (genres, cast, plot)Many users’ interaction histories
StrengthWorks with no other users; explainable (“same director”)Finds surprising cross-genre picks
WeaknessStays close to what you already knowCold start — useless for new items and new users

Streaming services usually combine both in hybrid systems. The movie app uses content-based filtering: it has rich movie attributes but no history of other users’ choices.


Step 1: Turning Movies into Numbers

Algorithms compare numbers, not words. Feature extraction converts each movie into a numeric representation. The app builds a short text from each movie’s genres and cast, then uses scikit-learn’s CountVectorizer:

  1. Build a vocabulary of every distinct word (token) across all the movies.
  2. Represent each movie as a vector — a list of numbers, one position per vocabulary word — holding how many times that word appears.

This is called a bag-of-words representation: word order is thrown away, and only counts remain. The result is a matrix with one row per movie and one column per word.

Take three movies described only by genre:

MovieGenresactioncomedydramathriller
AAction, Thriller1001
BAction, Thriller, Drama1011
CComedy, Drama0110

Step 2: Measuring Closeness with Cosine Similarity

Each movie is now an arrow in a space with one dimension per word. Cosine similarity measures how closely two arrows point in the same direction — the cosine of the angle between them:

cosine(A, B) = (A · B) / (|A| × |B|)

The top is the dot product: multiply matching positions and add them up. The bottom multiplies the vectors’ lengths (magnitudes). For count vectors, the result runs from 0 (nothing in common) to 1 (identical direction).

action count →drama count ↑P (1, 1)Q (3, 3)R (3, 0.5)θWhat the angle tells youP vs Q: same direction → cosine = 1.00Q is longer, but length doesn’t matterP vs R: wider angle → cosine ≈ 0.81R is mostly action, P is balancedPerpendicular → cosine = 0no words in common at all

Working the example

  • A vs. B: dot product = 1·1 + 0 + 0 + 1·1 = 2. Lengths: √2 ≈ 1.41 and √3 ≈ 1.73. Cosine = 2 ÷ 2.45 ≈ 0.82.
  • A vs. C: no shared genres, dot product = 0. Cosine = 0.
  • B vs. C: they share only Drama. Cosine = 1 ÷ 2.45 ≈ 0.41.

So someone who picked A gets B recommended first, and C last — exactly what intuition says.

Why cosine, not raw overlap?

Cosine similarity is magnitude-independent. A movie with a 30-person cast list has a much longer vector than one with 5, but a long list isn’t more relevant — only the proportion of overlap matters. Dividing by the lengths normalizes that away. (Euclidean distance, by contrast, would treat long and short vectors as far apart even when they point the same way.)


The Full Recommendation Flow

  1. 1 Pick 3

    user selects movies

  2. 2 Fetch details

    ML service → Express

  3. 3 Vectorize

    CountVectorizer on genres + cast

  4. 4 Compare

    cosine similarity

  5. 5 Rank

    top results, minus the picks

In scikit-learn, the core is only a few lines:

vectors = CountVectorizer().fit_transform(texts)   # movies → count matrix
scores = cosine_similarity(vectors[selected], vectors)  # picks vs. all

The service averages each candidate’s similarity to the selected movies, removes the movies the user already picked, sorts, and returns the top few.

Where Bag-of-Words Falls Short

Count vectors are a strong baseline — simple, fast, and explainable — but they have clear limits:

  • No meaning, only spelling. “Sci-Fi” and “Science Fiction” share nothing; “thriller” and “suspense” look unrelated.
  • Common words dominate. A token that appears everywhere adds similarity to everything. TF-IDF (term frequency–inverse document frequency) fixes this by down-weighting common tokens.
  • No sense of context. Embeddings — learned vectors that place similar meanings close together — address that, and they return as the foundation of RAG in Module 9.
⚠️ Common Pitfall: Letting the Tokenizer Split Names and Labels

Common mistake: feeding cast names and genre labels to a default text vectorizer.

By default, CountVectorizer lower-cases text and splits on non-word characters. “Tom Hanks” and “Tom Cruise” both produce the token tom, creating a false match; “Sci-Fi” becomes sci and fi. Treat names and labels as whole tokens — for example, join them with underscores (tom_hanks) or supply a custom tokenizer — so the model compares people, not first names.

🧠 Knowledge Check

1. Using the genre vectors A = [1,0,0,1] and C = [0,1,1,0], what is their cosine similarity?

2. Why is cosine similarity preferred over raw shared-word counts for comparing movies with very different cast-list lengths?

3. A brand-new streaming service with no user history wants recommendations. Which approach can work immediately?

4. Which limitation of bag-of-words do embeddings address?


Up Next

This recommender uses a fixed formula. Module 7 trains several actual models, tracks every experiment, compares them honestly, and serves the chosen one — the discipline called MLOps.