Bird
Raised Fist0
ML Pythonml~20 mins

Naive Bayes classifier in ML Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Naive Bayes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
How does Naive Bayes handle feature independence?

Naive Bayes assumes that features are independent given the class. What does this mean in simple terms?

AFeatures depend on each other to predict the class.
BFeatures are combined into one before classification.
CFeatures are ignored if they are correlated.
DEach feature affects the class independently without influencing other features.
Attempts:
2 left
💡 Hint

Think about how Naive Bayes simplifies calculations by treating features separately.

❓ Predict Output
intermediate
2:00remaining
Output of Naive Bayes prediction code

What is the predicted class output by this code snippet?

ML Python
from sklearn.naive_bayes import GaussianNB
import numpy as np

X_train = np.array([[1, 20], [2, 21], [3, 22], [4, 23]])
y_train = np.array([0, 0, 1, 1])
model = GaussianNB()
model.fit(X_train, y_train)

X_test = np.array([[1.5, 20.5]])
prediction = model.predict(X_test)
print(prediction[0])
A0
B1
C2
DError
Attempts:
2 left
💡 Hint

Look at the training data and which class is closer to the test point.

❓ Hyperparameter
advanced
1:30remaining
Choosing the smoothing parameter in Naive Bayes

In Multinomial Naive Bayes, what effect does increasing the smoothing parameter alpha have on the model?

AIt removes features with low frequency from the dataset.
BIt reduces the effect of zero-frequency problems by adding more weight to unseen features.
CIt decreases training time by skipping some features.
DIt increases the model complexity by adding more features.
Attempts:
2 left
💡 Hint

Think about how smoothing helps when a feature never appears in training for a class.

❓ Metrics
advanced
1:30remaining
Evaluating Naive Bayes with imbalanced data

You trained a Naive Bayes classifier on imbalanced classes. Which metric is best to evaluate its performance fairly?

AF1-score
BAccuracy
CPrecision
DTraining loss
Attempts:
2 left
💡 Hint

Accuracy can be misleading when one class dominates.

🔧 Debug
expert
2:00remaining
Why does this Naive Bayes code raise an error?

What error does this code raise and why?

ML Python
from sklearn.naive_bayes import GaussianNB

X_train = [[1, 2], [3, 4]]
y_train = [0, 1, 0]
model = GaussianNB()
model.fit(X_train, y_train)
AIndexError: list index out of range
BTypeError: Unsupported operand type(s) for +: 'int' and 'str'
CValueError: Found input variables with inconsistent numbers of samples
DNo error, code runs successfully
Attempts:
2 left
💡 Hint

Check if the number of samples in features and labels match.

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