Home / Machine Learning

Naive Bayes Classifier

Uncover the probability logic behind weather-based predictions.

Inference Engine

Calculation Story

Ready

Adjust inputs and click 'Next Step' to start the mathematical breakdown.

Knowledge Base

Results

Probability Play = Yes 0%
Probability Play = No 0%

Overview

Naive Bayes predicts a class by combining its prior probability with the likelihood of the observed features under that class. The “naive” assumption makes the calculation practical: given the class, the model treats each feature as independent of the others.

Bayes’ rule and the independence assumption

For a class c and observed features x₁, …, xₚ:

P(c | x₁, …, xₚ) ∝ P(c) × P(x₁, …, xₚ | c)

Naive Bayes approximates the joint likelihood as a product:

P(c | x₁, …, xₚ) ∝ P(c) × ∏ᵢ P(xᵢ | c)

The features need not be independent in general; the assumption is conditional on the class. It is often false—for example, “New” and “York” are related words—but the approximation can still rank classes usefully. The class prior matters: common classes start with more probability before the features are considered.

Work through a prediction

In the classic weather example, the observed features are Sunny, Cool and Strong wind. Without smoothing, the training counts give:

Score(Yes) = 9/14 × 2/9 × 3/9 × 3/9 = 1/63

Score(No) = 5/14 × 3/5 × 1/5 × 3/5 = 9/350

These are unnormalised scores: each is the class prior multiplied by the three class-conditional likelihoods. The No score is larger, so the model predicts No. Dividing each score by their sum would produce normalised probabilities. The visualiser uses Laplace smoothing to avoid zero likelihoods, so its displayed values differ from this unsmoothed hand calculation.

What the assumption buys, and what it costs

The product turns a large joint-probability problem into separate per-feature counts. This makes training and prediction fast, even with many features and relatively few examples. It can work well for text classification, where word features are dependent but their counts still carry useful class information.

Naive Bayes variants match different feature types: Multinomial Naive Bayes is common for non-negative counts, Bernoulli Naive Bayes for binary indicators, and Gaussian Naive Bayes for continuous features modelled with class-specific normal distributions. Choosing the wrong variant or preprocessing can make the likelihoods inappropriate.

The independence approximation can produce probabilities that are too extreme, especially when correlated features repeat the same evidence. A strong class prediction is not proof that the probability is calibrated. If probability accuracy matters, evaluate a reliability curve and consider calibration. For a model that can learn feature interactions directly, compare with logistic regression.

Sources and next steps

See scikit-learn’s Naive Bayes guide for the variants and their assumptions. The weather counts above illustrate how priors and likelihoods combine; your own application should validate both classification performance and probability quality.

Predict, then reveal

About to run: press Next Step. Before it does — what happens to the readout?

Committing to an answer first is the point — the reveal runs the experiment on the visualisation above and reads the real value back, so nothing here is scripted.

Recall check

0 of 3

Say the answer out loud before you reveal it — recalling it is what makes it stick, and rereading it is not.

  1. What does this module say about “Bayes’ rule and conditional independence”?

  2. What does this module say about “What the assumption buys, and what it costs”?

  3. What does this module say about “Sources and next steps”?

Cheat sheet

Naive Bayes Classifier

Step through Bayes' theorem on real text and see why a 'naive' independence assumption still classifies spam remarkably well.

MACHINE LEARNING · vizlearn.in/machine_learning/naive_bayes.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.