Bird
Raised Fist0
NLPml~10 mins

Vocabulary size control in NLP - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to limit the vocabulary size when tokenizing text.

NLP
from sklearn.feature_extraction.text import CountVectorizer
vectorizer = CountVectorizer(max_features=[1])
texts = ['I love machine learning', 'AI is the future']
X = vectorizer.fit_transform(texts)
Drag options to blanks, or click blank then click option'
A1000
B0
CNone
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using None or negative numbers which do not limit vocabulary size.
Setting max_features to 0 which disables vocabulary.
2fill in blank
medium

Complete the code to remove rare words by setting a minimum document frequency.

NLP
from sklearn.feature_extraction.text import CountVectorizer
vectorizer = CountVectorizer(min_df=[1])
texts = ['apple banana apple', 'banana orange', 'apple orange orange']
X = vectorizer.fit_transform(texts)
Drag options to blanks, or click blank then click option'
A2
B0.5
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using integer values without understanding they mean absolute counts.
Setting min_df to 0 which keeps all words.
3fill in blank
hard

Fix the error in the code to correctly limit vocabulary size and remove rare words.

NLP
from sklearn.feature_extraction.text import CountVectorizer
vectorizer = CountVectorizer(max_features=[1], min_df=[2])
texts = ['cat dog cat', 'dog mouse', 'cat mouse mouse']
X = vectorizer.fit_transform(texts)
Drag options to blanks, or click blank then click option'
A2
B0.5
C1
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Setting min_df too high removes all words.
Using invalid types for max_features or min_df.
4fill in blank
hard

Fill both blanks to create a vocabulary of words appearing in at least 2 documents and limit size to 3.

NLP
from sklearn.feature_extraction.text import CountVectorizer
vectorizer = CountVectorizer(min_df=[1], max_features=[2])
texts = ['sun moon sun', 'moon star', 'sun star star']
X = vectorizer.fit_transform(texts)
Drag options to blanks, or click blank then click option'
A2
B3
C1
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping min_df and max_features values.
Using values that remove all words.
5fill in blank
hard

Fill all three blanks to create a vocabulary with max size 4, min document frequency 2, and max document frequency 0.8.

NLP
from sklearn.feature_extraction.text import CountVectorizer
vectorizer = CountVectorizer(max_features=[1], min_df=[2], max_df=[3])
texts = ['apple banana apple', 'banana orange apple', 'apple orange banana', 'banana apple orange']
X = vectorizer.fit_transform(texts)
Drag options to blanks, or click blank then click option'
A4
B2
C0.8
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using max_df greater than 1 or less than 0.
Setting min_df higher than max_df causes empty vocabulary.

Practice

(1/5)
1. What is the main purpose of controlling vocabulary size in NLP models?
easy
A. To add more rare words to the dataset
B. To increase the number of training epochs
C. To limit the number of words the model uses
D. To make the model ignore stop words

Solution

  1. Step 1: Understand vocabulary size control

    Vocabulary size control means setting a limit on how many unique words the model can use.
  2. 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.
  3. Final Answer:

    To limit the number of words the model uses -> Option C
  4. Quick Check:

    Vocabulary size control = limit words [OK]
Hint: Vocabulary size control means limiting words used [OK]
Common Mistakes:
  • Thinking it increases training epochs
  • Believing it adds rare words
  • Confusing it with stop word removal
2. Which parameter in scikit-learn's CountVectorizer controls the vocabulary size?
easy
A. max_features
B. min_df
C. stop_words
D. ngram_range

Solution

  1. Step 1: Recall CountVectorizer parameters

    CountVectorizer has parameters like max_features, min_df, stop_words, and ngram_range.
  2. Step 2: Identify parameter for vocabulary size

    max_features sets the maximum number of words (features) to keep, controlling vocabulary size.
  3. Final Answer:

    max_features -> Option A
  4. Quick Check:

    max_features controls vocabulary size [OK]
Hint: max_features sets max vocabulary size in vectorizers [OK]
Common Mistakes:
  • Choosing min_df which filters by document frequency
  • Confusing stop_words with vocabulary size
  • Thinking ngram_range controls vocabulary size
3. What will be the output vocabulary size after running this code?
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))
medium
A. 3
B. 2
C. 4
D. 1

Solution

  1. Step 1: Understand max_features effect

    max_features=2 means the vectorizer keeps only the top 2 most frequent words.
  2. Step 2: Count unique words and frequencies

    Words: apple(3), banana(2), orange(3). Top 2 are apple and orange.
  3. Final Answer:

    2 -> Option B
  4. Quick Check:

    max_features=2 means vocabulary size = 2 [OK]
Hint: max_features limits vocabulary count to given number [OK]
Common Mistakes:
  • Counting all unique words ignoring max_features
  • Assuming max_features is minimum count
  • Confusing frequency with vocabulary size
4. Identify the error in this code snippet that tries to limit 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)
medium
A. max_features should be an integer, not a string
B. fit() should be replaced with fit_transform()
C. get_feature_names_out() is deprecated
D. texts should be a numpy array

Solution

  1. Step 1: Check max_features type

    max_features expects an integer, but '3' is a string, causing a type error.
  2. Step 2: Confirm other parts are correct

    fit() works fine, get_feature_names_out() is current method, texts can be list.
  3. Final Answer:

    max_features should be an integer, not a string -> Option A
  4. Quick Check:

    max_features type must be int [OK]
Hint: max_features must be int, not string [OK]
Common Mistakes:
  • Using string instead of integer for max_features
  • Thinking fit_transform is required here
  • Believing get_feature_names_out is deprecated
5. You want to build a text classifier but your dataset has 100,000 unique words. To speed up training and reduce noise, which approach best controls vocabulary size?
hard
A. Increase max_features to 200,000 to include more words
B. Use all 100,000 words to keep maximum information
C. Remove stop words only without limiting vocabulary size
D. Set max_features to a smaller number like 5000 in your vectorizer

Solution

  1. Step 1: Understand problem with large vocabulary

    100,000 words is large and slows training; many words may be rare and noisy.
  2. Step 2: Choose best vocabulary control method

    Setting max_features to a smaller number like 5000 keeps common words and speeds training.
  3. Final Answer:

    Set max_features to a smaller number like 5000 in your vectorizer -> Option D
  4. Quick Check:

    Limit vocabulary size to speed training [OK]
Hint: Limit vocabulary size to speed training and reduce noise [OK]
Common Mistakes:
  • Using all words causing slow training
  • Only removing stop words without size control
  • Increasing max_features unnecessarily