Concepts / Grid Search for Model Parameters

Grid Search for Model Parameters

The other describes the model, namely how well it scores.

  • Programming

Why Removing Common Words Seems Like a Good Idea

Many text datasets contain words that show up in almost every document. Articles, pronouns, and short connectors are the usual examples. These words often carry little information about what a document is about, so a common practice is to delete them before a model sees the text. The expectation is that a cleaner feature set will help the model.

This article asks a practical question: does that expectation hold up when you measure it on a real model? Answering it well requires reading two different kinds of numbers. One number describes the data, namely how many features the model has to work with. The other describes the model, namely how well it scores. The two are easy to confuse, and the rest of this article keeps them apart.

What do you think happens?

Before reading on: if you delete common words from a text dataset, do you expect the model's best score to rise, fall, or stay the same? Which measurement would you check first?

  • The score rises

  • The score falls

  • The score stays about the same

  • It depends on how many features are removed

Reveal answer
Answer:

The source reports that the best cross-validation score moved slightly down, from a larger value to 0.88, after a stopwords list was applied. Whether that counts as a meaningful change depends on the size of the drop and on how many features were removed.

The source does not report the earlier score as a precise value, so the size of the change is described qualitatively as a small decrease. The first thing to check is the feature count, because the score is only meaningful once you know how the feature set changed.

Where the Reported Score Comes From

The measurement in this article uses a logistic regression model tuned with grid search. Grid search tries a set of candidate parameter values, scores each candidate with cross-validation, and reports the best score it found. The number the script prints is therefore the best cross-validated score across the whole grid, not the score of a single fixed model.

The source snippet that produces this score assumes that X_train, y_train, and param_grid were already defined earlier in the workflow. The excerpt does not show those definitions, so the number you see is only meaningful if you know what data went into the search and which candidate values were in the grid.

Candidate parametervaluesevery value in the gridCross-validated scorescored for each candidateKeep the best scorecompare with the othersfound so farReported best scorethe number the scriptprintstest each onecomparereturn the best
What is the order of steps when grid search tests each parameter combination, and how does it decide which one is best?

Counting Features Before and After Removal

In a text dataset, a feature is one column of the data the model learns from. Usually each column corresponds to a word or word-like token. Removing a stopwords list deletes the columns that match words on the list. Only the features that matched the list are affected; every other column stays as it was.

Before removal27,271 featuresAfter removal26,966 featuresFeatures removed305 featuresdelete matching columnsdifference
Which words are dropped from the vocabulary when the stopwords list is applied, and how many features remain before versus after?

The source describes the reduction in its simplest form. Only one number changed in a meaningful way, and it changed by 305. The source describes this as most, but not all, of the stopwords having appeared in the dataset. Some words on the list never occurred in this data, so they could not remove any features.

Worked example

Checking the feature reduction by hand

The dataset had 27,271 features before the stopwords list was removed and 26,966 after. How many features were removed, and what fraction of the original total does that represent?

  1. 1
    Name the three numbers

    The starting count is 27,271, the remaining count is 26,966, and the difference is the change. Keep these three roles separate.

  2. 2
    Subtract to find the change

    $27{,}271 - 26{,}966 = 305$, so 305 features were removed.

  3. 3
    Compare with the total

    $305 / 27{,}271$ is about 0.011, or roughly 1.1 percent of the original features. That is a small slice of the total.

Result

305 features were removed, about 1.1 percent of the original 27,271. The remaining count is 26,966.

Feature Count Versus Model Performance

A change in feature count and a change in model performance are separate measurements. The feature count is a fact about the data after preprocessing. The cross-validation score is a fact about how a fitted model did on held-out portions of the training data. One can change without the other, so seeing the count drop does not tell you whether the model got better or worse.

Feature count27,271 to 26,966CV scorebest score 0.88Describes the datacolumns the model learnsfromDescribes the modelhow the tuned model scored
How does a change in the number of features differ from a change in cross-validation score, and why can one change without the other?

Interpreting a Small Drop in Score

After the feature set shrank, grid search was run again and reported a best cross-validation score of 0.88. The source describes this as a slight decrease in performance. The source does not report the earlier score as a precise value, so the size of the change is best described qualitatively: the score went down a little, not dramatically.

The source gives two separate reasons the result did not justify keeping the list. First, a drop that small was judged not enough to worry about. Second, removing 305 features from more than 27,000 was unlikely to change either performance or interpretability much. When the performance change is small and the size of the feature change is small, the cost of maintaining the list can outweigh the benefit.

