 Grab Deal : Flat 20% off on live classes + 2 free self-paced courses! - SCHEDULE CALL - Python Blogs -

# Naive Bayes: An Easy To Interpret Classifier

Naive 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.

## Why call these as Naive?

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:

#### A). Conditional Probability:

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.

#### B). Bayes Theorem:

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 vs Java : Which Programming Language is Best for Your Career? 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.

#### C). Types of Naïve Bayes:

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: Top 154+ Python Interview Questions and Answers for Beginners and Experienced

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.

### Designing a Naïve Bayes based classifier:

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
```

Read: Python Pandas Tutorial Guide for Beginners
```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, (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%.

#### The pros and cons of Naïve Bayes based classifiers:

Pros:

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

Cons:

1. 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”.
2. 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:

1. Text Classification: Classifiers based on Naïve Bayes are mostly used in text classification as they outperform in case of multi-class classification.
2. 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.
3. Sentiment analysis: Sentiments like happy, sad, angry are also independent in nature. Thus, making an ideal use case.
4. Recommendation systems: Naïve Bayes based classifiers are also used in recommendation systems where collaborative filtering or independence in feature space is observed.

#### Concluding on Naïve Bayes:

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: Python Certifications Guide - Types, Exam Details, Preparation Tips FaceBook Twitter Google+ LinkedIn Pinterest Email

### Trending Courses AWS

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

0 day 25 Sep 2023 DevOps

• Intro to DevOps
• GIT and Maven
• Jenkins & Ansible
• Docker and Cloud Computing Upcoming Class

8 days 03 Oct 2023 Data Science

• Data Science Introduction
• Python & Intro to R Programming
• Machine Learning Upcoming Class

4 days 29 Sep 2023 • Architecture, HDFS & MapReduce
• Unix Shell & Apache Pig Installation
• HIVE Installation & User-Defined Functions
• SQOOP & Hbase Installation Upcoming Class

4 days 29 Sep 2023 Salesforce

• Salesforce Configuration Introduction
• Security & Automation Process
• Sales & Service Cloud
• Apex Programming, SOQL & SOSL Upcoming Class

1 day 26 Sep 2023 QA

• Introduction and Software Testing
• Software Test Life Cycle
• Automation Testing and API Testing
• Selenium framework development using Testing Upcoming Class

2 days 27 Sep 2023 • BA & Stakeholders Overview
• BPMN, Requirement Elicitation
• BA Tools & Design Documents
• Enterprise Analysis, Agile & Scrum Upcoming Class

5 days 30 Sep 2023 MS SQL Server

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

11 days 06 Oct 2023 Python

• Features of Python
• Python Editors and IDEs
• Data types and Variables
• Python File Operation Upcoming Class

4 days 29 Sep 2023 Artificial Intelligence

• Components of AI
• Categories of Machine Learning
• Recurrent Neural Networks
• Recurrent Neural Networks Upcoming Class

4 days 29 Sep 2023 Machine Learning

• Introduction to Machine Learning & Python
• Machine Learning: Supervised Learning
• Machine Learning: Unsupervised Learning Upcoming Class

39 days 03 Nov 2023 Tableau

• Introduction to Tableau Desktop
• Data Transformation Methods
• Configuring tableau server
• Integration with R & Hadoop Upcoming Class

4 days 29 Sep 2023

Search Posts

Related Posts

Python Skills for Staying Ahead in a Rapidly-Changing Field 2.8k

Python Conditional Statements : If, Else, Elif, Nested If & Switch Case 8.6k

Python String Methods - What should you know? 812.1k

How Long Does It Take To Learn Python? 8k

How To Make Python Developer Resumes For Professional & Freshers: Comprehensive Guide With Samples 220k

Receive Latest Materials and Offers on Python Course