Bird
Raised Fist0
ML Pythonml~20 mins

Text feature basics (CountVectorizer, TF-IDF) in ML Python - 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
🎖️
Text Feature 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?
ML Python
from sklearn.feature_extraction.text import CountVectorizer
corpus = ['apple orange apple', 'orange banana orange']
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(corpus)
result = X.toarray()
feature_names = vectorizer.get_feature_names_out()
print(feature_names)
print(result)
A['apple' 'banana' 'orange']\n[[2 0 1]\n [0 1 2]]
B['apple' 'banana' 'orange']\n[[1 0 2]\n [0 2 1]]
C['apple' 'banana' 'orange']\n[[2 1 0]\n [1 0 2]]
D['banana' 'apple' 'orange']\n[[2 0 1]\n [0 1 2]]
Attempts:
2 left
💡 Hint
CountVectorizer sorts features alphabetically and counts word occurrences per document.
🧠 Conceptual
intermediate
1:30remaining
Understanding TF-IDF importance
Which statement best describes why TF-IDF is useful compared to simple word counts?
ATF-IDF counts the total number of words in a document without weighting.
BTF-IDF reduces the weight of common words and highlights rare but important words.
CTF-IDF only counts words that appear in all documents equally.
DTF-IDF ignores word frequency and only uses document length.
Attempts:
2 left
💡 Hint
Think about how common words like 'the' or 'and' should be treated in text analysis.
Metrics
advanced
2:00remaining
Comparing vector lengths from CountVectorizer and TF-IDF
Given the same text corpus, which statement about the vector lengths produced by CountVectorizer and TfidfVectorizer is true?
AVectors from TfidfVectorizer usually have smaller values but the same length as CountVectorizer vectors.
BVectors from CountVectorizer always have larger length because they count words multiple times.
CVectors from TfidfVectorizer are always longer because they add extra features.
DVectors from CountVectorizer and TfidfVectorizer have different lengths because they use different vocabularies.
Attempts:
2 left
💡 Hint
Both vectorizers use the same vocabulary by default.
🔧 Debug
advanced
2:00remaining
Identifying error in TF-IDF code snippet
What error will this code raise and why? from sklearn.feature_extraction.text import TfidfVectorizer corpus = ['cat dog', 'dog mouse'] vectorizer = TfidfVectorizer(stop_words='english') X = vectorizer.fit_transform(corpus) print(vectorizer.get_feature_names_out())
AValueError because 'english' stop words remove all words leaving empty vocabulary
BAttributeError because get_feature_names_out() does not exist
CTypeError because stop_words must be a list, not a string
DNo error; output is ['cat' 'dog' 'mouse']
Attempts:
2 left
💡 Hint
Check what words remain after removing English stop words from the corpus.
Model Choice
expert
2:30remaining
Choosing the best vectorizer for short text classification
You want to classify very short text messages (like tweets) where common words appear frequently but are not useful. Which vectorizer choice is best and why?
AUse CountVectorizer with max_features=10 to limit vocabulary size.
BUse CountVectorizer without stop words because raw counts capture all info.
CUse TfidfVectorizer with English stop words to reduce common word impact and highlight rare words.
DUse TfidfVectorizer without stop words to keep all words weighted equally.
Attempts:
2 left
💡 Hint
Think about how to reduce noise from common words in short texts.

Practice

(1/5)
1. What does CountVectorizer do in text processing?
easy
A. Calculates the importance of words based on frequency and rarity
B. Counts how many times each word appears in the text
C. Removes stop words from the text
D. Converts text into lowercase only

Solution

  1. Step 1: Understand CountVectorizer's role

    CountVectorizer transforms text into a matrix of token counts, counting word occurrences.
  2. Step 2: Differentiate from TF-IDF

    Unlike TF-IDF, it does not weigh words by importance, only counts frequency.
  3. Final Answer:

    Counts how many times each word appears in the text -> Option B
  4. Quick Check:

    CountVectorizer = word counts [OK]
Hint: CountVectorizer counts words, TF-IDF scores importance [OK]
Common Mistakes:
  • Confusing CountVectorizer with TF-IDF
  • Thinking it removes stop words by default
  • Assuming it normalizes text only
2. Which of the following is the correct way to import and create a CountVectorizer in Python?
easy
A. from sklearn.feature_extraction.text import CountVectorizer vectorizer = CountVectorizer()
B. import CountVectorizer from sklearn.text vectorizer = CountVectorizer()
C. from sklearn.text import CountVectorizer vectorizer = CountVectorizer()
D. import CountVectorizer vectorizer = CountVectorizer()

Solution

  1. Step 1: Recall correct sklearn import path

    CountVectorizer is in sklearn.feature_extraction.text module.
  2. Step 2: Check syntax correctness

    from sklearn.feature_extraction.text import CountVectorizer vectorizer = CountVectorizer() uses correct import and instantiation syntax.
  3. Final Answer:

    from sklearn.feature_extraction.text import CountVectorizer vectorizer = CountVectorizer() -> Option A
  4. Quick Check:

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

Solution

  1. Step 1: Count unique words in sentences

    Words are: 'i', 'love', 'cats', 'me' -> 4 unique words.
  2. Step 2: Understand shape of output matrix

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

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

    Rows = sentences, columns = unique words [OK]
Hint: Shape = (number of texts, unique words) [OK]
Common Mistakes:
  • Mixing rows and columns in shape
  • Counting duplicate words multiple times
  • Ignoring case sensitivity (CountVectorizer lowercases by default)
4. Identify the error in this TF-IDF code snippet:
from sklearn.feature_extraction.text import TfidfVectorizer
texts = ["apple banana apple", "banana fruit"]
tfidf = TfidfVectorizer()
X = tfidf.fit_transform(texts)
print(tfidf.get_feature_names())
medium
A. fit_transform() should be called on texts as a string, not list
B. TfidfVectorizer() requires stop_words parameter
C. get_feature_names() is deprecated, should use get_feature_names_out()
D. Import statement is incorrect

Solution

  1. Step 1: Check method usage for feature names

    In recent sklearn versions, get_feature_names() is deprecated.
  2. Step 2: Use updated method

    Use get_feature_names_out() instead to get feature names without error.
  3. Final Answer:

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

    Use get_feature_names_out() for TF-IDF features [OK]
Hint: Use get_feature_names_out() with TF-IDF [OK]
Common Mistakes:
  • Using deprecated get_feature_names() method
  • Passing wrong data type to fit_transform
  • Incorrect import paths
5. You want to transform text data so that common words like 'the' and 'is' have less impact, but rare important words have higher scores. Which method should you use?
hard
A. One-hot encoding of words
B. CountVectorizer without stop words
C. Raw word counts from CountVectorizer
D. TF-IDF Vectorizer

Solution

  1. Step 1: Understand the goal of reducing common word impact

    Common words appear frequently but carry less meaning, so their impact should be lowered.
  2. Step 2: Identify method that weighs words by importance

    TF-IDF scores words higher if they are rare and important, reducing common word impact.
  3. Final Answer:

    TF-IDF Vectorizer -> Option D
  4. Quick Check:

    TF-IDF = importance weighting [OK]
Hint: Use TF-IDF to weigh rare words higher [OK]
Common Mistakes:
  • Using raw counts which treat all words equally
  • Assuming stop words removal alone solves importance
  • Confusing one-hot encoding with frequency weighting