The source also notes when a fixed list is more useful. 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. With a large dataset, the frequency patterns in the data can do much of that work.

Checking Each Checkpoint in Order

When a result surprises you, find the exact step where the outcome departed from your expectation. This experiment has three checkpoints: the feature count before removal, the feature count after removal, and the cross-validation score after the model is refit. Compare each checkpoint with what you predicted.

  1. 1
    Record the feature count before the stopwords list is removed.
  2. 2
    Record the feature count after removal and confirm the difference matches what you expected.
  3. 3
    Refit the model with grid search and record the best cross-validation score.
  4. 4
    Compare each of the three numbers with your prediction, starting with the earliest one that differs.

This framing helps you avoid a common trap, which is to blame the whole pipeline when only one step produced the change. Checking the checkpoints in order tells you which step to investigate.

Letting max_df Decide Which Words Are Too Common

The source offers an alternative to a fixed stopwords list. Instead of naming the words to remove in advance, you can let the data decide which words are too common by using the max_df option of CountVectorizer. This option discards words that appear frequently in the dataset.

python
Notes on the code
  • max_df is the option that discards frequently appearing words
  • 0.5 is an illustrative setting only; check the library documentation for its exact meaning

Because the threshold comes from the data, it does not depend on whether a word happens to be on someone's list. The value 0.5 in this snippet is only an illustrative setting. The source does not specify a value to use, and it does not state how the threshold is expressed. Consult the library documentation for the exact meaning of max_df before choosing a number for a real experiment.

Fixed stopwordslistwords named in advancemax_df optionset in CountVectorizerMembership on thelistdrops only listed wordsFrequency in the datadrops frequently appearingwords
How does max_df decide which words to drop based on document frequency, compared with a fixed list of words?

Mistakes When Reading the Result

  • Common mistake

    Treating the feature count as the model score

    What it looks like

    Seeing 27,271 drop to 26,966 and concluding the model improved.

    Why it fails

    The feature count describes the data. It says nothing direct about how well the fitted model scored.

    Do this instead

    Record the feature count and the cross-validation score as two separate checkpoints.

  • Common mistake

    Ignoring the size of the score change

    What it looks like

    Rejecting the stopwords list because the score fell at all.

    Why it fails

    The source judged a drop to 0.88 as not enough to worry about, given how few features were removed.

    Do this instead

    Compare the size of the score change with the size of the feature change and with the variation across folds.

  • Common mistake

    Assuming every listed stopword is present in the data

    What it looks like

    Expecting a long list to remove a long list of features.

    Why it fails

    Some words on the list did not occur in this dataset, so they could not remove any features.

    Do this instead

    Count the features that actually changed, not the number of words on the list.

  • Common mistake

    Using a fixed list on a large dataset by default

    Why it fails

    The source notes fixed lists are mostly helpful for small datasets, where frequency patterns may not reveal stopwords.

    Do this instead

    Consider max_df for large datasets, and check its documentation before choosing a threshold.

Applying the Checks to a New Result

MEDIUM

A colleague removes a stopwords list from a text dataset. The feature count drops by 305, from 27,271 to 26,966. Grid search then reports a best cross-validation score of 0.88, a slight decrease. Using the three checkpoints, decide whether the list is worth keeping for this dataset, and state which number you based the decision on.

Hints
  • Start by checking the feature count, then the score, then how the two compare in size.
  • Think about whether the dataset is large or small, since the source ties fixed lists to small datasets.
  • Remember that the source judged this specific drop as not enough to worry about.

Summary of the Stopwords Decision

  1. 1
    Grid search tests candidate parameter values, scores each with cross-validation, and reports the best score it found.
  2. 2
    Removing the stopwords list reduced the features from 27,271 to 26,966, so 305 features were removed.
  3. 3
    The best cross-validation score moved slightly down to 0.88, a change the source judged not enough to worry about.
  4. 4
    Feature count and model performance are separate measurements, and one can change without the other.
  5. 5
    The max_df option of CountVectorizer removes frequently appearing words based on the data, as an alternative to a fixed list.

At a glance

  • Grid search reports the best cross-validated score across candidate parameter values, so that number needs to be read alongside the data it came from.
  • A feature count and a cross-validation score measure different things, and a change in one does not establish a change in the other.
  • A small score drop, combined with a small feature reduction, argued against keeping the stopwords list in the source example.
  • Fixed stopwords lists are most useful for small datasets; for large datasets, max_df lets the data decide which frequent words to drop.

Related concepts

More concepts starting with G