Bird
Raised Fist0
ML Pythonml~8 mins

Naive Bayes classifier in ML Python - Model Metrics & Evaluation

Choose your learning style10 modes available
Metrics & Evaluation - Naive Bayes classifier
Which metric matters for Naive Bayes classifier and WHY

For Naive Bayes classifiers, common metrics like accuracy, precision, recall, and F1 score are important. Accuracy shows overall correctness, but if classes are uneven, precision and recall help understand how well the model finds the right class without too many mistakes. For example, in spam detection, precision tells us how many emails marked as spam really are spam, while recall tells us how many spam emails were caught. F1 score balances precision and recall, giving a single number to compare models.

Confusion matrix example
      Actual \ Predicted | Spam (Positive) | Not Spam (Negative)
      -------------------------------------------------------
      Spam (Positive)    |       80 (TP)    |       20 (FN)
      Not Spam (Negative)|       10 (FP)    |       90 (TN)
    

From this matrix:

  • Precision = 80 / (80 + 10) = 0.89
  • Recall = 80 / (80 + 20) = 0.80
  • F1 Score = 2 * (0.89 * 0.80) / (0.89 + 0.80) ≈ 0.84
  • Accuracy = (80 + 90) / 200 = 0.85
Precision vs Recall tradeoff with examples

Naive Bayes models often face a tradeoff between precision and recall:

  • High Precision, Low Recall: The model is very sure when it says "spam," but it misses many spam emails. Good if you want to avoid marking good emails as spam.
  • High Recall, Low Precision: The model catches almost all spam emails but also marks many good emails as spam. Good if missing spam is worse than false alarms.

For example, in medical diagnosis, high recall is critical to catch all sick patients. In email filtering, high precision avoids annoying users by wrongly blocking good emails.

What "good" vs "bad" metric values look like for Naive Bayes

Good metrics:

  • Accuracy above 80% on balanced data
  • Precision and recall both above 0.75, showing balanced performance
  • F1 score close to precision and recall, indicating no big tradeoff

Bad metrics:

  • Accuracy high but recall very low (e.g., 98% accuracy but 10% recall) means model misses many positives
  • Precision very low, meaning many false alarms
  • F1 score much lower than precision or recall, showing imbalance
Common pitfalls in evaluating Naive Bayes metrics
  • Accuracy paradox: High accuracy can be misleading if classes are imbalanced.
  • Data leakage: Using test data during training inflates metrics falsely.
  • Overfitting: Naive Bayes is simple but can still overfit if features leak target info.
  • Ignoring class imbalance: Not using precision/recall when classes differ greatly.
Self-check question

Your Naive Bayes model has 98% accuracy but only 12% recall on the positive class (e.g., fraud). Is it good for production? Why or why not?

Answer: No, it is not good. The model misses 88% of positive cases, which is dangerous for fraud detection. High accuracy is misleading because most data is negative. You need to improve recall to catch more fraud cases.

Key Result
Precision, recall, and F1 score are key to evaluate Naive Bayes, especially with imbalanced classes.

Practice

(1/5)
1. What is the main assumption made by the Naive Bayes classifier about the features in the data?
easy
A. Features must be numeric only
B. All features are independent of each other
C. Features are dependent on each other
D. Features must be categorical only

Solution

  1. Step 1: Understand Naive Bayes assumption

    Naive Bayes assumes that each feature contributes independently to the outcome, ignoring any correlation between features.
  2. Step 2: Identify the correct assumption

    This independence assumption simplifies calculations and makes the model 'naive'.
  3. Final Answer:

    All features are independent of each other -> Option B
  4. Quick Check:

    Feature independence [OK]
Hint: Naive means simple independence assumption [OK]
Common Mistakes:
  • Thinking features must be numeric
  • Assuming features depend on each other
  • Confusing Naive Bayes with other classifiers
