Spam Classifier Pipeline with MultinomialNB

Quick Overview

Build a scikit-learn pipeline that chains CountVectorizer and MultinomialNB to label short messages as spam or ham and return the spam probability for each one. It tests pipeline construction, reading predict_proba columns through classes_, and how Naive Bayes scores messages made only of words it never saw in training.

Spam Classifier Pipeline with MultinomialNB

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement two functions with scikit-learn: - `build_spam_pipeline(alpha=1.0)` returns an unfitted `Pipeline`. - `classify_messages(train_texts, train_labels, new_texts, alpha=1.0)` fits that pipeline on labelled messages and returns `(labels, spam_proba)` for `new_texts`. Requirements: - The pipeline steps are exactly `("vectorizer", CountVectorizer())` followed by `("classifier", MultinomialNB(alpha=alpha))`. Keep the default vectorizer settings, so text is lowercased. - `labels` is a NumPy array of predicted labels (`"spam"` or `"ham"`), one per new message. - `spam_proba` is a 1-D float array of P(spam) with shape `(len(new_texts),)`. - Find the spam column by looking up `"spam"` in `classes_` so the order of the training labels does not matter. Do not hard-code a column index. - A message made only of words that never appeared in training gets the spam prior as its probability. Example: ```python train = ["win money now", "free money", "meeting at noon", "lunch at noon", "see you at lunch"] labels = ["spam", "spam", "ham", "ham", "ham"] pred, spam_proba = classify_messages(train, labels, ["free money", "hello there"]) # pred -> array(['spam', 'ham']) # spam_proba -> array([0.877, 0.4]) (rounded; 0.4 is the spam prior) ``` ```hint Unseen words Look at the row the vectorizer produces for a message whose words are all outside the training vocabulary, and at what the classifier's per-class score reduces to for that row. ``` ### What a Strong Answer Covers - A pipeline with the two named steps and `alpha` passed through to `MultinomialNB`, so the same tokenization runs at fit and predict time. - Locating the spam column through `classes_` and knowing how scikit-learn orders that attribute. - Return types and shapes: an array of labels and a 1-D float array of P(spam), one entry per new message. - Why a message of only unseen words falls back to the class prior. - Edge cases such as training data with no spam label or an empty batch of new messages. ### Follow-up Questions - How does `alpha` affect the predicted probabilities? - Why can raw `predict_proba` outputs from Naive Bayes be poorly calibrated, and how would you fix that when the probabilities drive a threshold? - Would you switch the vectorizer to TF-IDF or add bigrams, and what would that change for a multinomial model?

Overview: Build a scikit-learn pipeline that chains CountVectorizer and MultinomialNB to label short messages as spam or ham and return the spam probability for each one. It tests pipeline construction, reading predict_proba columns through classes_, and how Naive Bayes scores messages made only of words it never saw in training.

PracHub logo
General
Nov 24, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement two functions with scikit-learn:

  • build_spam_pipeline(alpha=1.0) returns an unfitted Pipeline .
  • classify_messages(train_texts, train_labels, new_texts, alpha=1.0) fits that pipeline on labelled messages and returns (labels, spam_proba) for new_texts .

Requirements:

  • The pipeline steps are exactly ("vectorizer", CountVectorizer()) followed by ("classifier", MultinomialNB(alpha=alpha)) . Keep the default vectorizer settings, so text is lowercased.
  • labels is a NumPy array of predicted labels ( "spam" or "ham" ), one per new message.
  • spam_proba is a 1-D float array of P(spam) with shape (len(new_texts),) .
  • Find the spam column by looking up "spam" in classes_ so the order of the training labels does not matter. Do not hard-code a column index.
  • A message made only of words that never appeared in training gets the spam prior as its probability.

Example:

train = ["win money now", "free money", "meeting at noon",
         "lunch at noon", "see you at lunch"]
labels = ["spam", "spam", "ham", "ham", "ham"]
pred, spam_proba = classify_messages(train, labels, ["free money", "hello there"])
# pred       -> array(['spam', 'ham'])
# spam_proba -> array([0.877, 0.4])  (rounded; 0.4 is the spam prior)

What a Strong Answer Covers Guidance

  • A pipeline with the two named steps and alpha passed through to MultinomialNB , so the same tokenization runs at fit and predict time.
  • Locating the spam column through classes_ and knowing how scikit-learn orders that attribute.
  • Return types and shapes: an array of labels and a 1-D float array of P(spam), one entry per new message.
  • Why a message of only unseen words falls back to the class prior.
  • Edge cases such as training data with no spam label or an empty batch of new messages.

Follow-up Questions Guidance

  • How does alpha affect the predicted probabilities?
  • Why can raw predict_proba outputs from Naive Bayes be poorly calibrated, and how would you fix that when the probabilities drive a threshold?
  • Would you switch the vectorizer to TF-IDF or add bigrams, and what would that change for a multinomial model?