Concepts / Stemming and Lemmatization Compared

Stemming and Lemmatization Compared

Lemmatization reduces words to their base or root form, and in the source's example it cut the feature count from 27,271 to 21,596 while the number of documents stayed at 25,000.

  • Programming

Predicting the Shape Before Running Anything

This article starts with a prediction rather than a definition. Two text representations are built from the same training data, and each one reports its shape. A shape has two numbers: the first counts rows, which are documents, and the second counts columns, which are features. Before you read any output, decide what you expect each number to be for the lemmatized representation compared with the standard one.

What do you think happens?

When lemmatization is applied to the training text, what happens to the number of rows and the number of columns compared with the standard CountVectorizer?

Reveal answer
Answer:

The number of columns drops, from 27,271 features to 21,596 features, while the number of rows stays at 25,000 documents.

Lemmatization maps several surface forms of a word to one base form, so fewer distinct vocabulary columns are needed. Each document is still one row, so the row count cannot change.

Reading the Shape Output

A shape line is the fastest check on any vectorizer. The first number tells you how many documents went in, and the second tells you how many vocabulary columns the vectorizer learned. Because the vectorizer creates one column per vocabulary term, the second number is a direct measure of how many distinct terms the representation keeps track of. When you change the text processing, the shape is the first thing to inspect, and it tells you whether the change did what you intended.

python
Notes on the code
  • fit_transform learns the vocabulary from the text it receives and returns the document-by-feature matrix
  • The lemmatized matrix is produced from lemma_text, which holds the lemmatized training documents
  • Printing shape reports (rows, columns), so the column count is the feature count to compare

How Lemmatization Collapses Columns

In a bag-of-words matrix, each column counts how often one vocabulary term appears in each document. If two different surface forms of the same word are both counted, they occupy two separate columns, and the model must learn from each column on its own. Lemmatization reduces words to their base or root form. When several surface forms map to one base form, their counts land in one shared column. The matrix ends up with fewer columns, and each remaining column pools evidence from several surface forms.

runningsurface formrunone shared columnrunssurface formransurface formmaps tomaps tomaps to
Which surface word forms map to the same base form, so that their counts share one column?

The three forms in the diagram are an illustration chosen for this article, not a figure taken from the source's dataset. The mechanism is the same for any group of forms that share a base form: each form that used to own a column now contributes its counts to the single base column.

The Arithmetic of the Feature Reduction

The source gives exact numbers for the effect. The standard representation has 27,271 features, and the lemmatized representation has 21,596. The difference is 5,675 features. Expressed as a share of the original columns, 5,675 out of 27,271 is roughly 21 percent. The number of documents stayed at 25,000 in both cases, so the entire change is in the columns.

Before

standard CountVectorizer: 25,000 rows x 27,271 features

27,271 feature columns, one per distinct surface form

After

lemmatized vectorizer: 25,000 rows x 21,596 features

21,596 feature columns, forms with a shared base merged

25,000 documents25,000 documents27,271 featuresstandard CountVectorizer21,596 featureslemmatized vectorizerlemmatization
How does the vocabulary size change between the standard vectorizer and the lemmatized one, while the document count stays fixed?

Why Fewer Columns Can Act as Regularization

A smaller feature set is not automatically a better model. The source gives a specific reason to expect a benefit: because lemmatization conflates some features, it works like regularization. Regularization limits how much freedom a model has to fit fine details of the training data. When two forms that carry nearly the same meaning are merged, the model has fewer separate weights to estimate, and it cannot assign a different importance to each surface form unless the data supports that.

This is why the source expects the benefit to be largest when the dataset is small. With little training data, a model with many sparse columns has more opportunities to latch onto accidental patterns, such as a rare form that happens to appear only in one class. Each column has only a few documents supporting it. Merging columns pools those few documents across several forms, so each remaining column rests on more evidence. That reduces the risk of fitting noise.

The 1 Percent Cross-Validation Experiment

To test the small-dataset expectation, the source does not use the full 25,000-document training set. It builds a cross-validation experiment that uses only 1 percent of the data as training data and the rest as test data. The splitter is StratifiedShuffleSplit, and the search over the regularization strength C is run with GridSearchCV around LogisticRegression. The comparison uses five random splits.

