How do you pick the truncated-SVD rank k for a users-by-items ratings matrix?
answer
- k is a budget, not a formula
- the spectrum brackets, it does not decide
- in-sample error only ever falls
- hold out known entries and sweep k
- smallest k that is not meaningfully worse
basics
~20 sTreat k as a budgeted tradeoff, not a formula. Let the flattening of the singular-value spectrum bracket a range, then choose inside it by held-out reconstruction error and the downstream metric, subject to storage and serving limits.
solid answer
~50 sI start with the spectrum: listing `sigma_1` down to `sigma_r` shows where the values flatten into a slowly decaying tail, which brackets a plausible range for k rather than naming one value. Eckart-Young then tells me exactly what each candidate costs in reconstruction error, since the Frobenius residual is the root sum of squares of the discarded singular values. But reconstruction error on the entries I factored is the wrong objective, because it falls monotonically with k. So I hold out a sample of known ratings, refactor at each candidate k, and measure error on those held-out entries, which penalizes both too-coarse and noise-fitting choices. Then I weigh the operational side: `k(m + n + 1)` numbers to store and serve, and the cost of refactoring as the matrix grows. Finally I confirm against the metric the system is actually judged by, and take the smallest k that is not meaningfully worse.
go deeper
Know that k is the number of singular values kept, and that a smaller k gives a coarser and cheaper approximation of the matrix.
Explain what varying k does to reconstruction error and to storage, and that the discarded singular values quantify the approximation error exactly.
Show an evaluation protocol: mask known entries, refactor at each candidate k, and choose by held-out error rather than by fit on the data you factored.
Own the whole tradeoff, approximation quality against serving cost, refactoring cadence and the business metric, and be ready to defend a smaller k than the error curve alone would suggest.
## Why there is no formula A users-by-items ratings matrix factored by a truncated SVD produces k latent factors per user and per item. The value of k controls three things at once: how much of the matrix's structure is retained, how many free numbers the representation carries and therefore how easily it fits noise, and how much it costs to store and serve. No single criterion addresses all three, which is why this is a judgment call and interviewers ask it as one. ## Step 1: let the spectrum bracket the range List the singular values in order. Real ratings matrices typically show a few large values followed by a long, slowly decaying tail. The point where the decay flattens is informative: layers beyond it each contribute a similar, small amount of energy, so they are hard to distinguish from noise. Treat this as a **bracket**, not an answer. Reading a precise k off a curve with no sharp elbow is the classic overreach, and many real spectra have no visible break at all. Eckart-Young converts any candidate k into an exact error figure: the Frobenius residual is `sqrt(sigma_{k+1}^2 + ... + sigma_r^2)`. That is useful for pricing the options, and it is free once you have the spectrum. ## Step 2: evaluate on data you did not factor Reconstruction error on the entries you factored is monotonically decreasing in k, so it always votes for the largest k and can never tell you to stop. The fix is a holdout: mask a random sample of known ratings, factor the remaining matrix at each candidate k, and score the predictions on the masked entries. That curve typically falls and then rises, because each extra layer adds `m + n + 1` free numbers and eventually starts reproducing noise in the observed ratings instead of structure. The turning point is the honest signal. Masking by random entries and masking whole users or items answer different questions, and a system that must serve new users should be evaluated the second way. ## Step 3: respect the operational budget A rank-k factorization stores `k(m + n + 1)` numbers. On a matrix with millions of users this dominates memory, the per-request latency of scoring a user against candidate items, and the wall-clock cost of refactoring on a schedule. A k that is twice as large for a fractional gain in held-out error is usually the wrong call. Break-even against storing the raw matrix arrives at `k(m + n + 1) = m n`, though for a large sparse ratings matrix the practical comparison is against the number of observed ratings, not against mn. ## Step 4: judge on the metric that matters Reconstruction error is a proxy. The system is graded on something else, and the mapping between the two is not monotone: a k that reconstructs held-out ratings slightly better may not change the ordering the product depends on. Sweep a small set of candidate k values against the downstream metric and prefer the smallest that is not meaningfully worse, since smaller is cheaper, faster to recompute and more stable across refits. ## The missing-data caveat A plain SVD needs a value in every cell, and a ratings matrix is mostly empty. Filling the gaps with zeros or a global mean makes the decomposition fit the fabricated values as though they were observations, which distorts both the spectrum and the choice of k: what you are looking at partly describes your imputation. This is why factorizations that model only the observed entries are preferred for this matrix. Naming that caveat unprompted is a strong signal in an interview, because it shows you know the difference between the mathematics of the SVD and the shape of real ratings data. ## Stability as a tiebreaker A final consideration: refit the factorization on a slightly different slice of data and see whether the chosen k still looks reasonable. If the held-out curve is flat over a wide band of k, any value in that band is defensible, and picking the low end is the safer engineering decision. ## Common mistakes Declaring a k from the spectrum alone; optimizing reconstruction error on the fitted entries; quoting a universal rule such as a fixed number of factors for every dataset; ignoring serving cost and refactoring cadence; and silently zero-filling missing ratings.
- Why can a plain SVD of a ratings matrix with missing entries mislead you?A dense factorization needs a value in every cell, so the unobserved pairs must be filled in, and zeros or a global mean are then fitted as though they were data. The spectrum and the k it suggests partly describe the imputation rather than the ratings, which is why factorizations that model only the observed entries are preferred for this matrix.
- How does k trade off against overfitting here?Each extra rank-1 layer adds m + n + 1 free numbers, so a large k can reproduce the observed ratings almost exactly while generalizing badly to unseen pairs. Held-out error typically falls and then rises as k grows, and that turning point, not the point where in-sample reconstruction error is smallest, is the useful signal.
- If the singular-value spectrum shows no clear flattening, what do you do?Accept that there is no low-rank structure to read off and stop hunting for an inflection point. Either pick k from the operational budget and accept the error Eckart-Young predicts, or conclude that a low-rank factorization is the wrong model for this matrix and evaluate an alternative on the downstream metric instead.
saying these in an interview costs you the question
- Picks k by staring at the spectrum alone
- Optimizes reconstruction error on the entries it factored
- Quotes a universal number of factors for every dataset
- Ignores serving cost and refactoring cadence
- Zero-fills missing ratings without comment