An Introduction to Naive Bayes Algorithm for Beginners
An Introduction to Naive Bayes Algorithm for Beginners
The Naive Bayes Algorithm is one of the crucial algorithms in machine learning that helps
with classification problems. It is derived from Bayes’ probability theory and is used for text
classification, where you train high-dimensional datasets. Some best examples of the Naive
Bayes Algorithm are sentimental analysis, classifying new articles, and spam filtration.
Classification algorithms are used for categorizing new observations into predefined classes
for the uninitiated data. The Naive Bayes Algorithm is known for its simplicity and
effectiveness. It is faster to build models and make predictions with this algorithm. While
creating any ML model, it is better to apply the Bayes theorem. Application of Naive Bayes
Algorithms requires the involvement of expert ML developers.
Table of Contents
1. What is the Naive Bayes Algorithm?
2. Probability, Bayes Theory, and Conditional Probability
2.1. Probability
2.2. Bayes Theory
2.3. Conditional Probability
2.4. Bayesian Probability
3. Bayes Theory from a machine learning standpoint
4. How Naive Bayes Classifier works?
5. Types of the Naive Bayes Model
5.1. Gaussian Naive Bayes
5.2. Optimal Naive Bayes
5.3. Bernoulli Naive Bayes
5.4. Multinominal Naive Bayes
6. Advantages of a Naive Bayes Classifier
7. Disadvantages of a Naive Bayes Classifier
8. Applications that use Naive Bayes
9. Wrapping Up
For example, you cannot identify a bird based on its features and color as there are many
birds with similar attributes. But, you make a probabilistic prediction about the same, and that
is where the Naive Bayes Algorithm comes in.
Probability
Probability helps to predict an event's occurrence out of all the potential outcomes. The
mathematical equation for probability is as follows:
0 < = probability of an event < = 1. The favorable outcome denotes the event that results from
the probability. Probability is always between 0 and 1, where 0 means no probability of it
happening, and 1 means the success rate of that event is likely.
For better understanding, you can also consider a case where you predict a fruit based on its
color and texture. Here are some possible assumptions that you can make. You can either
choose the correct fruit that you have in mind or get confused with similar fruits and make
mistakes. Either way, the probability of choosing the right fruit is 50%.
Bayes Theory
Bayes Theory works on coming to a hypothesis (H) from a given set of evidence (E). It
relates to two things: the probability of the hypothesis before the evidence P(H) and the
probability after the evidence P(H|E). The Bayes Theory is explained by the following
equation:
Conditional Probability
When you take events X and Y, the conditional probability of event Y is defined as the
probability that the event occurs when event X is already over. It is written as P(Y|X). The
mathematical formula for this is as follows:
Bayesian Probability
The Bayesian Rule is used in probability theory for computing - conditional probabilities.
What is important is that you cannot discover just how the evidence will impact the
probability of an event occurring, but you can find the exact probability.
When the problem has two outputs, you can calculate the probability of every outcome and
say which one wins. Whereas if you have various input attributes, then the Naive Bayesian
Algorithm will be needed.
For instance, you are using the social_media_ads dataset. With this problem, you can predict
if a user has purchased a product by clicking on the ad, depending on her age and other
attributes. You can understand the working of the Naive Bayes Classifier by following the
below steps:
You can use the below command for importing the basic libraries required.
The below command will help you with the data preprocessing.
Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.transform_fit(X_train)
X_test = sc.transform(X_test)
In this step, you have to split the dataset into a training dataset (70%) and a testing dataset
(30%). Next, you have to do some basic feature scaling with the help of a standard scaler. It
will transform the dataset in a way where the mean value will be 0, and the standard deviation
will be 1.
You should then write the following command for training the model.
A confusion matrix helps to understand the quality of the model. It describes the production
of a classification model on a set of test data for which you know the true values. Every row
in a confusion matrix portrays an actual class, and every column portrays the predicted class.
However, in some cases, these steps might not be absolutely necessary. But the above-
mentioned example provides a clear idea and information about how data points can be
classified.
There are four types of the Naive Bayes Model, which are explained below:
It is a straightforward algorithm used when the attributes are continuous. The attributes
present in the data should follow the rule of Gaussian distribution or normal distribution. It
remarkably quickens the search, and under lenient conditions, the error will be two times
greater than Optimal Naive Bayes.
Optimal Naive Bayes selects the class that has the greatest posterior probability of
happenings. As per the name, it is optimal. But it will go through all the possibilities, which
is very slow and time-consuming.
Bernoulli Naive Bayes is an algorithm that is useful for data that has binary or boolean
attributes. The attributes will have a value of yes or no, useful or not, granted or rejected, etc.
Multinominal Naive Bayes
Wrapping Up
Though the Naive Bayes Algorithm has a lot of limitations, it is still the most chosen
algorithm for solving classification problems because of its simplicity. It works well on spam
filtering and the classification of documents. It has the highest rate of success when compared
to other algorithms because of its speed and efficiency.
Author
Turing
Author is a seasoned writer with a reputation for crafting highly engaging, well-
researched, and useful content that is widely read by many of today's skilled
programmers and developers.