23

AprNaive Bayes is one of the simplest methods to design a classifier. It is a probabilistic algorithm used in machine learning for designing classification models that use Bayes Theorem as their core. Its use is quite widespread especially in the domain of Natural language processing, document classification and allied.

In this blog, we’ll learn:

The term naïve in the name tells one of the basic assumptions in these classifiers that the input features are completely independent of each other. In other words, there will be no implicit change in the input features when one or more than one input parameter is changed, explicitly.

Naïve Bayes is an extremely popular algorithm owing to its probabilistic nature which provides it a significant advantage like fast-paced predictions, easy codes over other algorithms. This makes this model highly scalable.

Naïve Bayes classifier uses conditional probability and number probability distributions to train the machine. Thus, it becomes important to know the following points:

In the domain of probability theory, conditional probability is the measure of an event A occurring when another event say B has taken place. This is represented by and is read as “the conditional probability of A given B”.

One of the classical example in the domain of conditional probability is flipping the coin. SO, while flipping a fair coin, the chances of having a head or tail are equal. IN other words, the probability of any of the events is 0.5. So, conditional probability talks about the chances of having a head once we already had a tail. In this case, theoretically, it remains at 0.5 as well. Bayes theorem provides a mathematical model for calculating these.

It is also sometimes called as the god’s theorem. It describes the probability of occurrence of an event, based upon the existing knowledge about the conditions that are related to that specific event. Say, diabetes happens at some particular age X, and then by using the Bayes theorem, the age of a person can be used to forecast the chances that they will have diabetes and the results will be much better as compared to a situation when we had no idea about their age.

Mathematically, Bayes’ theorem is given by the following equation:

Read: Python Career Path - How & Why to Pursue Python Career Options!

**The Naïve Bayes:**

The Bayes theorem as depicted in figure (1), shows that it can reflect the reverse of an event. Just the event shown should be known. When the Bayes rule in applied to a set of events that are completely independent, the resulting model is called naïve Bayes.

A and B in the standard formula can be replaced with X and Y where X is the independent variable and Y is the dependent variable.

Now for non-linear cases of X where more than one instance of the same exists, the instance can be reduced to a linear instance by simply each instance to be a separate instance and applying the Bayes rule:

As a comparison of equation (3) and (4) depicts, it can be inferred from these that the likelihoods of all the X’s can be multiplied and is called the probability of likelihood of evidence. This can be known from the training dataset by filtering records where .

The multiplicative term with the probability of likelihood of evidence is called the prior which depicts the overall probability of Y= q, where q is the class label of Y.

As can be observed from the above equations, the Naïve Bayes theorem only depicts the probability. Hence, only in its nascent capacity, it cannot become a classifier. Thus, it is always used in combination with a probability distribution for designing the classifier. Most commonly used naïve Bayes based classifier is the following:

Read: How to Use SQL with Python?

**1). Gaussian naïve Bayes:**

A Gaussian naïve Bayes uses Gaussian probability distribution for designing and is used to deal with continuous data. A Gaussian distribution is defined as:

**2). Multinomial Naïve Bayes:**

If the dataset set consists of probability in terms of frequency instead of continuous data Gaussian distribution cannot be applied. In this situation, Multinomial distribution is used.

**3). Bernoulli Bayes:**

In case the features are binary in nature, the Bernoulli naïve Bayes are utilized. It is very popular in case of documentation when the feature space is binary.

In this blog, for designing a naïve Bayes classifier, sklearn is utilized with python. A classical iris flower dataset is utilized. Details of the dataset can be found here.

First of all, libraries need to be imported. The commands for the same are:

from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.naive_bayes import GaussianNB

Loading the dataset and splitting it into training and testing set

Read: Top 10 Python Libraries For Machine Learning

X, y = load_iris(return_X_y=True) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=0)

Training the model:

gnb = GaussianNB()

If one wishes to use any other model from the domain of Naïve Bayes based classifier. Then, that needs to be specified here.

Querying the query set and checking the results:

y_pred = gnb.fit(X_train, y_train).predict(X_test) print("Number of mislabeled points out of a total %d points : %d" % (X_test.shape[0], (y_test != y_pred).sum()))

This query, in this case, will give the output as

Number of mislabeled points out of a total of 75 points: 4

Thus, out of all the test sets of 75, 4 were incorrect. Giving a final accuracy of almost 99%.

**Pros:**

- Naïve Bayes based classifiers are efficient in terms of time and space complexity.
- When the features follow independence, this classifier outperforms other types of the classifier.
- Naïve Bayes works well with continuous as well as categorical data.

**Cons:**

