Concepts / Feature Reduction in Text Data

Feature Reduction in Text Data

Stopwords are common words that frequently appear in text data and are often removed before processing.

  • Programming

Starting With One Experiment

Text data is full of words that appear over and over again, such as articles, prepositions, and common verbs. When a model is trained on text, every distinct word can become a feature. This article follows a single experiment built around that situation. A text dataset was processed to remove stopwords, which are common words that appear frequently in text data and are often discarded before modeling. Once those words were gone, the same hyperparameter search was run again. The question is simple: did removing the stopwords change how well the model performs?

Keep that question in mind as you read. The answer depends on two numbers that the experiment produced: how many features disappeared, and how the best cross-validation score moved. Both numbers need to be read carefully, because a large-looking change can be small in practice, and a small-looking score can still matter.

What a Stopword Is

Definition

A stopword is a common word that frequently appears in text data and is often removed before processing. Stopwords are usually words that carry little on their own for the task at hand because they show up so often across the text.

The reason for removing them is practical. If a word appears in almost every document, it adds a column to the feature set without helping the model tell documents apart. Removing those words shrinks the feature set. A smaller feature set is easier to work with, which is the motivation behind feature reduction in text data.

Tracing a Token List Through Removal

Consider a short sentence that has already been split into tokens: the cat sat on the mat. Suppose the stopword list used in this illustration contains the and on. Removing those two words changes the token list, and the change is easiest to see by comparing the list before and after.

thecatcatsatsatmatonthematkeptkeptkept
Which tokens in the sample sentence are dropped by stopword removal, and what does the token list look like afterward?

Read the diagram from top to bottom. The three tokens that remain, cat, sat, and mat, carry the content words of the sentence. The three removed tokens, the, on, and the, are the common words. Notice that the word the appears twice in the original list and both copies are removed, because stopword removal works on each token, not on each unique word.

Reading the Grid Search Run

Before reading any score, it helps to know what the code in the experiment was doing. A grid search tries a set of candidate hyperparameter values for a model, scores each one using cross-validation, and reports the best score it found. The search in the experiment is the same one run before and after stopword removal, so the two results can be compared directly.

The original workflow defined several names that the search depends on, including the parameter grid and the training data. Those definitions are not shown in the source, so treat the search step itself as the object of study rather than a complete program. What matters for interpretation is the output: one best cross-validation score per run.

Key point

The best cross-validation score is the highest score the search found among the candidate settings it tried. It is a summary of one search, so it should be compared against another run of the same search rather than read as an absolute measure of quality.

In this experiment, the best cross-validation score after removing stopwords was 0.88. The source describes the grid search performance as decreasing slightly after the stopwords were removed. The earlier score is not given in the source pack, so the size of the drop cannot be computed exactly here. What the source supports is the direction and the size of the change, which it calls slight.

Counting the Features Removed

The dataset started with 27,271 features. After the stopwords were removed, the feature count was 26,966. The first step in reading the experiment is to measure how big the change was. The pipeline below shows where the feature count is set in a typical text workflow, which explains why removing stopwords changes that number.

Raw textdocumentsTokenizesplit into tokensLowercasenormalize caseRemove stopwordsdrop common wordsFeature setfeature count is set here
What steps does raw text pass through, and where does the feature count get set?

The order of the first three steps is an illustrative choice for this article. The source pack does not spell out this pipeline. What it does establish is that stopword removal happens before the search is rerun, and that the feature count reported afterward reflects that removal.

Worked example

How many features were removed

The feature count fell from 27,271 to 26,966 after stopword removal. How many features were removed, and what fraction of the original feature set does that represent?

  1. 1
    Subtract the new count from the old count

    The number removed is the starting count minus the ending count: 27,271 minus 26,966 gives 305.

  2. 2
    Compare the removal to the starting total

    Divide the number removed by the original feature count to get the share of features that disappeared.

  3. 3
    Express the share as a percentage

    The share is $\frac{305}{27271} \approx 1.1\%$, so roughly one percent of the feature set was removed.

Result

305 features were removed, about 1.1 percent of the original 27,271. The source describes this as a small share of the total.

What do you think happens?

Before reading the next step, estimate how many features were removed when the count went from 27,271 to 26,966. Is it a few, a few dozen, or a few hundred?

Reveal answer
Answer:

305 features were removed.

The difference between 27,271 and 26,966 is 305. That is about 1.1 percent of the original feature set, which the source treats as a small share.

