How do I use sklearn CountVectorizer with both 'word' and 'char' analyzer? - python
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
One CountVectorizer instance can use only one analyzer at a time. If you want both word-level and character-level features from the same text, the normal solution is to build two vectorizers and combine their outputs.
Why One Vectorizer Is Not Enough
CountVectorizer has an analyzer parameter, but it accepts one mode per instance:
- '
word' - '
char' - '
char_wb'
That means this is not possible in one object:
Instead, create one vectorizer for words and another for character n-grams.
Combine Two Vectorizers with FeatureUnion
Scikit-learn's FeatureUnion is the standard tool for combining multiple feature extractors that operate on the same input.
The result is one sparse matrix whose columns include both word n-grams and character n-grams.
Why This Is Useful
Word features capture meaning at the token level, while character features capture spelling patterns, prefixes, suffixes, and resilience to small typos.
That combination is often helpful when:
- text contains misspellings or noisy user input
- morphology matters
- you want both semantic and subword information
It is a common pattern in text classification tasks.
Add the Combined Features to a Classifier Pipeline
You can feed the union directly into a classifier:
This keeps the preprocessing and model together in one reusable object.
Consider char_wb as a Variation
If plain character n-grams feel too noisy, char_wb can be a good compromise. It creates character n-grams inside word boundaries, which often reduces cross-word artifacts.
You can swap the character branch easily:
That small change can matter in short-text classification or language-identification tasks.
It is especially useful when you want character features that stay inside individual words instead of creating n-grams that cross whitespace boundaries.
That often produces a cleaner feature space for short texts, product names, and typo-prone user input.
Watch the Feature Space Size
Combining word and character analyzers increases dimensionality quickly. That is not automatically bad, but it does mean:
- more memory usage
- larger sparse matrices
- potentially slower model training
Practical controls include min_df, max_features, and narrower n-gram ranges.
Common Pitfalls
The biggest pitfall is trying to force both analyzers into one CountVectorizer. The API is not designed that way.
Another issue is converting the combined sparse matrix to dense form with .toarray() on large datasets. That can explode memory usage immediately.
People also underestimate how large character n-gram vocabularies become. Start with a narrow range and tune from there instead of enabling everything at once.
Summary
- One
CountVectorizercan use only one analyzer setting at a time. - To use both word and character features, build two vectorizers and combine them with
FeatureUnion. - The combined output is a single sparse matrix suitable for normal scikit-learn pipelines.
- '
char_wbis worth considering when raw character n-grams are too noisy.' - Control vocabulary growth with sensible n-gram ranges and feature limits.
Related reading
- How do you remove all the alphabetic characters from a string?
- How do you write a program to find if certain words are similar?
- How does Apple find dates, times and addresses in emails?
- How does Beam Search operate on the output of The Transformer?
- How do I use TensorFlow GPU?
- How do I use the group_by_window function in TensorFlow
- How do I use threading in Python?
- How do I use threading in Python?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.