Foundations to Frontier
Tests
D. Classical Machine Learning › D9 Dimensionality Reduction and Manifold Learning

Truncated SVD and Latent Semantic Analysis

Truncated SVD keeps only the top k singular triplets of a matrix, giving its best rank-k approximation. Unlike PCA it does not centre the data, so it works directly on large sparse matrices such as document–term counts. Applied to a TF-IDF matrix it becomes latent semantic analysis (LSA, Deerwester et al., 1990): documents and terms are embedded in a k-dimensional concept space where words that occur in similar contexts end up close. Queries can then match documents that share meaning but not words. Randomised SVD (Halko, Martinsson and Tropp) computes the leading triplets fast, using a random projection plus a few power iterations. This page covers truncated SVD versus PCA, TF-IDF, LSA's concept space and folding-in, choosing k, randomised SVD and its error behaviour, and LSA's limits. In the exercises you build LSA and show it retrieving synonym-only matches; then you map randomised SVD's accuracy against oversampling and power iterations.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Finding concepts behind words

Search by exact words misses documents that say “seal failure” when you typed “leak”. Latent semantic analysis notices that “leak”, “seal” and “pump” keep appearing around the same other words, and treats them as belonging to one underlying concept. It does this with a truncated SVD of the document–word table: keep only the few strongest patterns of co-occurrence, and represent every document and every word by how much it uses each pattern.

LSA: matching meaning, not just words
raw TF-IDF
  1. fluid leak pump inspect (1.69)
  2. actuator leak pump found (1.65)
  3. leak valve pump check (1.60)
  4. seal leak pressure found (1.54)
  5. software radio antenna check (0.00, no query word)
  6. rivet corrosion dent replace (0.00, no query word)
LSA (k = 3)
  1. actuator fluid hose found (1.00, no query word)
  2. actuator leak pump found (1.00)
  3. seal leak pressure found (0.99)
  4. seal hose valve found (0.98, no query word)
  5. fluid leak pump inspect (0.97)
  6. pressure fluid hose inspect (0.96, no query word)
among notes without the word, share of the top 6 from the right subsystem: raw 17% · LSA 100%
Colours mark the subsystem: hydraulics, avionics, structures. Raw TF-IDF can only find notes containing the query word; everything else scores 0. LSA also ranks notes from the same subsystem that use different words, because those words co-occur with the same neighbours. With k = 3 the concepts match the three subsystems. With k = 1 everything collapses, and with large k LSA drifts back toward raw word matching.
Why engineers care

Maintenance logs, incident reports and requirement documents use inconsistent wording. LSA-style embeddings let you search, cluster and deduplicate them by meaning. More generally, truncated SVD is the workhorse for compressing huge sparse matrices (user–item ratings, sensor–time tables) without densifying them.

📝 Quick check (Level 0)
0/3 answered
  1. 1. LSA can match a query to a document that…
  2. 2. LSA builds its concepts from…
  3. 3. Truncated SVD keeps…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

TF-IDF, truncation and folding-in by hand

IDF. idf(t) = ln((1 + n)/(1 + df_t)) + 1 (smoothed). With n = 3 documents:

  • a term in all three gets idf = ln(4/4) + 1 = 1;
  • a term in one gets ln(4/2) + 1 = 1.693.

TF-IDF weights rare, distinctive words more heavily.

Truncation. The singular values of diag(3, 1, 2) sorted are 3, 2, 1; keeping k = 2 keeps 3 and 2. The rank-k error:

  • for diag(4, 3, 2, 1) with k = 2, the Frobenius error is √(2² + 1²) = √5 ≈ 2.236;
  • the spectral error is the next singular value, 2.

Folding in a query. With A ≈ U_k S_k V_kᵀ (documents × terms), a new term vector q maps to S_k⁻¹V_kᵀq (V_k is terms × k), the coordinates on the same scale as U’s rows. Then rank documents by cosine similarity in that space.

Cosine. For (1, 0) and (1, 1): 1/√2 = 0.707. Two documents with no shared words have raw cosine 0, but can be close in LSA space.

The steps in plain English:

  1. Build the document–term count matrix and apply TF-IDF weighting (and row normalisation).
  2. Compute a truncated SVD with k concepts (randomised SVD for large matrices). Do not centre: that would destroy sparsity.
  3. Represent documents by U_k S_k (or U_k), and terms by V_k S_k.
  4. Fold in queries or new documents, compare by cosine, and choose k by retrieval quality on held-out queries.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Spectral error of the best rank-1 approximation of diag(5, 2, 1)?
  2. 2. Why doesn't LSA centre the matrix?
  3. 3. IDF of a term appearing in every document (smoothed)?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.

Why LSA works, and computing it fast

Concepts from co-occurrence. The truncated SVD finds the k directions explaining the most TF-IDF energy. Words used interchangeably within a topic co-occur with the same context words, so they load on the same singular vectors and their columns collapse together in V_k. A query on one of them then lands near documents using the others.

Exercise 1: 150 documents, 3 topics, each with 30 interchangeable words. For one-word queries, among documents that did not contain the query word:

  • raw TF-IDF scored them all 0, so precision@10 was 0.333 (chance);
  • LSA with k = 3 reached precision 1.000.

Choosing k. Too small merges distinct topics; too large keeps the noise and drifts back toward exact word matching (watch the demo at k = 8). Typical text uses k = 100–300. Pick it by retrieval or downstream performance.

Randomised SVD (Halko, Martinsson and Tropp, 2011).

  1. Multiply A by a random n × (k + p) Gaussian matrix to sample its range.
  2. Optionally apply q power iterations, A(AᵀY), to sharpen the separation of singular values.
  3. Orthonormalise to Q, compute the SVD of the small matrix QᵀA, and map back.