- If there happens to be a new class querying the test data set, that class will be given class probability of 0 and the classifier will be unable to make a classification. This is a typical case and is called “Zero Frequency”.
- The other problem is the assumption of independence in the features which give the classifier its name. If the slightest of dependency exists the classifier will not make the best fit. This challenges the concept of naïve as well as becomes interpersonal to the architect of the classifier to be certain about independence.

**Uses of Naïve Bayes based classifier:**

**Text Classification:**Classifiers based on Naïve Bayes are mostly used in text classification as they outperform in case of multi-class classification.**Spam filters:**Checking for spam is a shows naïve bias as either it is spam or its not. Hence, is a suitable use case for naïve Bayes.**Sentiment analysis:**Sentiments like happy, sad, angry are also independent in nature. Thus, making an ideal use case.**Recommendation systems:**Naïve Bayes based classifiers are also used in recommendation systems where collaborative filtering or independence in feature space is observed.

IN this blog, Naïve Bayes classifier was covered which belongs to the supervised learning domain of machine learning. Naïve Bayes based classifier is extremely competent when the condition of independence in the feature space is met. It can be used as per the level of understanding and requirement of the project where the condition of independence is met.

Read: What is Python IDE? Five IDEs for Python Developers you must Know

A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.

AWS

- AWS & Fundamentals of Linux
- Amazon Simple Storage Service
- Elastic Compute Cloud
- Databases Overview & Amazon Route 53

DevOps

- Intro to DevOps
- GIT and Maven
- Jenkins & Ansible
- Docker and Cloud Computing

Data Science

- Data Science Introduction
- Hadoop and Spark Overview
- Python & Intro to R Programming
- Machine Learning

Hadoop

- Architecture, HDFS & MapReduce
- Unix Shell & Apache Pig Installation
- HIVE Installation & User-Defined Functions
- SQOOP & Hbase Installation

Salesforce

- Salesforce Configuration Introduction
- Security & Automation Process
- Sales & Service Cloud
- Apex Programming, SOQL & SOSL

QA

- Introduction and Software Testing
- Software Test Life Cycle
- Automation Testing and API Testing
- Selenium framework development using Testing

Business Analyst

- BA & Stakeholders Overview
- BPMN, Requirement Elicitation
- BA Tools & Design Documents
- Enterprise Analysis, Agile & Scrum

MS SQL Server

- Introduction & Database Query
- Programming, Indexes & System Functions
- SSIS Package Development Procedures
- SSRS Report Design

Python

- Features of Python
- Python Editors and IDEs
- Data types and Variables
- Python File Operation

Artificial Intelligence

- Components of AI
- Categories of Machine Learning
- Recurrent Neural Networks
- Recurrent Neural Networks

Machine Learning

- Introduction to Machine Learning & Python
- Machine Learning: Supervised Learning
- Machine Learning: Unsupervised Learning

Tableau

- Introduction to Tableau Desktop
- Data Transformation Methods
- Configuring tableau server
- Integration with R & Hadoop

- Salesforce
- Azure
- QA Testing
- SQL Server
- Business Analyst
- Hadoop
- AWS
- DevOps
- Data Science
- Java
- Digital Marketing
- Dotnet
- PMP
- Selenium
- Worth To Visit
- Machine Learning
- Python
- Oracle DBA
- Data Analyst
- Tableau
- Six Sigma
- Scrum Master
- Blockchain
- Artificial Intelligence
- Android
- Cyber Security
- VMware
- Online IT Training

Search Posts

Trending Posts

Related Posts

Receive Latest Materials and Offers on **Python Course**

**Interviews**

- Business Analyst Interview Questions
- DevOps Interview Questions
- AWS Interview Questions
- QA Testing Interview Questions
- Software Testing Interview Questions
- SQL Interview Questions
- Salesforce Interview Questions
- Java Interview Questions
- Hibernate Interview Questions
- Spark Interview Questions
- Vmware Interview Questions
- Data Science Interview Questions
- Digital Marketing Interview Questions
- API Testing Interview Questions
- SSAS Interview Questions
- Power BI Interview Questions
- Cloud Computing Interview Questions
- SSRS Interview Questions
- Manual Testing Interview Questions
- Social Media Interview Questions
- Performance Testing Interview Questions
- MSBI Interview Questions
- QTP Interview Questions
- Automation Testing Interview Questions
- SSIS Interview Questions
- GIT Interview Questions

## Sai

I need to to thank you for this very good read!! I definitely loved every little bit of it. I have bookmarked your site to check out new things you post…

## Manisha Karne

Through this post, I realize that your great information in playing with all the pieces was useful. I inform that this is the primary spot where I discover issues I've been looking for. You have a cunning yet alluring method of composing.