Bird
Raised Fist0
NLPml~20 mins

Bag of Words (CountVectorizer) in NLP - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Bag of Words Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of CountVectorizer on simple text
What is the output of the following code snippet using CountVectorizer from scikit-learn?
NLP
from sklearn.feature_extraction.text import CountVectorizer
corpus = ['apple banana apple', 'banana orange']
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(corpus)
result = X.toarray()
vocab = vectorizer.get_feature_names_out()
print(vocab)
print(result)
A['apple' 'orange' 'banana']\n[[2 0 1]\n [0 1 1]]
B['banana' 'apple' 'orange']\n[[1 2 0]\n [1 0 1]]
C['apple' 'banana' 'orange']\n[[2 1 0]\n [0 1 1]]
D['apple' 'banana' 'orange']\n[[1 2 0]\n [0 1 1]]
Attempts:
2 left
💡 Hint
CountVectorizer sorts vocabulary alphabetically and counts word occurrences per document.
🧠 Conceptual
intermediate
1:30remaining
Understanding vocabulary size in CountVectorizer
Given the corpus: ['cat dog', 'dog mouse', 'cat mouse dog'], what is the vocabulary size created by CountVectorizer with default settings?
A3
B5
C4
D2
Attempts:
2 left
💡 Hint
CountVectorizer creates one vocabulary word per unique token across all documents.
Hyperparameter
advanced
2:00remaining
Effect of stop_words parameter in CountVectorizer
What will be the vocabulary output of CountVectorizer when applied to ['the cat sat', 'the dog barked'] with stop_words='english'?
A['the', 'cat', 'sat', 'dog', 'barked']
B['barked', 'cat', 'dog', 'sat']
C['cat', 'dog']
D['the']
Attempts:
2 left
💡 Hint
The stop_words='english' removes common English words like 'the'.
Metrics
advanced
1:30remaining
Calculating document frequency with CountVectorizer
Using CountVectorizer on ['apple apple banana', 'banana orange', 'apple orange orange'], what is the document frequency (number of documents containing the word) for 'apple'?
A2
B3
C1
D0
Attempts:
2 left
💡 Hint
Document frequency counts in how many documents the word appears at least once.
🔧 Debug
expert
2:00remaining
Identifying error in CountVectorizer usage
What error will the following code raise? from sklearn.feature_extraction.text import CountVectorizer corpus = ['hello world', 123, 'hello'] vectorizer = CountVectorizer() X = vectorizer.fit_transform(corpus)
AAttributeError: 'int' object has no attribute 'lower'
BValueError: empty vocabulary; perhaps the documents only contain stop words
CNo error, code runs successfully
DTypeError: expected string or bytes-like object
Attempts:
2 left
💡 Hint
CountVectorizer expects all documents to be strings.

Practice

(1/5)
1. What does the Bag of Words model do in text processing?
easy
A. Counts how often each word appears in the text
B. Translates text into another language
C. Removes all punctuation from the text
D. Generates summaries of the text

Solution

  1. Step 1: Understand Bag of Words purpose

    Bag of Words counts the frequency of each word in a text, ignoring order.
  2. Step 2: Compare options to definition

    Only Counts how often each word appears in the text matches this description exactly.
  3. Final Answer:

    Counts how often each word appears in the text -> Option A
  4. Quick Check:

    Bag of Words = Counts words [OK]
Hint: Bag of Words counts words, not translates or summarizes [OK]
Common Mistakes:
  • Confusing Bag of Words with translation
  • Thinking it removes punctuation only
  • Assuming it summarizes text
2. Which of the following is the correct way to import CountVectorizer from scikit-learn in Python?
easy
A. import CountVectorizer from sklearn.feature_extraction
B. from sklearn.feature_extraction.text import CountVectorizer
C. from sklearn.text import CountVectorizer
D. import CountVectorizer from sklearn.text

Solution

  1. Step 1: Recall correct import path

    CountVectorizer is in sklearn.feature_extraction.text module.
  2. Step 2: Match options to correct syntax

    Only from sklearn.feature_extraction.text import CountVectorizer uses the correct 'from ... import ...' syntax and correct module path.
  3. Final Answer:

    from sklearn.feature_extraction.text import CountVectorizer -> Option B
  4. Quick Check:

    Correct import path = from sklearn.feature_extraction.text import CountVectorizer [OK]
Hint: CountVectorizer is in sklearn.feature_extraction.text [OK]
Common Mistakes:
  • Using wrong module path
  • Incorrect import syntax
  • Trying to import from sklearn.text
3. What will be the output shape of the matrix after applying CountVectorizer on these two sentences:
['I love cats', 'Cats love me']?
medium
A. (3, 2)
B. (2, 3)
C. (4, 2)
D. (2, 4)

Solution

  1. Step 1: Identify unique words

    Words are: 'I', 'love', 'cats', 'me' (case insensitive, 'Cats' and 'cats' same).
  2. Step 2: Count sentences and features

    There are 2 sentences and 4 unique words, so matrix shape is (2, 4).
  3. Final Answer:

    (2, 4) -> Option D
  4. Quick Check:

    2 sentences, 4 words = (2, 4) [OK]
Hint: Count unique words and sentences for shape (rows, columns) [OK]
Common Mistakes:
  • Counting words per sentence instead of unique words
  • Mixing rows and columns in shape
  • Ignoring case sensitivity
4. The following code throws an error. What is the mistake?
from sklearn.feature_extraction.text import CountVectorizer
texts = ['hello world', 'hello']
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(texts)
print(X.toarray())
print(vectorizer.get_feature_names())
medium
A. get_feature_names() is deprecated, should use get_feature_names_out()
B. fit_transform() should be fit_transform_text()
C. toarray() is not a method of X
D. CountVectorizer() needs a parameter for language

Solution

  1. Step 1: Identify deprecated method

    get_feature_names() is deprecated in recent sklearn versions.
  2. Step 2: Use correct method

    Replace get_feature_names() with get_feature_names_out() to fix error.
  3. Final Answer:

    get_feature_names() is deprecated, should use get_feature_names_out() -> Option A
  4. Quick Check:

    Use get_feature_names_out() not get_feature_names() [OK]
Hint: Use get_feature_names_out() instead of deprecated get_feature_names() [OK]
Common Mistakes:
  • Thinking fit_transform() is wrong
  • Assuming toarray() is invalid
  • Believing CountVectorizer needs language parameter
5. You have a list of sentences with some words repeated many times. How can you use CountVectorizer to ignore words that appear in more than 50% of the sentences?
hard
A. Set min_df=0.5 to ignore frequent words
B. Use stop_words='english' to remove frequent words
C. Set the parameter max_df=0.5 when creating CountVectorizer
D. Set max_features=0.5 to limit word count

Solution

  1. Step 1: Understand max_df parameter

    max_df=0.5 tells CountVectorizer to ignore words in more than 50% of documents.
  2. Step 2: Compare other options

    min_df controls minimum frequency, stop_words removes common English words, max_features limits number of features, none ignore frequent words by percentage.
  3. Final Answer:

    Set the parameter max_df=0.5 when creating CountVectorizer -> Option C
  4. Quick Check:

    max_df filters frequent words by document frequency [OK]
Hint: Use max_df to exclude very common words [OK]
Common Mistakes:
  • Confusing max_df with min_df
  • Thinking stop_words removes all frequent words
  • Using max_features to filter frequency