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%

How Naive Bayes Works: A Step-by-Step Guide

Follow the "Calculation Story" as you click through the steps to see the logic unfold.

The Naive Bayes Classifier is a simple yet powerful algorithm for predictive modeling. It's based on Bayes' Theorem and is particularly useful for text classification, like spam filtering. Its core idea is to calculate the probability of a certain outcome (e.g., "Should I play tennis?") based on the evidence provided by a set of features (e.g., the weather outlook, temperature, and wind).

The "Naive" Assumption: A Key Simplification

The "naive" part of the name comes from a key assumption the algorithm makes: it assumes that all features are independent of each other. In our example, it assumes that the 'Outlook' has no effect on the 'Temperature' or 'Wind'. While this is often not true in the real world (a sunny outlook usually implies hotter temperatures), this simplification makes the calculations much easier and faster. Despite this "naive" assumption, the classifier works surprisingly well in many real-world scenarios.

The Calculation Process: A Walkthrough

The interactive panel walks you through the exact steps the algorithm takes to make a prediction. Let's use the default input: Outlook=Sunny, Temp=Cool, Wind=Strong.

  1. Step 1: Calculate Prior Probabilities

    First, the algorithm looks at the historical data (the "Knowledge Base") to determine the overall probability of each outcome. This is the "prior" belief before considering any new evidence.

    • P(Play=Yes): Out of 14 total days, 'Play' was 'Yes' on 9 of them. So, P(Yes) = 9/14.
    • P(Play=No): 'Play' was 'No' on 5 of them. So, P(No) = 5/14.
  2. Step 2: Calculate Likelihoods for Each Feature

    Next, for each class ('Yes' and 'No'), the algorithm calculates the probability of our specific input features occurring. This is the "likelihood".

    • Likelihood for 'Yes':
      • P(Outlook=Sunny | Play=Yes): Among the 9 'Yes' days, 2 were 'Sunny'. (Prob = 2/9)
      • P(Temp=Cool | Play=Yes): Among the 9 'Yes' days, 3 were 'Cool'. (Prob = 3/9)
      • P(Wind=Strong | Play=Yes): Among the 9 'Yes' days, 3 were 'Strong'. (Prob = 3/9)
    • Likelihood for 'No':
      • P(Outlook=Sunny | Play=No): Among the 5 'No' days, 3 were 'Sunny'. (Prob = 3/5)
      • P(Temp=Cool | Play=No): Among the 5 'No' days, 1 was 'Cool'. (Prob = 1/5)
      • P(Wind=Strong | Play=No): Among the 5 'No' days, 3 were 'Strong'. (Prob = 3/5)

    Note: The visualization uses Laplace Smoothing (adding 1 to the numerator and the number of unique feature values to the denominator) to avoid zero-probability issues, so the exact fractions will look slightly different in the "Calculation Story".

  3. Step 3: Combine and Calculate Scores

    Now, we combine everything. For each outcome, we multiply its prior probability by all of its likelihoods.

    • Score(Yes) = P(Yes) * P(Sunny|Yes) * P(Cool|Yes) * P(Strong|Yes)
    • Score(No) = P(No) * P(Sunny|No) * P(Cool|No) * P(Strong|No)
  4. Step 4: Normalize and Predict

    The scores are not final probabilities. To get a clean 0-100% result, we normalize them by dividing each score by the sum of both scores. The class with the higher final probability is our prediction.

Cheat sheet

Naive Bayes Classifier

The Naive Bayes Classifier is a simple yet powerful algorithm for predictive modeling. It's based on Bayes' Theorem and is particularly useful for text classification, like spam filtering. Its core idea is to calculate the probability of a certain outcome (e.g., "Should I play tennis?") based on the evidence provided by a set of features (e.g., the weather outlook, temperature, and wind).

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