Answer key · 12 questions

Python Naive Bayes text classifier Exercises With Answers

All 12 questions for Python Naive Bayes text classifier in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python Naive Bayes text classifier, with the exact answer and a short explanation for every question.

The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.

01 · Multiple choice

What independence assumption gives Naive Bayes its name?

  1. The labels are always balanced
  2. Features are assumed independent given the classcorrect
  3. Every class has the same prior
  4. All words have equal length

Answer: Features are assumed independent given the class

Why: The model multiplies per-word likelihoods as if each word were independent once the class is known.

02 · Multiple choice

Why add Laplace smoothing to word counts?

  1. To remove the prior
  2. To keep unseen words from making a probability zerocorrect
  3. To make training documents longer
  4. To sort the vocabulary

Answer: To keep unseen words from making a probability zero

Why: Adding one to every count gives unseen words a small nonzero likelihood.

03 · Multiple choice

Why score Naive Bayes in log space?

  1. It removes the need for smoothing
  2. Multiplying many small probabilities can underflowcorrect
  3. It turns counts into integers
  4. Logarithms make the classes balanced

Answer: Multiplying many small probabilities can underflow

Why: Adding logs is numerically stable and preserves the ranking of classes.

04 · Multiple choice

What is the output?

docs = [("good movie", "pos"), ("great film", "pos"), ("bad movie", "neg"), ("boring film", "neg")]
counts = {}
for text, label in docs:
    counts.setdefault(label, {})
    for word in text.split():
        counts[label][word] = counts[label].get(word, 0) + 1
print(counts["pos"].get("movie", 0))
  1. 2
  2. 1correct
  3. 4
  4. 0

Answer: 1

Why: The token movie appears once in the positive class.

05 · Fill in the blank

Complete the count increment.

counts = {}
word = "movie"
counts[word] = counts.get(word, 0) + ____
print(counts[word])

Answer: 1

Why: Each observation adds one to the existing count.

06 · Fill in the blank

Complete the Laplace smoothing numerator.

counts = {"movie": 1}
total = 2
vocab_size = 3
probability = (counts.get("film", 0) + ____) / (total + vocab_size)
print(round(probability, 2))

Answer: 1

Why: Smoothing adds one to the observed count of every vocabulary word.

07 · Fill in the blank

Complete the log-space score.

import math
probability = 0.2
print(round(math.____(probability), 3))

Answer: log

Why: The log function converts a product of probabilities into a sum.

08 · Fill in the blank

Complete the prior denominator.

import math
class_docs = {"pos": 2, "neg": 2}
docs = ["a", "b", "c", "d"]
print(math.log(class_docs["pos"] / ____(docs)))

Answer: len

Why: The prior divides the number of documents in a class by the total number of documents.

09 · Predict output

What does this code print?

import math

docs = [("good movie", "pos"), ("great film", "pos"), ("bad movie", "neg"), ("boring film", "neg")]
vocab = sorted({word for text, _ in docs for word in text.split()})
counts = {}
priors = {}
for text, label in docs:
    priors[label] = priors.get(label, 0) + 1
    counts.setdefault(label, {})
    for word in text.split():
        counts[label][word] = counts[label].get(word, 0) + 1

def predict(text):
    scores = {}
    for label in counts:
        total = sum(counts[label].values())
        score = math.log(priors[label] / len(docs))
        for word in text.split():
            score += math.log((counts[label].get(word, 0) + 1) / (total + len(vocab)))
        scores[label] = score
    return max(scores, key=scores.get)

print(predict("great film"))

Answer: pos

Why: Both words are positive in training, and the positive class wins the log score.

10 · Predict output

What does the program output?

docs = [("good movie", "pos"), ("great film", "pos"), ("bad movie", "neg"), ("boring film", "neg")]
counts = {}
for text, label in docs:
    counts.setdefault(label, {})
    for word in text.split():
        counts[label][word] = counts[label].get(word, 0) + 1
print(counts["neg"]["bad"])

Answer: 1

Why: The token bad appears once in the negative class, so the count is 1.

11 · Predict output

What appears on the screen?

docs = ["good movie", "great film", "bad movie", "boring film"]
labels = ["pos", "pos", "neg", "neg"]
print(labels.count("pos") / len(docs))

Answer: 0.5

Why: Two of the four training documents are positive, so the prior is 0.5.

12 · Predict output

What is printed?

counts = {"great": 0}
total = 4
vocab_size = 6
probability = (counts.get("great", 0) + 1) / (total + vocab_size)
print(round(probability, 1))

Answer: 0.1

Why: The smoothed numerator is 1 and the denominator is 10, so the probability is 0.1.

Try the questions first

Open the practice page for Python Naive Bayes text classifier and answer the same 12 questions with instant feedback.

Practice this lesson