Your TF-IDF matrix has 50,000 columns for 20,000 documents — how do you decide what to prune?
answer
- do the storage arithmetic first
- column count is not the same as cost
- the vocabulary tail is enormous and near-empty
- cut-offs on document frequency, both ends
- the threshold is a fold-fitted hyperparameter
basics
~20 sCheck the arithmetic before pruning: stored sparsely, 20,000 documents touching a hundred terms each is a few million values, not a billion. Cut with document-frequency thresholds at both ends, and let held-out folds decide how far to go.
solid answer
~50 sFirst check whether the width is actually a problem. Dense, that matrix would be a billion cells; sparse, each document has perhaps a hundred non-zero terms, so you are storing a few million values — tens of megabytes. Width alone is not a reason to cut. When memory, latency or overfitting do argue for cutting, the honest dials are document-frequency based. A minimum-document-frequency cut-off removes terms appearing in fewer than `k` documents; in a typical corpus these are typos and one-offs, they are a large fraction of the distinct vocabulary and a tiny fraction of the signal. A maximum-document-frequency cut-off removes near-universal terms, which is a data-driven substitute for a hand-written stopword list. You can also cap the vocabulary at the most frequent `V` terms outright. Whichever you use, it is a hyperparameter tuned on held-out folds, and it is fitted inside the fold like the vocabulary itself.
go deeper
Know that a text matrix is stored sparsely, so most of those 50,000 columns cost nothing for a document that does not use them. Recognise the names of the two document-frequency cut-offs.
Be able to do the arithmetic out loud — non-zeros per row times rows — and explain why cutting terms that appear in one document removes most columns but almost no data.
Show the empirical habit: baseline first, sweep the threshold, read score against artefact size, and keep the cut-off fitted inside the fold so the estimate stays honest.
Own the budget framing. Decide when accuracy on the plateau's edge is worth trading for latency or artefact size, and insist that debuggability of one-column-one-term is itself a value being traded.
## Is 50,000 columns actually wide? The reflex is that having more columns than rows is dangerous. Before acting on that reflex, do the arithmetic. Stored densely, 20,000 x 50,000 is one billion cells — at eight bytes each, about eight gigabytes, most of it zeros. Stored sparsely, only the non-zero entries exist. A document containing a hundred distinct terms contributes a hundred entries, so the whole matrix is roughly two million values plus index overhead: a few tens of megabytes. The density is about 0.2%. So the first answer to "we have 50,000 columns" is: that is normal for text, it is cheap in the right storage format, and the column count on its own is not evidence of a problem. What *would* be evidence: - Training or scoring time is unacceptable, and profiling points at the feature matrix. - The serialised artefact — the vocabulary map plus the idf vector — is too big to ship to where it has to run. - Held-out performance improves when you prune, which is the only argument that settles it. ## The shape of a text vocabulary Term frequencies in natural language are extremely skewed. A small number of terms occur in nearly every document; an enormous tail occurs once or twice in the entire corpus. In a typical corpus a very large share of the distinct terms — often more than half — appear in exactly one document. Those are typos, product codes, one-off proper nouns, tokenisation debris. That shape is what makes pruning cheap. Cutting the tail removes most of the columns and almost none of the mass in the matrix, because those terms occur in one document each by construction. ## The dials **Minimum document frequency.** Drop any term appearing in fewer than `k` documents (or fewer than some fraction of them). `k = 2` alone often halves the vocabulary. The argument is statistical as well as economic: a term seen in one training document gives the model a column that can only ever fire for that document, which is an invitation to memorise. Raising `k` to 5 or 10 is common on large corpora. **Maximum document frequency.** Drop any term appearing in more than, say, 90% of documents. This is a corpus-derived stopword list — it removes whatever *this* corpus treats as filler, which beats a generic list because the filler is domain-specific. In a corpus of support tickets, `ticket`, `issue` and `customer` may be near-universal and useless even though no standard stopword list contains them. Note the interaction with idf: those terms already have weights close to zero, so removing them mostly buys memory and clarity rather than accuracy. **A hard cap on vocabulary size.** Keep the `V` most frequent terms overall. Blunter than the two cut-offs, but it gives a predictable artefact size, which matters when you have a memory budget to hit. **Explicit stopword removal.** Fine, but largely redundant once idf and a maximum-document-frequency cut-off are in play, and it introduces a hand-maintained list that drifts away from the data. ## Two things people get wrong **Pruning is fitted, so it lives inside the fold.** A document-frequency threshold decides which columns exist. Compute it over the whole corpus and held-out documents help create or destroy features, which biases your estimate. Fit it on the training portion of each fold along with the vocabulary and the idf values. **Do not confuse pruning with dimensionality reduction.** Cutting rare terms removes columns you decided are not worth their weight. Projecting the matrix into a smaller dense space is a different operation with different tradeoffs, and it destroys the one-column-one-term correspondence that makes a sparse text model debuggable. If the reason you wanted fewer columns was to keep the model explainable, pruning helps and projection hurts. ## How to actually decide Treat the cut-offs as hyperparameters, not as hygiene. 1. Establish the baseline with no pruning, so you know what you are trading against. 2. Sweep the minimum-document-frequency threshold across a few values and watch held-out score, artefact size and training time together. 3. Expect a plateau: score is usually flat over a wide range of `k` while vocabulary size falls dramatically. Take the aggressive end of the plateau. 4. Only if the plateau slopes downward do you have a genuine accuracy-versus-size tradeoff to escalate, and then it is a product decision about latency or memory budget, not a modelling one. The interviewer is listening for whether you reach for arithmetic before reaching for a knob, and whether you know that the answer is measured rather than asserted.
- How much memory does that matrix actually take in sparse form?If each of the 20,000 documents has around a hundred distinct terms, there are roughly two million non-zero entries. Storing a value plus a column index for each, with row offsets, lands in the tens of megabytes. Dense, the same matrix would be a billion cells and about eight gigabytes. The format, not the column count, decides whether width is a problem.
- Why does raising the minimum document frequency to 2 usually shrink the vocabulary so much?Because term frequencies are extremely skewed: a very large share of distinct terms — often more than half — occur in exactly one document. They are typos, identifiers and one-off names. Removing them deletes most of the columns while removing almost none of the matrix's non-zero mass, since each contributed a single entry.
- If idf already down-weights near-universal terms, why bother with a maximum-document-frequency cut-off?Mostly for memory and clarity rather than accuracy — idf has already made those columns nearly inert. The cut-off is useful because it is corpus-derived: it catches domain filler like `ticket` or `customer` that no generic stopword list contains, and it removes the column outright instead of carrying a near-zero one.
- How do you tell whether a pruning threshold is too aggressive?Sweep it and watch held-out score against vocabulary size. Typically the score is flat across a wide range while the vocabulary collapses; take the aggressive end of that plateau. When the score starts sloping down you have crossed into a real tradeoff, and that becomes a budget conversation about latency or artefact size rather than a modelling choice.
saying these in an interview costs you the question
- Panics that columns exceed rows without checking sparsity
- Quotes dense memory cost for a sparse matrix
- Prunes by intuition instead of a held-out sweep
- Computes document-frequency cut-offs over the whole corpus
- Treats pruning and dimensionality reduction as the same thing