How to increase weight of a word for CountVectorizer
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
CountVectorizer from scikit-learn converts text into a matrix of token counts where each word gets equal weight. To increase the importance of specific words, you can multiply their columns in the count matrix by a weight factor, use TfidfVectorizer (which automatically downweights common words and upweights rare ones), create a custom vocabulary with repeated tokens, or build a custom transformer. The right approach depends on whether you want domain-specific boosting or automatic importance weighting.
Basic CountVectorizer
Every word gets a count of how many times it appears. "learning" appearing twice in the third document gets a count of 2, but all words are treated equally in terms of importance.
Method 1: Multiply Specific Columns by a Weight
This approach directly scales specific feature columns. It is the most straightforward way to boost known important terms.
Method 2: Use TfidfVectorizer Instead
TfidfVectorizer computes Term Frequency-Inverse Document Frequency. Words that appear in many documents (like "the") get low weight, while words unique to few documents get high weight. This is the standard approach for automatic importance weighting.
Method 3: Custom Transformer with Weights
A custom transformer integrates into scikit-learn pipelines and applies weights consistently during both training and prediction.
Method 4: Using Pipeline with Custom Weights
Method 5: Vocabulary Duplication Trick
Repeating words in the input text increases their count in the feature matrix. This is a hack — prefer the column-weighting approach for production code.
Common Pitfalls
- Applying weights after fitting the model: Weights must be applied during both training and prediction. If you weight the training data but not the test data, the model sees different feature distributions and performs poorly.
- Choosing arbitrary weight values: Weight factors should be validated via cross-validation. Boosting a word by 10x may cause overfitting. Start with small multipliers (2-3x) and tune on validation data.
- Using CountVectorizer when TF-IDF suffices: If your goal is to reduce the impact of common words and boost rare ones,
TfidfVectorizerdoes this automatically without manual weight tuning. - Vocabulary duplication changing model semantics: Repeating words in the text changes the total token count per document, which affects normalization. The column-weighting approach preserves the original document structure.
- Not accounting for word weights in feature importance: If you weight columns, feature importance scores from the model reflect the weighted values, not the raw counts. Document the weights applied so model interpretation remains valid.
Summary
- Multiply specific columns in the count matrix by a weight factor for direct control
- Use
TfidfVectorizerfor automatic importance weighting based on document frequency - Build a custom
WeightedCountVectorizertransformer for scikit-learn pipeline integration - Apply weights consistently during both training and prediction to avoid data leakage
- Validate weight choices with cross-validation rather than using arbitrary multipliers
- Prefer column weighting over vocabulary duplication for cleaner, more maintainable code
Related reading
- How to initialize word-embeddings for Out of Vocabulary Word?
- How to load the saved tokenizer from pretrained model
- How to make the tensorflow hub embeddings servable using tensorflow serving?
- How to make use of pre-trained word embeddings when training a model in sklearn?
- how to increment matrix element in tensorflow using tf.scatter_add?
- How to index a list with a TensorFlow tensor?
- How to Merge Numerical and Embedding Sequential Models to treat categories in \`RNN\`
- How to Merge Numerical and Embedding Sequential Models to treat categories in `RNN`
.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 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.