RepresentationMean cross-validation scoreGap to the other
Standard CountVectorizer0.721baseline
Lemmatized representation0.731+0.010 higher

Scores from the 1 percent training experiment reported by the source

The gain is a difference of 0.010 on the score scale. Relative to the standard score of 0.721, that is about 1.4 percent. The source calls this a modest improvement. It was observed in this specific setup, with 1 percent training data and five random splits, so it is evidence for that experiment rather than a general promise. The source pack reports the two averages but does not give per-fold scores, so the spread across individual splits cannot be read from it.

The source also notes that lemmatization and stemming can sometimes produce a better model, or at least a more compact one. These are two different wins. A representation can be smaller without being more accurate, and a small accuracy gain can still justify a compact vocabulary. When you report a result, state which of the two you observed.

Finding Where the Unexpected Result Appears

Suppose you run the first snippet and expect a smaller matrix, but the lemmatized shape prints 27,271 columns. Do not change the model or the search yet. Work through the pipeline in order and compare each stage's output against what you expected. The chain runs from raw text, through lemmatization and vectorizing, to fitting and cross-validating. Each stage hands its output to the next, so an error at one stage will carry forward into everything after it.

Training text25,000 documentsLemmatized textbase forms expectedlemma_vect.fit_transformfirst shape lineGridSearchCV over CLogisticRegression, 1percent trainCross-validationscore0.731 vs 0.721CountVectorizer.fit_transformstandard shape linelemmatizeno lemmatizingdocumentsmatrixmatrixscores
Which step in the comparison pipeline first produces the unexpected result, and what data reaches that step?

The first print line already tells you where to look. It was produced by lemma_vect.fit_transform, so the problem lies in that object or in the text it was given, not in the standard CountVectorizer line that follows. Check whether the text passed in was actually lemmatized, and whether the vectorizer was built on that processed text. Only after the shapes agree with your expectations should you move on to the search and the scores.

Mistakes When Reading the Comparison

  • Common mistake

    Treating a smaller feature count as proof of a better model

    Why it fails

    Fewer columns only shows that the vocabulary was compressed. The source ties any benefit to regularization and then measures it, rather than assuming it.

    Do this instead

    Report the feature count and the score separately, and treat the score as the test of accuracy.

  • Common mistake

    Assuming the 0.010 gain applies to every dataset

    Why it fails

    The source says the result varies by dataset and was observed in one setup of 1 percent training data across five random splits.

    Do this instead

    Rerun the same comparison on the new corpus before claiming a gain.

  • Common mistake

    Expecting the document count to change

    Why it fails

    Lemmatization changes the vocabulary, not the set of documents. The source's rows stayed at 25,000 while the columns dropped.

    Do this instead

    If the row count changes, look for a data-loading or filtering problem, not a lemmatization effect.

  • Common mistake

    Changing the model when the shapes are already wrong

    Why it fails

    A wrong matrix feeds every later stage, so tuning C cannot repair a text input that was never lemmatized.

    Do this instead

    Check the shape output of each vectorizer, in pipeline order, before touching the model or the search.

Practice Predicting the Comparison Outcome

MEDIUM

A colleague lemmatizes a corpus of 25,000 documents and reports a shape of (25,000, 18,400) against a standard shape of (25,000, 27,271). Using only the source's logic, state what the colleague's result confirms, what it does not confirm, and which stage you would inspect first if the shape had instead printed (24,000, 18,400).

Hints
  • Compare the second number of each shape first, because that is the feature count.
  • Rows should match when only the vocabulary changes; a changed row count points to the input data, not the lemmatizer.
  • The score is a separate question; the shape alone cannot tell you whether accuracy improved.

Key Takeaways

  • Lemmatization maps several surface forms of a word to one base form, so their counts share one column; in the source it reduced features from 27,271 to 21,596 while the 25,000 documents stayed the same.
  • The source expects lemmatization to act as regularization by conflating features, which limits how freely the model fits details and should matter most when the training set is small.
  • In the 1 percent training experiment, the lemmatized representation scored 0.731 against 0.721 for the standard CountVectorizer, a modest gain of 0.010 that the source says depends on the dataset.
  • A compact representation and an accurate one are different wins, so report which you observed.
  • When a comparison looks wrong, check shapes stage by stage in pipeline order before changing the model or the search.

Related concepts

More concepts starting with S