Feature selection using bigram
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Bigrams can improve text models because they capture short phrase context that unigrams miss, such as not good or credit card. The downside is that bigrams increase feature count quickly, so feature selection becomes essential if you want a model that is both accurate and efficient.
Why bigrams help
A unigram model treats words independently. That is often enough for topic classification, but it can lose important local structure. Bigrams help recover some of that structure.
Examples:
- '
new yorkmeans more thannewplusyork' - '
not badoften reverses the sentiment ofbad' - '
machine learningis a phrase, not just two unrelated tokens'
That extra context is why bigrams are commonly used in sentiment analysis, topic labeling, and document classification.
Generate bigram features first
In scikit-learn, the simplest way to include bigrams is through CountVectorizer or TfidfVectorizer with ngram_range=(1, 2) or (2, 2).
This creates unigram and bigram features. If you want only bigrams, set ngram_range=(2, 2).
Then reduce the feature space
Once bigrams are generated, select features with a method that matches the task.
Common options:
- frequency thresholds such as
min_df - chi-squared selection for classification
- mutual information
- model-based sparsity such as L1 regularization
The fastest first filter is usually not a formal selector at all. It is just vocabulary pruning through min_df and max_df.
This removes rare phrases that are probably noise and extremely common phrases that add little discrimination.
Use chi-squared to keep the most informative bigrams
For text classification with nonnegative features, chi-squared is a practical selector.
This keeps only the highest-scoring features after vectorization, which is often enough to tame a noisy bigram space.
Inspect which phrases survive
Feature selection is easier to trust when you inspect the retained terms.
This is especially useful when reviewing whether the model is picking meaningful phrases or just artifacts from the dataset.
Bigrams are useful, but not always necessary
Do not assume bigrams automatically improve every task. They help most when phrase meaning matters. For some problems, a unigram model with solid preprocessing performs just as well and is easier to deploy.
A practical workflow is:
- build a unigram baseline
- add bigrams
- prune features
- compare validation metrics and model size
If the bigram model is only slightly better but much larger, the simpler baseline may be the better production choice.
Common Pitfalls
The most common mistake is enabling bigrams without pruning the vocabulary, which creates a huge sparse matrix and slows everything down. Another frequent issue is selecting features on the full dataset before the train and validation split, which leaks information and inflates metrics. Teams also sometimes assume every high-scoring bigram is semantically meaningful, when some are just dataset artifacts. Using only rare bigrams is another problem because they often look informative in training but fail to generalize. Finally, people often skip a unigram baseline, so they cannot tell whether bigrams actually helped.
Summary
- Bigrams capture short phrase context that unigrams often miss.
- Generate them with
CountVectorizerorTfidfVectorizerusing n-gram settings. - Control feature explosion early with
min_df,max_df, and vocabulary pruning. - Use selectors such as chi-squared to keep the most informative terms.
- Inspect the retained phrases instead of treating feature selection as a black box.
- Always compare against a unigram baseline before deciding the extra complexity is worth it.

