Bird
Raised Fist0
NLPml~20 mins

Naive Bayes for text 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
🎖️
Naive Bayes Text Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
How does Naive Bayes handle word independence in text classification?
Naive Bayes assumes that words in a text are independent of each other when calculating probabilities. What does this assumption mean in simple terms?
AWords are grouped into pairs and their combined frequency is used for classification.
BThe order of words in the text is the most important factor for classification.
CEach word's presence or absence does not affect the presence of any other word in the text.
DWords are ignored and only sentence length is used for classification.
Attempts:
2 left
💡 Hint
Think about how the model treats each word when calculating probabilities.
Predict Output
intermediate
2:00remaining
Output of Naive Bayes prediction probabilities
Given the following Python code using sklearn's MultinomialNB for text classification, what is the output of the prediction probabilities?
NLP
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB

texts = ['I love apples', 'I hate bananas']
labels = [1, 0]

vectorizer = CountVectorizer()
X = vectorizer.fit_transform(texts)

model = MultinomialNB()
model.fit(X, labels)

new_text = ['I love bananas']
X_new = vectorizer.transform(new_text)
probs = model.predict_proba(X_new)
print(probs)
A[[0.5 0.5]]
B[[0.75 0.25]]
C[[0.25 0.75]]
D[[1.0 0.0]]
Attempts:
2 left
💡 Hint
Look at how the model was trained and the new text features.
Model Choice
advanced
1:30remaining
Choosing the best Naive Bayes variant for text with word counts
You have a dataset of text documents represented as word counts. Which Naive Bayes variant is most suitable for this data?
AMultinomial Naive Bayes
BComplement Naive Bayes
CBernoulli Naive Bayes
DGaussian Naive Bayes
Attempts:
2 left
💡 Hint
Consider which variant models count data well.
Hyperparameter
advanced
1:30remaining
Effect of smoothing parameter alpha in Naive Bayes
In Multinomial Naive Bayes, what is the effect of increasing the smoothing parameter alpha?
AIt removes stop words automatically from the text data.
BIt increases the influence of rare words by adding more weight to zero counts.
CIt decreases the model's ability to handle unseen words by ignoring them.
DIt prevents zero probabilities by adding a small count to all features, smoothing the model.
Attempts:
2 left
💡 Hint
Think about how smoothing helps with words not seen in training.
Metrics
expert
2:00remaining
Interpreting Naive Bayes classification report metrics
A Naive Bayes text classifier reports the following metrics on test data: precision=0.8, recall=0.5, accuracy=0.7. What does this tell you about the model's performance?
AThe model perfectly balances false positives and false negatives.
BThe model is good at finding positive examples but misses many actual positives.
CThe model predicts positives too often, causing many false positives.
DThe model has high recall but low precision.
Attempts:
2 left
💡 Hint
Recall measures how many actual positives are found, precision measures correctness of positive predictions.

Practice

(1/5)
1. What is the main assumption behind the Naive Bayes algorithm when used for text classification?
easy
A. Words always appear in a fixed order
B. Words in a document are independent of each other given the class label
C. All documents have the same length
D. The frequency of words does not affect classification

Solution

  1. Step 1: Understand Naive Bayes assumption

    Naive Bayes assumes that each feature (word) is independent of others given the class label.
  2. Step 2: Relate assumption to text classification

    This means the presence or absence of one word does not affect another word's probability in the same document for classification.
  3. Final Answer:

    Words in a document are independent of each other given the class label -> Option B
  4. Quick Check:

    Naive Bayes = word independence assumption [OK]
Hint: Naive Bayes treats words as independent features [OK]
Common Mistakes:
  • Thinking word order matters
  • Assuming word frequency is ignored
  • Believing documents must be same length
2. Which of the following is the correct way to calculate the probability of a document belonging to a class using Naive Bayes?
easy
A. P(class) / \sum_{word} P(word|class)
B. P(class) + \sum_{word} P(word|class)
C. P(class) * \prod_{word} P(word|class)
D. P(class) - \prod_{word} P(word|class)

