Naive Bayes

How to implement Naive Bayes Classifier in Python

Machine learning is in great demand in the 21st century. It’s difficult to find any technology around us that is not influenced by machine learning nowadays. Be it machine learning or artificial intelligence, everything works on basic algorithms that comprise logic, mathematics, reasoning, and formulas. 

Algorithms are a proper set of instructions and rules that help in the sequential functioning of a computer system. Machine learning is a big cluster of different algorithms, rules, and calculations, and it has one very important algorithm, Naive Bayes. 

What is the Naive Bayes Classifier?

We all have in our school big math theorems, proofs, algorithms, etc. There is one common theorem in mathematics that is Bayes’ theorem in probability. The Naive Bayes classifier has come from mathematics itself. It is actually a collection of many classification algorithms based on Bayes’ Theorem. It is a big family of algorithms that follow a common principal that states every pair of features is always classified as independent of each other.

What are the types of Naive Bayes?

Naive Bayes is bifurcated into six types, and each one has a unique identity. 

  • Gaussian Naive Bayes
  • Multinomial Naive Bayes
  • Complement Naive Bayes
  • Bernoulli’s Naive Bayes
  • Categorical Naive Bayes
  • Out-of-core Naive Bayes model fitting

Implement the Naive Bayes Classifier in Python

Today we are going to learn step-by-step how to implement the Gaussian Naive Bayes Classifier in Python. We are going to use Google Collabory for the execution of our code. 

The easiest of all the types of Naive Bayes classifiers is Gaussian Naive Bayes, and the assumptions from this classifier are very easy to study and analyze.

Here are all the steps for implementing the Naive Bayes Classifier in Python.

Step 1: Import required libraries

First, we are going to import the required Python libraries. The libraries that we are using are NumPy, matplotlib, and seaborn. For easy execution of code, we are using declaring abbreviations of each and every library.

NumPy: It is used for scientific computation and enables all kinds of mathematical functions, such as linear algebra, algebraic routines, statistics, etc. 

Matplotlib: It is used to represent data in the form of graphs and figures.

Seaborn: It is used for making statistical-based graphs.

Import required libraries

 Step 2: Import the data

Import the dataHere you will see the output of the visualization of data has come in the form of a dot plot graph. We have two data sets, one in red dots and the other one in blue dots.

Step 3: Import Gaussian Naive Bayes

We are going to import the Gaussian naive Bayes model, and the model will be fitted in the x and y planes for the graph plotting.

Step 4: Generating New Data and Predicting It

Step 5: Plot the new data to get an idea of where the decision boundary is

Gaussian graphs are always quadratic in nature, but from this graph, we have inferred that there is a slightly curved boundary in the classifications. 

Step 6: Using Bayesian formalism for probabilistic classification

This is how we can implement the Naive Bayes Classifier in Python by using different libraries and using graphs and figures to analyze everything. 

Leave a Comment

Your email address will not be published. Required fields are marked *