27,271 features26,966 features305 removedstopword removaldifference
How many features remain after removing the stopword set, and how large is that reduction relative to the total?

Judging Whether the Change Matters

Two observations must be read together. The feature set lost 305 features out of more than 27,000, and the best cross-validation score dropped slightly. The source gives its reasoning in plain terms: the performance decrease was slight and not enough to worry about. Excluding 305 features out of more than 27,000 is unlikely to change performance or interpretability a lot.

The source also notes that this means most, but not all, of the stopwords appeared among the features. In other words, the stopwords were present in the feature set, and removing them shrank it, but they did not account for every feature affected by the change. This is a reminder that the feature count change and the removed stopwords are related but not identical quantities.

Key point

A change is meaningful when it is large relative to the thing being measured and when it moves the outcome you care about. Here the feature change is about one percent, and the score change is slight in the same direction. Both are small, so the source concludes the change is not worth acting on for this dataset.

When a Fixed Stopword List Stops Paying Off

The experiment ends with a conclusion that goes beyond this one dataset. Because the gain from removal was small in both directions, the source concludes that it does not seem worth using a fixed stopword list in this case. The same source also explains where a fixed list is more useful, and the answer depends on dataset size.

Fixed lists are mostly helpful for small datasets. A small dataset may not contain enough information for the model to work out which words are stopwords from the data itself, so a list written in advance supplies that knowledge. With a large dataset, the frequent words are usually visible in the data, so the model or the preprocessing step can identify them without a predefined list.

The source also points to a different approach as an exercise: discarding frequently occurring words. Instead of drawing the words to drop from a list written in advance, this approach determines them from the data. The source text for this exercise is cut off, so this article does not describe its details beyond that contrast.

Mistakes When Reading Stopword Results

  • Common mistake

    Assuming stopword removal always improves the score

    What it looks like

    A reader sees that the feature count dropped and expects the score to rise.

    Why it fails

    In this experiment the best cross-validation score decreased slightly after removal. Removing features does not guarantee a better result.

    Do this instead

    Compare the score before and after the change, and check the direction and size of the difference.

  • Common mistake

    Reading 0.88 as a verdict on its own

    What it looks like

    Treating the score after removal as proof that the model is good or bad.

    Why it fails

    A single best score says little without the earlier result to compare against.

    Do this instead

    Read the score as one half of a before-and-after comparison, alongside the feature count change.

  • Common mistake

    Treating the removed count as the number of stopwords

    What it looks like

    Assuming that 305 removed features means exactly 305 stopwords were in the list.

    Why it fails

    The source notes that most, but not all, of the stopwords appeared among the features, so the two quantities are related but not the same.

    Do this instead

    Keep the feature change and the stopword list as separate things when you report results.

  • Common mistake

    Calling a small change important because it is a real number

    What it looks like

    Reporting a one percent reduction as a major improvement in the workflow.

    Why it fails

    The source judges a 305 feature change out of more than 27,000 as too small to worry about.

    Do this instead

    Judge the change relative to the total and to the outcome you care about.

  • Common mistake

    Using a fixed stopword list on a large dataset without checking which words are frequent in that data

Practice Reading a Feature Count

EASY

A text dataset starts with 4,000 features. After removing a stopword list, 3,960 features remain. Work out how many features were removed and what share of the original set that is. Then decide whether the change is likely to matter, using the same reasoning the source applies to the 27,271 feature experiment.

Hints
  • Subtract the remaining count from the starting count to get the number removed.
  • Divide the number removed by the starting count and express the result as a percentage.
  • Ask whether the removal is small relative to the total, and whether the source would call it not enough to worry about.
MEDIUM

Explain, in two sentences, why a fixed stopword list might be worth using on a small dataset but not on a large one. Use the idea that the model may or may not be able to find the frequent words in the data itself.

Hints
  • Think about how much information a small dataset contains about which words are common.
  • Compare that with what a large dataset shows about its frequent words.

Key Takeaways

  • A stopword is a common word that frequently appears in text data and is often removed before processing, which shrinks the feature set.
  • A grid search tries candidate hyperparameter settings, scores each with cross-validation, and reports the best score; that score must be compared with another run to be interpreted.
  • In the experiment, removing 305 stopwords' worth of features from 27,271 left 26,966, about 1.1 percent of the total, and the best score dropped slightly.
  • Because the change was small, the source concludes a fixed stopword list was not worth using for this dataset.
  • Fixed stopword lists are mostly useful for small datasets; on large datasets the frequent words are usually visible in the data itself.

Related concepts

More concepts starting with F