Solution

  1. Step 1: Recall Naive Bayes formula for text

    The probability of a class given a document is proportional to the prior probability of the class times the product of the conditional probabilities of each word given the class.
  2. Step 2: Match formula to options

    P(class) * \prod_{word} P(word|class) correctly shows multiplication (product) of P(word|class) terms with P(class).
  3. Final Answer:

    P(class) * \prod_{word} P(word|class) -> Option C
  4. Quick Check:

    Naive Bayes uses product of word probabilities [OK]
Hint: Multiply class prior by product of word likelihoods [OK]
Common Mistakes:
  • Adding probabilities instead of multiplying
  • Dividing probabilities incorrectly
  • Subtracting probabilities
3. Given the following code snippet using sklearn's MultinomialNB for text classification, what will be the predicted class for the input text ['love this movie']?
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB

texts = ['I love this movie', 'I hate this movie']
labels = ['positive', 'negative']

vectorizer = CountVectorizer()
X = vectorizer.fit_transform(texts)

model = MultinomialNB()
model.fit(X, labels)

new_text = vectorizer.transform(['love this movie'])
prediction = model.predict(new_text)
print(prediction[0])
medium
A. movie
B. negative
C. hate
D. positive

Solution

  1. Step 1: Understand training data and labels

    The model is trained on two texts: one labeled 'positive' and one 'negative'. The words 'love' and 'hate' are key indicators.
  2. Step 2: Analyze prediction input

    The input text 'love this movie' contains the word 'love' which appeared in the positive example, so the model predicts 'positive'.
  3. Final Answer:

    positive -> Option D
  4. Quick Check:

    Word 'love' matches positive class [OK]
Hint: Check which class words in input appeared during training [OK]
Common Mistakes:
  • Confusing label names with words
  • Ignoring vectorizer transformation
  • Predicting word instead of class
4. Consider this code snippet using Naive Bayes for text classification:
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB

texts = ['spam spam spam', 'ham ham ham']
labels = ['spam', 'ham']

vectorizer = CountVectorizer()
X = vectorizer.fit_transform(texts)

model = MultinomialNB()
model.fit(X, labels)

new_text = vectorizer.transform(['spam ham spam'])
prediction = model.predict(new_text)
print(prediction[0])
The output is unexpected. What is the likely cause?
medium
A. The input text contains words from both classes causing confusion
B. The vectorizer did not fit on the training data
C. MultinomialNB requires numeric labels, not strings
D. The model cannot handle words not seen in training

Solution

  1. Step 1: Analyze training and input data

    The training data has clear spam and ham texts. The input text mixes words from both classes.
  2. Step 2: Understand Naive Bayes behavior with mixed words

    Naive Bayes calculates probabilities for each class. Mixed words can cause the model to be uncertain or pick the class with higher prior or likelihood.
  3. Final Answer:

    The input text contains words from both classes causing confusion -> Option A
  4. Quick Check:

    Mixed class words confuse Naive Bayes prediction [OK]
Hint: Mixed class words can confuse Naive Bayes predictions [OK]
Common Mistakes:
  • Assuming unseen words cause error
  • Thinking vectorizer was not fitted
  • Believing labels must be numeric
5. You want to improve a Naive Bayes text classifier that often misclassifies short texts with rare words. Which approach is best to reduce this problem?
hard
A. Use Laplace smoothing to handle rare or unseen words
B. Remove all stop words from the training data
C. Increase the number of classes to make classification finer
D. Use raw word counts without normalization

Solution

  1. Step 1: Identify problem with rare words

    Rare or unseen words can cause zero probabilities, making Naive Bayes assign zero probability to classes incorrectly.
  2. Step 2: Apply Laplace smoothing

    Laplace smoothing adds a small count to all words, preventing zero probabilities and improving classification on rare words.
  3. Final Answer:

    Use Laplace smoothing to handle rare or unseen words -> Option A
  4. Quick Check:

    Laplace smoothing fixes zero probability issues [OK]
Hint: Add smoothing to avoid zero probabilities for rare words [OK]
Common Mistakes:
  • Thinking removing stop words fixes rare word issue
  • Believing more classes always improve accuracy
  • Ignoring smoothing effects on probabilities