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.
What independence assumption gives Naive Bayes its name?
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.
Why add Laplace smoothing to word counts?
Answer: To keep unseen words from making a probability zero
Why: Adding one to every count gives unseen words a small nonzero likelihood.
Why score Naive Bayes in log space?
Answer: Multiplying many small probabilities can underflow
Why: Adding logs is numerically stable and preserves the ranking of classes.
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))Answer: 1
Why: The token movie appears once in the positive class.
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.
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.
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.
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.
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.
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.
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.
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.