Matrix factorization
Quick answer
Matrix factorization approximates a user-item interaction matrix using low-dimensional user and item vectors. Their dot product produces a score for ranking or a rating estimate, depending on the training objective. Examples are static Python snippets tested locally on CPU; NumPy and scikit-learn are required for selected examples. ByteMind execution is unverified.
Factor matrix scores
import numpy as npP=np.array([[1,2],[0,1]]);Q=np.array([[2,1],[1,3],[0,2]])print((P@Q.T).tolist())Expected output (locally tested): [[4, 7, 4], [1, 3, 2]]. Two user rows and three item rows give six scores.
How it works
For U users, I items and d factors, P has shape U by d and Q has shape I by d. Predicted matrix is P Q^T; score s_ui=p_u dot q_i, optionally with global/user/item biases. For explicit ratings minimize sum over observed entries of (r_ui-s_ui)^2 plus lambda times squared factor norms; unobserved entries are not target ratings. For one observed pair with loss 0.5*(s-r)^2 + 0.5lambda(||p||^2+||q||^2), gradients are (s-r)q+lambdap and (s-r)p+lambdaq. Compute both from the old factors before updating. Implicit weighted ALS uses binary preferences and confidence-weighted squared errors including low-confidence missing entries, alternating least-squares solves with the other factor fixed. Bayesian personalized ranking instead optimizes pairwise ordering, e.g. -log sigmoid(s_positive-s_sampled_negative), with sampling assumptions. Low rank compresses relationships but does not guarantee interpretable factors: rotations can preserve dot products. Initialize reproducibly, monitor held-out ranking not merely training reconstruction, and regularize. Cold-start IDs lack learned rows; use metadata or a fallback. A dense SVD of zero-filled explicit data is not the observed-only objective.
Simultaneous gradient update
p=[1.,2.];q=[2.,1.];r=5e=sum(a*b for a,b in zip(p,q))-rpp=[a-.1*e*b for a,b in zip(p,q)];qq=[b-.1*e*a for a,b in zip(p,q)]print(round(sum(a*b for a,b in zip(pp,qq)),2))Expected output (locally tested): 5.04. Both gradients use the original vectors; squared error falls.
Common mistakes
Do not update q using already updated p in a simultaneous SGD derivation. Do not mistake a factor coordinate for a demographic attribute or score for a probability.
Practice and next steps
For p=[1,2], q=[2,1], r=5 and lambda=0, derive both gradients, then verify one step of learning rate 0.1 decreases this pair loss.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Matrix factorization — knowledge check
Apply matrix factorization to small examples and debugging scenarios.
What is the shape of P Q transpose?
What is the explicit rating objective mask?
What does regularization discourage?
Which factors should simultaneous SGD use?
Can a latent coordinate prove an interpretable preference?