Measuring Model Performance on Text Features
A pre-defined stopwords list, such as scikit-learn's ENGLISH_STOP_WORDS with 318 entries, is a library-supplied collection of common words that text processing usually ignores.
Why Common Words Crowd the Feature Matrix
When a computer turns a document into numbers, every distinct word it sees can become a feature. Words such as 'the', 'and', 'is', and 'of' appear in almost every English document. Because they show up everywhere, they tell a model very little about what any single document is about. They still occupy a column of the feature matrix, though, and they make the representation larger than it needs to be.
This article looks at a practical response to that problem: a pre-defined stopwords list. You will see what such a list contains, how it is passed to CountVectorizer, and what changes in the resulting features. Before reading each explanation, try to trace the code in your head and decide what you expect each stage to produce. Then compare your expectation with what the code actually does.
What do you think happens?
If you add stop_words='english' to a CountVectorizer, can the number of features go up?
Reveal answer
No, the count can only stay the same or go down.
Removing stopwords is a filter applied to candidate words. It never adds a word to the vocabulary, so the number of features can only shrink or remain unchanged. Whether a downstream model actually performs better is a separate question that must be measured.
What a Pre-defined Stopwords List Holds
A pre-defined stopwords list, such as scikit-learn's ENGLISH_STOP_WORDS with 318 entries, is a library-supplied collection of common words that text processing usually ignores.
The scikit-learn library provides this list in its feature extraction text module, under the name ENGLISH_STOP_WORDS. You can import it directly and ask how many entries it holds. The source reports that this list contains 318 stopwords. The list is set-like rather than an ordered sequence, so the source converts it to a list before slicing it. The slice in the source takes every tenth entry, a quick way to sample the contents without printing all 318 words.
- Import the text module that holds the built-in list
- len counts the entries; the source reports 318
- The set is converted to a list so it can be sliced by position
- The slice [::10] keeps every tenth entry as a sample
The sampled words look ordinary. The source mentions pronouns such as 'he' and 'me', the determiner 'the', and connecting words such as 'although' and 'into'. The sample contains no subject-specific vocabulary. The list is built around words that carry grammatical function rather than meaning about a particular topic, which is why it is a generic starting point for English text. Because it is generic, it may not suit every corpus, and that is one reason the effect of using it has to be checked.
Tracing a Document Through the Stopword Step
It helps to see the stopword step as one stage in a sequence rather than as a standalone operation. Text is first split into tokens. The stopword filter then removes the tokens that appear in the list. The remaining tokens form the vocabulary, and the count matrix records how often each vocabulary item appears in each document. This is a simplified conceptual model. Its purpose is to show where the stopword decision sits, not to describe every internal detail.
Notice that the removal is applied to the words of the text, not to the documents themselves. Each document still exists, just with fewer candidate features. The changed words are the ones that were dropped, and the remaining words keep their order as they become candidates for the vocabulary.
Passing stop_words to CountVectorizer
CountVectorizer transforms text into a matrix of token counts. It accepts a stop_words parameter. When you set that parameter to the string 'english', the vectorizer uses the built-in list. The parameter is given when the object is created, and the vocabulary is then learned when fit is called on the training text. The transform step that follows fit uses the vocabulary and stopword handling that were set up earlier.
- The
stop_wordssetting is supplied in the constructor, before fit is called - The first vectorizer keeps every word it sees
- The second vectorizer applies the built-in list while fitting
- vocabulary_ holds the words learned by fit
Worked Example: Dropping 'the' and 'was'
The source gives a small illustration. A document contains the words 'the', 'movie', 'was', and 'fun', and the list contains 'the' and 'was'. The vectorizer would not treat 'the' and 'was' as features, so the document is represented only by the words that remain. The example below applies the same idea to two short sentences.
Counting features with and without stopwords
The corpus is ['The movie was fun', 'The movie was dull']. Predict the number of vocabulary words with no stop_words setting, then with stop_words='english', and explain the difference.
- 1Collect the distinct words
Across both sentences the distinct words are 'the', 'movie', 'was', 'fun', and 'dull'. That is 5 candidate features with no filtering.
- 2Identify the stopwords
The built-in list contains 'the' and 'was'. Those two words are dropped as candidates.
- 3Count what remains
The remaining distinct words are 'movie', 'fun', and 'dull'. That is 3 features.
- 4Check the direction of change
The count moved from 5 to 3. It fell by 2, which is the number of list words that actually appeared in the corpus. It did not rise.
Without stopwords the vocabulary has 5 features. With stop_words='english' it has 3. The two dropped words are 'the' and 'was'.
Why Removal Can Only Shrink the Feature Count
The logic behind the worked example is simple. Removing stopwords is a filter applied to candidate words. A filter can keep or discard a word, but it cannot introduce one that was not there. So the feature count can only shrink or stay the same. The most it can drop is by the length of the list, which is 318 for ENGLISH_STOP_WORDS.
Mistakes Beginners Make with Stopwords
Common mistake Expecting the stopword setting to add features
Why it failsThe stopword list only removes candidate words. It never creates new columns in the matrix.
Do this insteadExpect the count to stay the same or fall when stop_words is set.
Common mistake Setting stop_words after fitting
What it looks likevect = CountVectorizer(); vect.fit(docs); then trying to change the setting on the already-fitted object and expecting the vocabulary to update.Why it failsThe setting is supplied when the vectorizer is constructed and is used during fitting. The vocabulary was already learned with the old setting.
Do this insteadCreate the vectorizer with stop_words='english' and then call fit.
Common mistake Assuming the list fits every corpus
Why it failsThe list is a generic set of words with grammatical function. A domain-specific corpus may contain words that matter for the task.
Do this insteadCheck the resulting vocabulary and test the model's performance rather than trusting the list blindly.
Common mistake Comparing two runs that differ in more than the stopword setting, which makes any change in results hard to attribute
Practice: Predicting the Feature Count
Consider the corpus ['The cat sat on the mat', 'The dog sat on the rug']. Before running any code, predict the vocabulary size with no stop_words setting and then with stop_words='english'. Name the stage where a surprising result would most likely originate if your prediction turned out wrong.
Hints
- List the distinct words across both sentences before applying any filter.
- Check which of those words appear in the built-in list, and remember the list is generic English function words.
- A filter can only remove candidates; it cannot add any.
Key Takeaways from the Stopword Workflow
- 1A pre-defined stopwords list such as ENGLISH_STOP_WORDS is a library-supplied collection of common words that text processing usually ignores; the scikit-learn list holds 318 entries.
- 2Passing
stop_words='english' to CountVectorizer applies the built-in list during fitting, and the setting is chosen when the vectorizer is constructed. - 3Removing stopwords is a filter, so the feature count can only shrink or stay the same, and any gain in model performance must be measured.
- 4When a result looks wrong, locate the stage (constructor, fit, transform, or comparison) where the unexpected value first appears.
At a glance
- A pre-defined stopwords list such as ENGLISH_STOP_WORDS is a library-supplied collection of common words that text processing usually ignores; the scikit-learn list holds 318 entries.
- Passing
stop_words='english' to CountVectorizer applies the built-in list during fitting, and the setting is chosen when the vectorizer is constructed. - Removing stopwords is a filter, so the feature count can only shrink or stay the same, and any gain in model performance must be measured.
- When a result looks wrong, locate the stage (constructor, fit, transform, or comparison) where the unexpected value first appears.