Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 29

Naive Bayes Classifier

Score each class by how likely the inputs are under that class, assuming the inputs contribute separately. One example is on this page.

Intermediate12 min
Naive Bayes Classifier MahiDev

What You Will Learn

  • Idea
  • Example
  • When to use it

Idea

Naive Bayes picks the class with the highest probability given the inputs. It assumes each input is independent once the class is known. That assumption is rarely exact. Multiplying the per-input probabilities is still a fast and strong baseline.

Example

python
import numpy as np
from sklearn.naive_bayes import GaussianNB

X = np.array([[1, 20], [2, 22], [8, 60], [9, 62]], dtype=float)
y = np.array([0, 0, 1, 1])
label = GaussianNB().fit(X, y).predict([[3, 25]])[0]
print(f"label: {int(label)}")

Output:

text
label: 0

When to use it

  • Use it for text counts or for a fast baseline on small tables.
  • GaussianNB fits a bell curve per input inside each class.
  • Correlated inputs break the independence assumption, and the model can still work.

The theory lesson Naive Bayes derives the formula.

Loading playground…