Sub topic 3
Latent Factors and Matrix Factorization
Assume you know NumPy arrays, dot products, averages, and basic statistics. Matrix factorization represents each user and item with a small vector called a latent factor. A latent factor is a learned number that captures a hidden preference, such as interest in science fiction.
Matrix Factorization
A user-item matrix contains ratings or interactions. Factorization approximates this matrix as two smaller matrices:
\[ R \approx P Q^T \]
P contains user vectors. Q contains item vectors. Their dot product estimates a user’s preference for an item. Unlike neighborhood methods, this model can discover similarities without storing every user-to-user comparison.
import numpy as np
R = np.array([[5, 3, 0], [4, 0, 1], [1, 1, 5]], dtype=float)
rng = np.random.default_rng(0)
P = rng.normal(0, 0.1, (3, 2))
Q = rng.normal(0, 0.1, (3, 2))
for _ in range(200):
for u, i in zip(*np.nonzero(R)):
error = R[u, i] - P[u] @ Q[i]
pu = P[u].copy()
P[u] += 0.01 * error * Q[i]
Q[i] += 0.01 * error * pu
print(P @ Q.T)Training with SGD and ALS
Stochastic gradient descent (SGD) updates one observed rating at a time. It minimizes prediction error plus regularization, which discourages overly large factors. Alternating least squares (ALS) fixes Q while solving for P, then fixes P while solving for Q. ALS is often easier to parallelize and works well with implicit data.
Implicit Feedback
For clicks, views, or purchases, a missing value usually means “unknown,” not “dislike.” Implicit-feedback models assign confidence weights to observed events and sample or weight unobserved pairs as negatives. Do not treat every zero as a confirmed rejection.
Practical Limits
New users and new items create the cold-start problem because no interaction history exists. Ask for preferences, use popular defaults, or use item metadata.
Large catalogs make scoring every item expensive. Precompute item vectors and retrieve nearby vectors with approximate nearest-neighbor indexes.
Popularity bias causes frequent items to dominate recommendations. Feedback loops strengthen this effect: exposure creates interactions, which make the item appear even more popular. Add exploration, diversity, and popularity-aware evaluation.
Hybrid Models
A hybrid recommender combines collaborative factors with content signals such as genre, text, or price. Content features help cold-start items, while collaborative factors capture behavior that metadata misses.
Exercise
Why can a hybrid model recommend a brand-new item?
Show answer
Its content features can produce a score before users have interacted with it.