Truncated SVD
Quick answer
Truncated singular value decomposition (SVD) retains a low-rank approximation without automatically subtracting column means. It is often useful for sparse count or TF-IDF matrices. Static examples tested locally with Python 3.13.5, scikit-learn 1.8.0 SciPy 1.17.0 and NumPy 2.3.5 where imported. ByteMind package execution is unverified.
Compute optimal discarded energy
s=[5,3,1]print(sum(v*v for v in s[1:]),sum(v*v for v in s[2:]))Observed local output:
10 1Squared singular values give the squared Frobenius residual for exact truncation.
How it works
For X of shape n by d, the rank-k approximation X_k = U_k S_k V_kᵀ keeps the k largest singular values. The squared Frobenius reconstruction error for the exact optimal truncation is sum of discarded s_j². If singular values are 5, 3, 1, retaining two yields squared error 1; retaining one gives 10. The projected scores are X V_k and reconstruction uses scores times V_kᵀ. Unlike PCA, scikit-learn TruncatedSVD does not center the matrix; subtracting a nonzero mean from sparse entries would generally densify it. This means the leading direction may capture average frequency rather than centered variation. Its explained_variance_ratio_ is based on variance of projected samples relative to original variance, not simply uncentered squared singular-value energy. For approximate randomized solvers, actual error may exceed the optimum; set seed, iterations and component count explicitly. Fit vocabulary/IDF and SVD on training documents before transforming future documents. The transformed representation is usually dense, even when the input is sparse. Signs can change, so compare reconstruction or subspace rather than raw signed vectors.
Use a sparse matrix
import numpy as npfrom scipy.sparse import csr_matrixfrom sklearn.decomposition import TruncatedSVDX=csr_matrix([[5.,0,0],[0,3,0],[0,0,1]])m=TruncatedSVD(n_components=2,random_state=7,n_iter=7)Z=m.fit_transform(X)print(Z.shape,round(float(np.linalg.norm(X.toarray()-m.inverse_transform(Z))**2),6))Observed local output:
(3, 2) 1.0Only the tiny fixture is densified to calculate reconstruction error.
Common mistakes
Do not equate uncentered SVD energy with PCA explained variance. Avoid densifying large sparse inputs and selecting k on the final test set. Do not expect exact reconstruction at low rank.
Practice and next steps
Compute the discarded energy of [8,4,2] at ranks one and two. Explain the memory effect of centering a count matrix containing many zeros. Complete the knowledge check and staged exercise. Next: NMF and ICA assumptions.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Truncated SVD — knowledge check
Apply truncated svd to new scenarios.
Why is TruncatedSVD useful for sparse text?
What is the squared exact rank-one error for [5,3,1]?
How does a randomized solver affect error?
Where is vocabulary fitting performed?
Which claim about uncentered SVD ratios is correct?