The cost is O(mn(k + p)) per pass, with only a few passes over A. That is ideal for large sparse matrices.

Error behaviour. The best possible spectral error is σ_(k+1). Exercise 2 (rank 10):

  • with no oversampling and no power iterations, randomised SVD was about 2× worse than optimal;
  • 20 extra samples alone were optimal for fast-decaying spectra, but 1.3× worse for slowly decaying ones;
  • one power iteration made it optimal (1.004×).
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.decomposition import TruncatedSVD
X = TfidfVectorizer(sublinear_tf=True).fit_transform(docs)                       # sparse
lsa = TruncatedSVD(n_components=200, algorithm="randomized", n_iter=5, random_state=0).fit(X)
Z = lsa.transform(X)                                                             # document concept coordinates
🐍 Exercise 1: LSA with randomised SVD
Implement smoothed TF-IDF, randomised SVD (oversampling + power iterations) and cosine retrieval; show LSA retrieving same-topic documents that share no words with one-word queries, and check against the exact SVD.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Synonyms end up close in LSA space because…
  2. 2. Power iterations in randomised SVD help most when…
  3. 3. The best possible rank-k spectral error is…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Randomised range-finder bound. For a Gaussian test matrix with oversampling p ≥ 2, the expected error satisfies

E‖A − QQᵀA‖ ≤ (1 + √(k/(p − 1)))σ_(k+1) + (e√(k + p)/p)(Σ over j > k of σⱼ²)^½.

The tail term is what hurts with slow decay. q power iterations replace the singular values σ by σ^(2q+1) in the bound, crushing the tail.

Truncated SVD versus PCA. On centred data they coincide. On uncentred data the first singular vector mostly captures the mean direction. For TF-IDF that is acceptable and keeps sparsity; for dense numeric features, PCA (centring) is usually what you want.

LSA’s limits.

  • Bag of words: word order and negation are ignored.
  • Polysemy: one word gets one vector, whatever its sense.
  • Linearity, and signed components that are hard to interpret.

Probabilistic LSA, LDA topic models, NMF, and neural embeddings (word2vec, transformers) address these. Interestingly, word2vec with negative sampling implicitly factorises a shifted PMI matrix, a close relative of LSA (Levy and Goldberg, 2014).

Interview questions.

  • How would you search 10⁶ maintenance reports by meaning cheaply? Use sparse TF-IDF and randomised truncated SVD with k of about 200, store normalised document vectors with an approximate-nearest-neighbour index, and fold in queries. Evaluate on labelled query–document pairs.
  • When is randomised SVD inaccurate, and how do you fix it? With slowly decaying spectra. Increase the oversampling (p of about 10–20) and add 2–5 power iterations, and check against a small exact SVD on a sample.
📝 Quick check (Level 3)
0/3 answered
  1. 1. q power iterations effectively replace singular values by…
  2. 2. On centred data, truncated SVD equals…
  3. 3. word2vec (SGNS) implicitly factorises…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

How much oversampling and how many power iterations?

History.

  • S. Deerwester, S. Dumais, G. Furnas, T. Landauer and R. Harshman introduced LSA (1990).
  • T. Hofmann (1999) proposed probabilistic LSA.
  • N. Halko, P.-G. Martinsson and J. Tropp (2011) surveyed randomised matrix decompositions.
  • O. Levy and Y. Goldberg (2014) linked word2vec to matrix factorisation.

Exercise 2. Rank-10 randomised SVD of 300 × 200 matrices, 3 runs. Values are the spectral error divided by the optimal σ₁₁ (1.000 = as good as the exact truncated SVD).

spectrum oversampling p q = 0 q = 1 q = 3
fast decay (e^(−i/6)) 0 2.151 1.168 1.027
5 1.326 1.000 1.000
20 1.000 1.000 1.000
slow decay (i^(−½)) 0 2.029 1.226 1.074
5 1.839 1.106 1.008
20 1.305 1.004 1.000
  • Without oversampling or power iterations, the error was about twice optimal for both spectra.
  • Oversampling alone fixed fast decay (p = 20 was optimal) but not slow decay (still 1.31×), because the energy in the tail leaks into the sampled range.
  • One or two power iterations fixed slow decay. scikit-learn’s default of several power iterations is there for exactly this.
🐍 Exercise 2 (research): randomised SVD accuracy
For matrices with fast and slow singular-value decay, measure rank-10 randomised SVD's spectral error relative to the optimum for oversampling p = 0, 5, 20 and power iterations q = 0, 1, 3.

Truncated SVD in AI, robotics and aerospace

  • Semantic search of maintenance logs, anomaly reports and requirements.
  • Compressing large sparse matrices (sensor–time, user–item) without densifying.
  • Fast low-rank approximations inside larger pipelines: reduced-order models, kernel approximations, preconditioners.

References

  1. S. Deerwester, S. T. Dumais, G. W. Furnas, T. K. Landauer, R. Harshman (1990). “Indexing by latent semantic analysis.” Journal of the American Society for Information Science 41(6).
  2. N. Halko, P.-G. Martinsson, J. A. Tropp (2011). “Finding structure with randomness: probabilistic algorithms for constructing approximate matrix decompositions.” SIAM Review 53(2).
  3. T. Hofmann (1999). “Probabilistic latent semantic analysis.” Proc. UAI.
  4. O. Levy, Y. Goldberg (2014). “Neural word embedding as implicit matrix factorization.” NeurIPS 27.
  5. scikit-learn developers. “TruncatedSVD.” Documentation.