Sub topic 12
Factorization for implicit feedback
This chapter assumes you know the explicit-feedback factorization from the previous chapters: the user and item factor matrices, the dot-product prediction, and the squared-error loss. It also assumes NumPy and basic linear algebra.
Why the explicit loss does not carry over
Implicit feedback is behavior such as plays, clicks, or purchases. It has two properties that break the explicit-rating setup.
- There are no negatives. A zero means "no interaction", not "dislike". The user may never have seen the item.
- Values are counts, not opinions. Ten plays signal stronger evidence than one play, but not ten times the liking.
If you train only on observed entries, as SGD does for ratings, the model sees only positives and learns to predict a high score for everything. You must also use the zeros.
Preference and confidence
The standard approach (Hu, Koren, and Volinsky) splits each entry r_ui into two quantities.
- Preference
p_uiis 1 ifr_ui > 0, otherwise 0. - Confidence
c_ui = 1 + alpha * r_uisays how much to trust that preference.
The loss covers every user-item pair, not just observed ones:
sum over all (u,i) of c_ui * (p_ui - x_u · y_i)^2 + reg * (|x_u|^2 + |y_i|^2)
A zero entry still pulls the prediction toward 0, but with the minimum confidence of 1. A heavily played item pulls toward 1 with a much larger weight. The parameter alpha controls how fast confidence grows with counts.
Why ALS fits this loss
Summing over all pairs makes plain SGD expensive, because the number of pairs is users times items. Alternating least squares (ALS) avoids this. Fix the item factors, and each user vector has a closed-form solution. Then fix the user factors and solve for each item vector. Each solve is a small k x k linear system.
A runnable example
The matrix below holds play counts. The code builds P and C, runs ALS, and ranks the items user 0 has not played.
import numpy as np
def implicit_als(R, k=2, alpha=40.0, reg=0.1, iters=10, seed=0):
rng = np.random.default_rng(seed)
n_users, n_items = R.shape
P = (R > 0).astype(float)
C = 1.0 + alpha * R
X = rng.normal(0, 0.1, (n_users, k))
Y = rng.normal(0, 0.1, (n_items, k))
I = np.eye(k)
for _ in range(iters):
for u in range(n_users):
YtC = Y.T * C[u]
X[u] = np.linalg.solve(YtC @ Y + reg * I, YtC @ P[u])
for i in range(n_items):
XtC = X.T * C[:, i]
Y[i] = np.linalg.solve(XtC @ X + reg * I, XtC @ P[:, i])
return X, Y
# Play counts: rows are users, columns are items.
R = np.array([
[10, 3, 0, 0],
[8, 0, 0, 1],
[0, 0, 6, 9],
[0, 1, 7, 5],
], dtype=float)
X, Y = implicit_als(R)
scores = X @ Y.T
unseen = np.where(R[0] == 0)[0]
ranked = unseen[np.argsort(scores[0, unseen])[::-1]]
print("Items to recommend to user 0, best first:", ranked)Each user solve uses all items, with weights C[u]. The cost per solve is roughly O(n_items * k^2). Production implementations rewrite the algebra so that only the observed entries need to be visited, using a precomputed Y^T Y.
Common errors
- Treating zeros as ratings of 0 and training with the explicit loss. The model collapses toward low scores for unobserved items.
- Using raw counts as confidence without scaling. A few extreme users dominate the loss. Try
1 + alpha * log(1 + r)instead. - Evaluating with RMSE. The predictions are not on a rating scale, so use ranking metrics such as precision@k and recall@k.
- Recommending items the user already played. Mask them out before ranking, as the code does.
Exercise
In the implicit loss, why does a zero entry still contribute to the loss, and what weight does it get?
Show answer
A zero means the user has not interacted with the item. It is weak evidence of dislike, so the model is pulled toward a preference of 0. The confidence is c = 1 + alpha * 0 = 1, the minimum, so it counts far less than an observed entry with many plays.