top of page

Naive Bayes Theorem

Bayes Theorem :

 Bayes’ Theorem is used to find the probability of an event occurring after the probability of another event that has already occurred.



                 P(A/B) =  -------------  


Note: A and B are events and P(B) not equal to zero.

Actually we are trying to find probability of A after probability of B is occurred. Now, based on the dataset we will apply our bayes theorem to get the desired outcome.

We can use the above as formula as given below:




                 P(Y/X) =  -------------


Note: Y is a variable and X is a dependent feature.

Naive Bayes is a family of probabilistic algorithms based on Bayes' theorem with the assumption of independence between the features. It is a classification algorithm widely used in machine learning and natural language processing.


Bayes' theorem states that the probability of a class (C) given a set of features (F) is proportional to the product of the prior probability of the class (P(C)) and the likelihood of the features given the class (P(F|C)):

                                                             P(C | F) = (P(F | C) * P(C)) / P(F)


In Naive Bayes, the feature independence assumption allows us to simplify the calculation of the likelihood by assuming that each feature contributes independently to the classification. So, the likelihood can be calculated as the product of the individual feature probabilities:


                                                             P(F | C) = P(f1 | C) * P(f2 | C) * ... * P(fn | C)

There are three popular variants of Naive Bayes: Gaussian Naive Bayes, Multinomial Naive Bayes, and Bernoulli Naive Bayes.

  1. Gaussian Naive Bayes: It assumes that the continuous features have a Gaussian distribution.

  2. Multinomial Naive Bayes: It is used for discrete count data such as text classification.

  3. Bernoulli Naive Bayes: It is similar to the Multinomial Naive Bayes, but it predicts the presence or absence of a feature rather than counting the number of occurrences.


Naive Bayes is a fast and simple algorithm, and it works well for high-dimensional datasets. However, the feature independence assumption is often violated, which can lead to poor performance in practice. Despite its limitations, Naive Bayes is still a useful algorithm to have in your machine learning toolkit.

Here are some examples of when Naive Bayes might be used:

  1. Text classification: Given a set of documents, a Naive Bayes classifier can be used to determine the category of each document based on the words it contains. For example, a Naive Bayes classifier could be trained to classify emails as spam or not spam.

  2. Sentiment analysis: Given a set of customer reviews, a Naive Bayes classifier can be used to predict the sentiment (positive, negative, or neutral) of each review.

  3. Medical diagnosis: Given a set of symptoms, a Naive Bayes classifier can be used to predict the underlying disease.

  4. Fraud detection: Given a set of transactions, a Naive Bayes classifier can be used to identify transactions that are likely to be fraudulent.


These are just a few examples, but Naive Bayes can be applied to many other domains where a categorical dependent variable needs to be predicted based on a set of independent variables.

Here's an example implementation of Naive Bayes in Python using the scikit-learn library:

import numpy as np
from sklearn.naive_bayes import GaussianNB


# Training data
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
y = np.array([1, 1, 1, 2, 2, 2])


# Create a Gaussian Naive Bayes object
clf = GaussianNB()


# Train the model, y)


# Predict using the model
pred = clf.predict([[-0.8, -1]])


In this example, we have training data X and y representing the independent and dependent variables respectively. The GaussianNB class from the scikit-learn library is used to fit a Gaussian Naive Bayes model to the data. Finally, the model is used to make a prediction for a new input value [-0.8, -1]. The predicted value is [1], which indicates that the input belongs to class 1.

bottom of page