CountVectorizer and Bag-of-Words Features
Lemmatization maps inflected forms onto a base form, so the vectorizer produces fewer distinct features.
Testing Whether Lemmatization Helps
Earlier in this chapter you saw that lemmatization maps inflected words onto a shared base form. That idea sounds tidy, but a practical question remains: does it actually make a text classifier better? Answering that question means running an experiment, not just reasoning about word forms. In this article you will commit to a prediction, trace each step of one such experiment, and then check why the outcome looks the way it does.
What do you think happens?
After lemmatization is applied to the text, will CountVectorizer produce more, fewer, or the same number of distinct features?
Reveal answer
Fewer features
Lemmatization collapses different forms of a word into one base form. Each group of related forms then occupies one entry in the vocabulary instead of several, so the vectorizer produces fewer distinct features. The experiment is built to test whether that smaller feature set also changes the model's score.
The experiment compares two versions of the same text representation. One is produced by the standard CountVectorizer. The other is produced after lemmatization has been applied to the text. Both versions are used to train a Logistic Regression model, and the two models are evaluated with the same cross-validation setup. The only thing meant to differ between the two runs is the vocabulary the text was reduced to. Keeping everything else fixed is what lets you attribute a difference in score to the feature change.
How Lemmatization Reshapes the Vocabulary
A vocabulary is the set of distinct words the vectorizer has seen, and each distinct word becomes one feature. Without lemmatization, every surface form is its own entry. Lemmatization changes that by mapping several inflected forms onto a single base form. After the mapping, the vectorizer counts the base form instead of each separate form, so the vocabulary shrinks.
The words in this diagram were chosen for illustration and are not taken from the experiment itself. The principle they show is the one the experiment depends on. Three surface forms of one verb would normally occupy three feature positions. After lemmatization they share one, so the feature set contains fewer distinct entries even though the underlying text has not lost any content words.
Reading a Bag-of-Words Column Map
In a bag-of-words representation, each vocabulary term corresponds to one column, and each document becomes a row. The cell at a given row and column holds how many times that document contains that term. The word order is discarded, which is why the representation is called a bag of words. Because the number of columns equals the number of distinct vocabulary terms, any change to the vocabulary changes the shape of the feature matrix.
The diagram uses an invented four-term vocabulary and an invented document. Column positions here are simply labels for this illustration. What matters is the pattern: each column is one term, and the document's row records a count for each column. If lemmatization merged two of these terms into one base form, the matrix would lose one column, and every row would be counted against the remaining columns.
Why Small Training Sets Feel the Effect
The experiment expects lemmatization to matter most on small datasets, and the reason is a matter of how many examples a model has to separate features that differ only slightly. When the training set is large, the model has plenty of examples to tell apart features that differ only slightly. When the training set is small, extra distinct features give the model more room to fit noise, meaning patterns that happen to appear in the few training documents but do not generalize.
This explains why the source treats merging related features as a kind of regularization. Merging removes some of the room a model has to memorize small, idiosyncratic differences between word forms. On a small dataset that restriction is useful, so the expected benefit is largest there. On a large dataset the same merge may remove distinctions the model could have used well, which is why the effect is not guaranteed to appear everywhere.
Following the Split and Grid Search
To test the small-dataset expectation, the experiment does not train on the full training data. It uses StratifiedShuffleSplit for cross-validation, with only 1 percent of the data as the training portion and the rest as the test portion. Stratification keeps the class proportions in each split similar to the full dataset. The shuffle means each repeated split draws a different random sample. The split is repeated five times, so each parameter value is judged across several small training samples rather than a single one.
The search is a grid search over the regularization strength C of Logistic Regression, with candidate values 0.001, 0.01, 0.1, 1, and 10. The grid search object is built once and then fitted twice: first on the standard CountVectorizer features, then on the lemmatized features. After each fit, the best cross-validation score is printed. Only the feature matrix at the start changes between the two fits. The splitter, the parameter grid, and the scoring are meant to stay the same.
Read the pipeline as a chain in which each stage uses the output of the one before it. A problem at any stage appears later as a strange score. The feature step is the only point where the two runs are meant to differ, so it is the first place to look when the two printed scores do not match expectations.
Judging the Size of the Score Gap
The two best scores in this experiment differ by 0.010 on the score scale. The source describes this as a modest improvement, and that wording deserves care. The phrase says the gap is small in absolute terms. It does not establish that lemmatization always helps. A single comparison on one dataset with one split configuration cannot settle the question, and the result is explicitly tied to the data.
When you judge the effect, ask three questions. Is the gap larger than the variation between repeated splits? Does the feature count actually drop, which shows the lemmatized matrix was built as intended? Does the result hold under a different dataset or split configuration? A modest gap that survives all three checks is a much stronger claim than a modest gap from one run.
Locating Where a Run Went Wrong
Suppose the lemmatized score had come out lower than the baseline, or the feature count had not dropped. The useful question is where the run departed from the plan. The experiment suggests three places to check, in order.
Tracing a Lemmatized Run That Looks Wrong
The lemmatized run reports a score lower than the baseline. Using the experiment's own checkpoints, decide what to inspect first and why.
- 1Check the feature matrix shapes
If the column count did not drop, the lemmatized matrix was probably not built the way you intended. Nothing downstream can be trusted until the feature step is confirmed.
- 2Check the split configuration
The training fraction must be 1 percent for this experiment. Any other fraction changes the experiment entirely, so a score from a different split cannot be compared with the baseline.
- 3Check which fit produced each printed score
The grid search object is reused, and its stored results are replaced on every fit. Confirm that each printed best score came from the fit you think it came from, not from the other run.
The first failure to find is the one that explains the surprise. If the shapes are right and the split is right, the discrepancy is most likely in which fit's output you are reading.
Common Mistakes When Reading the Experiment
Common mistake Treating a 0.010 gap as proof that lemmatization always helps
Why it failsThe result comes from one dataset with one split configuration, and the source states that the result depends on the data.
Do this insteadDescribe the gap as a modest result for this setup, and test it on other data before generalizing.
Common mistake Assuming a smaller feature set automatically produces a higher score
Why it failsThe feature count describes the representation, while the score describes what the model does with those features.
Do this insteadCheck both the shape of the feature matrices and the printed scores, and treat the score as the outcome to explain.
Common mistake Reading a printed score without knowing which fit produced it
Why it failsThe grid search object is reused, and its stored results are replaced on every fit.
Do this insteadMatch each printed line to its fit in order, and label the baseline and lemmatized outputs before comparing them.
Common mistake Comparing results obtained with different training fractions
Why it failsA training fraction other than 1 percent changes the experiment, so the scores no longer test the same question.
Do this insteadConfirm the split configuration before any comparison, and rerun with the intended fraction if it differs.
Practicing the Judgment
A colleague reruns the experiment with a 10 percent training fraction instead of 1 percent. They report that lemmatization changed nothing and conclude the technique is useless. Before accepting that conclusion, which three things from the trace would you check, and why does the change in training fraction matter?
Hints
- Think about whether the scores from the two runs test the same question.
- Check whether the lemmatized feature count actually dropped.
- Ask which fit each printed score came from.
Key Takeaways
- Lemmatization maps inflected forms onto a shared base form, so CountVectorizer produces fewer distinct features and the feature matrix has fewer columns.
- The experiment is expected to show the largest benefit on small training sets, because merging related features reduces the room a model has to fit noise, which acts as a kind of regularization.
- The experiment uses a 1 percent training portion with five stratified shuffled splits and a grid over C, and the reported 0.010 gap is a modest result that depends on the data.
- To locate a surprising result, check the feature matrix shapes first, then the split configuration, then which grid search fit produced each printed score.