2. Which of the following is the correct way to import the Gaussian Naive Bayes classifier from scikit-learn in Python?
easy
A. from sklearn.naive_bayes import GaussianNB
B. import GaussianNB from sklearn.naive_bayes
C. from sklearn.naive_bayes import Gaussian
D. import naive_bayes.GaussianNB

Solution

  1. Step 1: Recall correct import syntax in Python

    Python uses 'from module import class' syntax to import specific classes.
  2. Step 2: Match correct class and module names

    The Gaussian Naive Bayes class is named GaussianNB and is in the sklearn.naive_bayes module.
  3. Final Answer:

    from sklearn.naive_bayes import GaussianNB -> Option A
  4. Quick Check:

    Correct import syntax [OK]
Hint: Use 'from module import class' for sklearn imports [OK]
Common Mistakes:
  • Using wrong import order
  • Misspelling class or module names
  • Trying to import non-existent classes
3. Given the following Python code using GaussianNB, what will be the predicted class for the input [[1.5, 2.0]]?
from sklearn.naive_bayes import GaussianNB
import numpy as np

X = np.array([[1, 2], [2, 3], [3, 4], [6, 7], [7, 8], [8, 9]])
y = np.array([0, 0, 0, 1, 1, 1])
model = GaussianNB()
model.fit(X, y)
pred = model.predict([[1.5, 2.0]])
print(pred)
medium
A. [2]
B. [1]
C. [0]
D. Error

Solution

  1. Step 1: Understand training data and labels

    Points near [1, 2], [2, 3], [3, 4] belong to class 0; points near [6, 7], [7, 8], [8, 9] belong to class 1.
  2. Step 2: Predict class for [1.5, 2.0]

    This input is close to class 0 points, so GaussianNB predicts class 0.
  3. Final Answer:

    [0] -> Option C
  4. Quick Check:

    Nearby points class = 0 [OK]
Hint: Predict class near closest training points [OK]
Common Mistakes:
  • Confusing class labels
  • Expecting class 1 for distant points
  • Assuming error without checking code
4. Consider this code snippet using MultinomialNB:
from sklearn.naive_bayes import MultinomialNB
X = [[1, 0, 2], [0, 1, 1]]
y = [0, 1]
model = MultinomialNB()
model.fit(X, y)
pred = model.predict([[1, -1, 2]])
print(pred)
What is the error in this code?
medium
A. Model.fit() requires numpy arrays, not lists
B. MultinomialNB cannot be used with integer data
C. Labels y must be strings, not integers
D. Negative values in input features are not allowed

Solution

  1. Step 1: Check input feature values for MultinomialNB

    MultinomialNB expects non-negative feature counts; negative values cause errors.
  2. Step 2: Identify negative value in prediction input

    The input [[1, -1, 2]] contains -1, which is invalid for MultinomialNB.
  3. Final Answer:

    Negative values in input features are not allowed -> Option D
  4. Quick Check:

    Negative features invalid [OK]
Hint: MultinomialNB needs non-negative counts [OK]
Common Mistakes:
  • Thinking labels must be strings
  • Assuming lists cause errors
  • Using MultinomialNB with negative values
5. You have a dataset with text messages labeled as 'spam' or 'not spam'. Which Naive Bayes variant is best suited for this text classification task and why?
hard
A. MultinomialNB, because it models word counts effectively
B. GaussianNB, because it handles continuous numeric data well
C. BernoulliNB, because it models binary features like word presence
D. ComplementNB, because it is designed for imbalanced datasets

Solution

  1. Step 1: Understand text data representation

    Text messages are often represented as word count vectors for classification.
  2. Step 2: Match Naive Bayes variant to data type

    MultinomialNB works well with count data, modeling frequency of words in messages.
  3. Final Answer:

    MultinomialNB, because it models word counts effectively -> Option A
  4. Quick Check:

    Text counts use MultinomialNB [OK]
Hint: Use MultinomialNB for word count features [OK]
Common Mistakes:
  • Choosing GaussianNB for text data
  • Confusing BernoulliNB with MultinomialNB
  • Ignoring data type when selecting model
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard