When controlling vocabulary size in NLP models, the key metrics are model accuracy and out-of-vocabulary (OOV) rate. Accuracy shows how well the model understands text with the chosen vocabulary. OOV rate tells us how many words in new text are missing from the vocabulary. A smaller vocabulary reduces model size and speeds up training but can increase OOV rate, hurting accuracy. So, balancing these metrics helps find the best vocabulary size.
Vocabulary size control in NLP - Model Metrics & Evaluation
Start learning this pattern below
Jump into concepts and practice - no test required
Suppose we classify text into positive or negative sentiment.
Vocabulary size: 5,000 words
Total samples: 100
Confusion Matrix:
Predicted Positive | Predicted Negative
------------------------------------------
Actual Positive | 40 (TP) | 10 (FN)
Actual Negative | 5 (FP) | 45 (TN)
Precision = TP / (TP + FP) = 40 / (40 + 5) = 0.89
Recall = TP / (TP + FN) = 40 / (40 + 10) = 0.80
F1 Score = 2 * (0.89 * 0.80) / (0.89 + 0.80) = 0.84
If vocabulary size shrinks to 2,000, OOV words increase, causing more errors:
Confusion Matrix:
Predicted Positive | Predicted Negative
------------------------------------------
Actual Positive | 30 (TP) | 20 (FN)
Actual Negative | 10 (FP) | 40 (TN)
Precision = 30 / (30 + 10) = 0.75
Recall = 30 / (30 + 20) = 0.60
F1 Score = 2 * (0.75 * 0.60) / (0.75 + 0.60) = 0.67
Imagine packing a suitcase for a trip. A big suitcase (large vocabulary) lets you bring many clothes (words), so you are ready for anything (better accuracy). But it is heavy and slow to carry (larger model, slower training).
A small suitcase (small vocabulary) is light and fast but may miss important clothes (words), so you might feel unprepared (higher OOV, lower accuracy).
In NLP, choosing vocabulary size balances model speed and memory against understanding new text well.
- Good: Low OOV rate (under 5%), high accuracy (above 85%), balanced precision and recall.
- Bad: High OOV rate (above 15%), low accuracy (below 70%), large gap between precision and recall indicating poor generalization.
Good values mean the vocabulary covers most words the model sees, helping it predict well. Bad values mean many words are unknown, causing mistakes.
- Ignoring OOV rate: High accuracy on training data can hide poor performance on new text with many unknown words.
- Overfitting vocabulary: Using too large vocabulary may memorize training words but fail on new words.
- Data leakage: Including test words in vocabulary inflates accuracy falsely.
- Accuracy paradox: High accuracy with small vocabulary may happen if data is unbalanced, but model misses rare words.
Your NLP model has 98% accuracy but a 20% OOV rate on new text. Is it good for production? Why or why not?
Answer: No, because a 20% OOV rate means many words are unknown to the model. Even with high accuracy on known words, the model will struggle with new or rare words, reducing real-world performance. You should reduce OOV by increasing vocabulary or using subword methods.
Practice
Solution
Step 1: Understand vocabulary size control
Vocabulary size control means setting a limit on how many unique words the model can use.Step 2: Identify the main goal
The goal is to reduce complexity and noise by ignoring very rare words, so the model focuses on common words.Final Answer:
To limit the number of words the model uses -> Option CQuick Check:
Vocabulary size control = limit words [OK]
- Thinking it increases training epochs
- Believing it adds rare words
- Confusing it with stop word removal
Solution
Step 1: Recall CountVectorizer parameters
CountVectorizer has parameters like max_features, min_df, stop_words, and ngram_range.Step 2: Identify parameter for vocabulary size
max_features sets the maximum number of words (features) to keep, controlling vocabulary size.Final Answer:
max_features -> Option AQuick Check:
max_features controls vocabulary size [OK]
- Choosing min_df which filters by document frequency
- Confusing stop_words with vocabulary size
- Thinking ngram_range controls vocabulary size
from sklearn.feature_extraction.text import CountVectorizer texts = ['apple banana apple', 'banana orange', 'apple orange orange'] vectorizer = CountVectorizer(max_features=2) vectorizer.fit(texts) vocab = vectorizer.get_feature_names_out() print(len(vocab))
Solution
Step 1: Understand max_features effect
max_features=2 means the vectorizer keeps only the top 2 most frequent words.Step 2: Count unique words and frequencies
Words: apple(3), banana(2), orange(3). Top 2 are apple and orange.Final Answer:
2 -> Option BQuick Check:
max_features=2 means vocabulary size = 2 [OK]
- Counting all unique words ignoring max_features
- Assuming max_features is minimum count
- Confusing frequency with vocabulary size
from sklearn.feature_extraction.text import CountVectorizer texts = ['cat dog', 'dog mouse', 'cat mouse'] vectorizer = CountVectorizer(max_features='3') vectorizer.fit(texts) vocab = vectorizer.get_feature_names_out() print(vocab)
Solution
Step 1: Check max_features type
max_features expects an integer, but '3' is a string, causing a type error.Step 2: Confirm other parts are correct
fit() works fine, get_feature_names_out() is current method, texts can be list.Final Answer:
max_features should be an integer, not a string -> Option AQuick Check:
max_features type must be int [OK]
- Using string instead of integer for max_features
- Thinking fit_transform is required here
- Believing get_feature_names_out is deprecated
Solution
Step 1: Understand problem with large vocabulary
100,000 words is large and slows training; many words may be rare and noisy.Step 2: Choose best vocabulary control method
Setting max_features to a smaller number like 5000 keeps common words and speeds training.Final Answer:
Set max_features to a smaller number like 5000 in your vectorizer -> Option DQuick Check:
Limit vocabulary size to speed training [OK]
- Using all words causing slow training
- Only removing stop words without size control
- Increasing max_features unnecessarily
