You open Netflix. The perfect show is already on your home screen.
You open YouTube. A video you've never heard of is exactly what you wanted.
You open Spotify. The "Discover Weekly" playlist feels like it read your mind.
This isn't coincidence. It's recommendation systems โ AI that knows what you want before you do.
And they're everywhere. 35% of Amazon purchases. 70% of Netflix viewing time. 40% of YouTube watch time. All from recommendations.
Let's understand how they work.
The Goal: Predict Preference
At its core, recommendation is a prediction problem:
Given: User U, Item I, Context C
Predict: How much will U like I in context C?
The answer is a score. Rank all items by score. Show the top N.
Approach 1: Collaborative Filtering
The Insight: People who agreed in the past will agree in the future.
User-User Collaborative Filtering
You liked: {Inception, Interstellar, The Matrix}
User X liked: {Inception, Interstellar, The Matrix, Tenet}
โ
Similar taste!Recommend: Tenet
python
from sklearn.metrics.pairwise import cosine_similarity
User-item matrix (1 = liked, 0 = not seen)
user_item_matrix = [
[1, 1, 1, 0], # You
[1, 1, 1, 1], # User X
[1, 0, 1, 0], # User Y]
Find similar users
similarities = cosine_similarity([user_item_matrix[0]], user_item_matrix[1:])
similarities = [0.87, 0.82] - User X is most similarItem-Item Collaborative Filtering
People who bought X also bought Y.
Bought phone case โ Also bought screen protector
Watched Inception โ Also watched Interstellar
More scalable than user-user. Items are fewer than users. Item relationships are more stable.
The Cold Start Problem
Collaborative filtering fails for new users and new items:
New user: No history to find similar users
New item: No interactions to find similar items
Solutions:
Ask preferences during onboarding
Use content-based features for new items
Default to popular items temporarily
๐ง
Approach 2: Content-Based Filtering
The Insight: Recommend items similar to what you liked before.
Feature Extraction
For movies:
python
movie_features = {
"genre": ["sci-fi", "action"],
"director": "Christopher Nolan",
"actors": ["Leonardo DiCaprio"],
"year": 2010,
"keywords": ["dream", "subconscious", "heist"]}
For products:
python
product_features = {
"category": "electronics",
"brand": "Sony",
"price": 299.99,
"attributes": ["wireless", "noise-cancelling", "bluetooth"]}
Similarity Scoring
python
from sklearn.feature_extraction import DictVectorizer
from sklearn.metrics.pairwise import cosine_similarity
Vectorize features
vectorizer = DictVectorizer()
movie_vectors = vectorizer.fit_transform([movie1_features, movie2_features])Calculate similarity
similarity = cosine_similarity(movie_vectors[0:1], movie_vectors[1:2])High similarity = recommend
๐
Approach 3: Embeddings (The Modern Way)
See Episode 15 for how embeddings work.
User and Item Embeddings
Represent users and items in the same vector space:
User embedding: [0.2, -0.5, 0.8, ...] # 128-512 dimensions
Movie embedding: [0.3, -0.4, 0.7, ...]
Similarity = dot_product(user_emb, movie_emb)Learning Embeddings
Matrix Factorization:
python
import numpy as np
Decompose user-item matrix into user and item embeddings
R โ U ร I^T
R: user-item matrix (n_users ร n_items)
U: user embeddings (n_users ร n_factors)
I: item embeddings (n_items ร n_factors)
from sklearn.decomposition import TruncatedSVD
svd = TruncatedSVD(n_components=50)
user_embeddings = svd.fit_transform(user_item_matrix)
item_embeddings = svd.components_.TPredict rating
predicted = np.dot(user_embeddings[user_id], item_embeddings[item_id])Neural Collaborative Filtering:
python
import torch.nn as nn
class NCF(nn.Module):
def __init__(self, n_users, n_items, embedding_dim):
super().__init__()
self.user_embedding = nn.Embedding(n_users, embedding_dim)
self.item_embedding = nn.Embedding(n_items, embedding_dim)
self.fc = nn.Sequential(
nn.Linear(embedding_dim * 2, 128),
nn.ReLU(),
nn.Linear(128, 1),
nn.Sigmoid()
)def forward(self, user_ids, item_ids):
user_emb = self.user_embedding(user_ids)
item_emb = self.item_embedding(item_ids)
concat = torch.cat([user_emb, item_emb], dim=1)
return self.fc(concat)Two-Tower Architecture (Production Scale)
Used by YouTube, Netflix, and everyone at scale:
User Features โ User Tower โ User Embedding (64-256 dims)
โ
Dot Product โ Score
โ
Item Features โ Item Tower โ Item Embedding (64-256 dims)Why Two Towers?
Pre-compute item embeddings (millions of items)
At serving time, just encode user + dot product
Use approximate nearest neighbors (ANN) for fast retrieval (see Episode 17)
Sequence-Aware Recommendations
Your recent history matters more than your history from 5 years ago.
Session-Based Recommendations
python
Transformer-based session model
class SessionRec(nn.Module):
def __init__(self, n_items, d_model=128):
super().__init__()
self.embedding = nn.Embedding(n_items, d_model)
self.transformer = nn.TransformerEncoder(
nn.TransformerEncoderLayer(d_model, nhead=4),
num_layers=2
)
self.output = nn.Linear(d_model, n_items)def forward(self, session_items):
# session_items: [batch, seq_len] - item IDs in sequence
emb = self.embedding(session_items)
encoded = self.transformer(emb)
# Predict next item
return self.output(encoded[:, -1, :])๐ฌ
Multi-Objective Optimization
Real systems optimize for more than just clicks:
Objective Why It Matters
Click-through rate Immediate engagement
| Watch time | Quality of engagement (YouTube) |
|---|---|
| Conversion | Purchase (Amazon) |
| Retention | Come back tomorrow |
| Diversity | Don't show the same thing repeatedly |
| Freshness | Surface new content |
Pareto Frontier
There's a tradeoff between exploration (new items) and exploitation (known preferences). Too much exploration: users bounce. Too much exploitation: echo chamber.
๐ก๏ธ
The Netflix Example
Netflix's recommendation pipeline:
- Candidate Generation (1000s of titles)
- Collaborative filtering
- Content-based filtering
- Popularity
- Ranking (100s of titles)
- Gradient boosted decision trees
- Neural networks
- Multi-objective: rating + watch time
- Re-ranking (Top 10)
- Diversity constraints
- Page composition
- A/B testing
๐ฆ
Evaluation Metrics
Metric Measures Good For
| Precision@K | Of top K, how many relevant? | Quality of recommendations |
|---|---|---|
| Recall@K | Of all relevant, how many in top K? | Coverage |
| NDCG | Ranking quality (higher = better position) | Order matters |
| MAP | Mean average precision | Overall performance |
| Diversity | How different are recommendations? | Avoid filter bubbles |
| Coverage | % of catalog recommended | Long-tail items |
๐
Practical Takeaways
Collaborative filtering = wisdom of crowds โ Users similar to you liked this
Content-based = match features โ Items similar to what you likedEmbeddings unify both โ Same vector space for users and items
Two-tower architectures scale โ Pre-compute items, fast serving
Sequence matters โ Recent behavior > old behavior
Multiple objectives โ CTR, watch time, retention, diversity
Cold start is hard โ New users and items need special handling
๐
What's Next?
Episode 98: Building Your Own AI SaaS โ The grand finale. Full architecture walkthrough: auth, billing, model serving, scaling. Everything we've learned, tied together.
โ Previous
Ep 96: How Voice Assistants Work
Next โ
Ep 98: Building Your Own AI SaaS
Next: Episode 98 โ Building Your Own AI SaaS
You've made it. 98 episodes. Now let's put it all together and build a production AI SaaS from scratch.
This is part of a 98-episode series covering AI engineering from tokens to production deployment.