skip to content

Why does a linear model usually beat boosted trees on sparse n-gram text features?

level: seniorimportance: nice to knowfreq 30%

answer

  1. wide, sparse, almost all zeros
  2. many weak cues, purely additive
  3. one feature tested per split
  4. rare term gives unbalanced split
  5. dense features bring trees back

basics

~20 s

An n-gram matrix has tens of thousands of mostly-zero columns holding many weak additive cues. A linear model sums all their weights at once; a tree tests one feature per split, so it needs thousands of trees.

solid answer

~50 s

Bag-of-n-grams text is wide, sparse and additive: tens of thousands of columns, almost all zero in any one document, with the signal spread over hundreds of individually weak lexical cues. A linear model is exactly that shape - one weight per term, summed - so a regularized logistic regression or linear SVM absorbs all of it in a single pass, and the penalty handles the dimensionality. A tree splits on one feature at a time, and a split on a term present in 0.3% of documents carves off a tiny, unbalanced branch with almost no gain, so the ensemble needs thousands of trees to represent what one weight vector encodes. Trees earn their keep when a few features interact non-linearly, which is not how lexical evidence behaves. Compress the text into a few hundred dense features first and the tree family is competitive again.

go deeper

for a junior

Remember the pairing: wide sparse text goes to the linear family, dense mixed-type tables go to boosted trees. Being able to state the rule and give one reason is enough here.

for a middle

Explain the mechanics: a split tests one feature, so a term appearing in a fraction of a percent of documents produces a tiny branch and negligible gain, while a linear model gives every term a weight in one pass.

for a senior

Show that you reason from the representation rather than from the word text. Say what changes once documents are compressed to dense features, and how you would combine a text score with tabular metadata in production.

for a principal

Own the pipeline decision: whether the organisation maintains one representation and one family per data type, or a hybrid where a text score feeds a tabular model, has lasting maintenance, latency and staffing consequences.

### The shape of bag-of-n-gram text Represent documents as counts or weights over unigrams and bigrams and you get a matrix that is wide, sparse and additive. Wide: 50,000 to several hundred thousand columns is ordinary. Sparse: a single review touches a few dozen of those columns, so well over 99% of the matrix is zero. Additive: the evidence that a review should be routed to billing rather than shipping is spread across hundreds of individually weak cues - refund, charged, invoice, double, statement - none of which decides the case alone. Model choice on text is really a question about that shape, not about the words. ### Why the linear family fits the shape A linear model is exactly this shape written down. One weight per term, a document's score is the sum of the weights of the terms it contains, and every one of the hundreds of weak cues contributes in a single pass. Regularization does the work that would otherwise be impossible in 50,000 dimensions: an L2 penalty spreads credit across correlated near-synonyms, an L1 penalty prunes the vocabulary to the terms that carry weight. Regularized logistic regression and linear support vector machines have been the strong, hard-to-beat baselines for sparse text classification for decades, and they train in seconds on millions of documents because the arithmetic touches only the non-zero entries. ### Why a boosted tree ensemble fights the shape Three mechanisms work against trees here. - One feature per split. A split tests a single column - does the term refund appear at least once. To accumulate the hundreds of weak cues a linear model sums in one step, the ensemble needs a separate branch, in a separate tree, for each of them. Thousands of trees can approximate what one weight vector encodes directly, at far greater cost. - Rare terms give unbalanced splits with almost no gain. A term appearing in 0.3% of documents sends 99.7% of the rows down one branch. The impurity improvement is tiny, so the greedy search often prefers a frequent, uninformative term, and the informative rare cues are never used. - The split search itself is expensive and noisy over that many candidate columns, and the interactions trees exist to capture - this feature matters only when that one is high - are not how lexical evidence usually behaves. Trees are being asked to pay for a capability the data does not need. None of this makes boosting impossible on text; with heavy feature selection and enough tuning it can be pushed to a respectable score. The claim is about equal effort: at the same budget, a regularized linear model normally wins on raw sparse n-grams, and it gets there far faster. ### When the tree family comes back Change the representation and the argument disappears. Compress each document into a few hundred dense continuous features - a topic or similarity score, a length, a readability number, the output score of a linear text model - and you have an ordinary tabular problem where boosted trees are back in their element. That hybrid is the usual production shape for a routing system: a linear model reads the text and emits one score, and a tabular model combines that score with metadata such as account age, order value, channel and prior contacts, where genuine interactions between those columns do exist. Feeding raw n-gram columns and a dozen metadata columns into the same tree ensemble is the worst of both worlds - the metadata signal is buried among 50,000 near-empty candidate splits. ### Diagnosing rather than assuming The way to know the linear choice is right is to look at where it fails. If errors concentrate on cases where meaning depends on two tokens co-occurring - a negation next to a positive word, a product name next to a complaint verb - additive per-term weights genuinely cannot express that, and the cheap fix is to add the interacting phrases as explicit terms rather than to change family. If errors are spread evenly and the confusion is between genuinely similar categories, more capacity is not the problem; the labels or the taxonomy are.

  • When would you put text and tabular metadata into the same boosted model?
    Once the text has been compressed into a handful of dense features - the score of a linear model over the text, a topic or similarity number, a length - the tree can combine those with account age, order value or channel and pick up genuine interactions between them. Feeding raw n-gram columns alongside a dozen metadata columns instead buries the metadata signal among tens of thousands of near-empty candidate splits.
  • Does the argument still hold if the text is represented as dense embeddings?
    Not in the same form. A few hundred dense continuous columns is an ordinary tabular shape and the sparsity argument disappears. The additivity argument partly survives, because individual embedding dimensions are not meaningful on their own and the signal is usually spread smoothly across all of them, which still favours models that combine every dimension over ones that threshold a few. Worth testing both rather than assuming.
  • What would make you doubt the linear choice on a text problem?
    Residual structure that additive term weights cannot express. If the errors concentrate where meaning depends on two tokens co-occurring - a negation next to a positive word, a product name next to a complaint verb - per-term weights genuinely cannot represent it. The cheap fix is adding those phrases as explicit terms; changing family is the expensive one and often does not help.

saying these in an interview costs you the question

  • Assumes boosted trees are best for every dataset
  • Thinks one tree split can combine many sparse features
  • Believes more trees substitute for combining many features
  • Blames the algorithm without looking at the representation
  • Feeds raw n-gram columns and metadata to one tree ensemble

context