×

Naïve Bayes Algorithm in Machine Learning

Introduction to Naïve Bayes Algorithm in Machine Learning

The Naïve Bayes algorithm is a classification algorithm that is based on the Bayes Theorem, such that it assumes all the predictors are independent of each other. Basically, it is a probability-based machine learning classification algorithm which tends out to be highly sophisticated. It is very easy to build and can be used for large datasets.

Bayes Theorem:

Bayes theorem defines the likelihood of occurrence of an event that is based on some previous conditions related to that event. Or in simple words, it can be said that it is a way of calculating the Posterior probability from Likelihood, Class Prior Probability, and Predictor Prior Probability.

Mathematically;

Bayes Theorem

Where, is Posterior Probability, is a likelihood, is Class Prior Probability, and is Predictor Prior Probability from the equation given above.

As Bayes Theorem is a foundation of the Naïve Bayes machine learning algorithm, it requires some independence assumptions. So, the Naive Bayes machine learning algorithm often depends upon the assumptions which are incorrect. As we are working with the same dataset that we used in previous models, so in Bayes theorem, it is required age and salary to be an independent variable, which is a fundamental assumption of Bayes theorem.

But in our case, we can clearly see that fundamentally, it is not the case. There is some sort of correlation between them. As the person gets older, more the experience is gained, and simultaneously salary is increased. It can be seen that they are not absolutely independent. There is some sort of relation between the two variables.

Given that they are not independent, Bayes theorem can't be applied to the machine learning algorithm. And this is the reason we apply Naïve Bayes to the machine learning algorithm. It is often applied at the time when the features and variables are not independent of each other. It is still applied and gives the best results.  

Types of Naïve Bayes Models

To build a Naïve Bayes model, the scikit learn library is used. There are three types of Naïve Bayes models that fall under the scikit learn python library, which is given below:

  • Gaussian Naïve Bayes
  • Multinomial Naïve Bayes
  • Bernoulli Naïve Bayes

Gaussian Naïve Bayes model:

In this, the continuous feature values are considered assuming that the values of data are drawn from Gaussian distribution.

Multinomial Naïve Bayes model:

It is mostly used to classify document problems. The attributes are assumed to be drawn from a simple multinomial distribution. The model is best fitted for the attributes that denote discrete counts.

Bernoulli Naïve Bayes model:

It is mostly used in text classification problems. It assumes the features to be in a binary form (i.e., either 0 or 1). Only one value is taken up to forecast the class.

We will start by importing the python libraries.

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd 

After importing the libraries, we will then import the dataset and undergo pre-processing phase in the same way as we carried out in the earlier models.

# Importing the dataset
dataset = pd.read_csv('Social_Network_Ads.csv')
X = dataset.iloc[:, [2, 3]].values
y = dataset.iloc[:, 4].values 
# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0) 
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)                                                                           

After pre-processing the data, we will fit the Naïve Bayes classifier to the training set. And for that, we will first import the GaussianNB class from scikit learn naïve bayes library, and then we will create its object naming it as a classifier. Here we do not need to input any parameter, we just need to call the class. We will then fit our classifier to the training set.  

# Fitting Naive Bayes to the Training set
from sklearn.naive_bayes import GaussianNB
classifier = GaussianNB()
classifier.fit(X_train, y_train) 

Since we have not passed any parameter in the above few lines, so we have GaussianNB in the output as shown below:

Output:

GaussianNB(priors=None, var_smoothing=1e-09)

Now that we are done fitting the classifier to the training set, we will now predict the test set results in the same manner as we did in the previous models. Basically, y_pred is the vector prediction containing all test set results. As we know, this is not a correct way to look at the incorrect predictions, and so we will construct a confusion matrix.

# Predicting the Test set results
y_pred = classifier.predict(X_test)
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred) 

Output:

we have 10 incorrect predictions out of 100

From the output image displayed above, it can be seen that we have 10 incorrect predictions out of 100.

Now we will get into the visualization part. In this step we will visualize both training set results as well as the test set results, by plotting a graph that will differentiate the region that predicts the users who will buy the SUV from the region predicting those users who will not but the SUV. It will be carried out in the same as we did in the earlier models.

# Visualising the Training set results
 from matplotlib.colors import ListedColormap
 X_set, y_set = X_train, y_train
 X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
  np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
 plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
  alpha = 0.75, cmap = ListedColormap(('red', 'green')))
 plt.xlim(X1.min(), X1.max()) 
 plt.ylim(X2.min(), X2.max())
 for i, j in enumerate(np.unique(y_set)):
  plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
  c = ListedColormap(('red', 'green'))(i), label = j)
 plt.title('Naive Bayes (Training set)')
 plt.xlabel('Age') 
 plt.ylabel('Estimated Salary')
 plt.legend()
 plt.show() 
we have a very beautiful smooth curve with no irregularities.

From the output image given above, it can be clearly seen that we have a very beautiful smooth curve with no irregularities. The naïve bayes model accomplished in gathering the older user with low estimated salary who bought the SUV in correct region, which was not classified correctly by the logistic regression and SVM model because of linearity. But in this case, the separator is a curve and it managed quite well to catch some of them.

# Visualising the Test set results
 from matplotlib.colors import ListedColormap
 X_set, y_set = X_test, y_test
 X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
  np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
 plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
  alpha = 0.75, cmap = ListedColormap(('red', 'green')))
 plt.xlim(X1.min(), X1.max()) 
 plt.ylim(X2.min(), X2.max())
 for i, j in enumerate(np.unique(y_set)):
  plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
  c = ListedColormap(('red', 'green'))(i), label = j)
 plt.title('Naive Bayes (Test set)')
 plt.xlabel('Age')
 plt.ylabel('Estimated Salary') 
 plt.legend()
 plt.show() 
we can clearly see the test set results.

From the above image, we can clearly see the test set results. The naïve bayes classifier was successful in predicting most of the red users (who dint bought the SUV) in the red region as well as the green users (who bought the SUV) in the green region. We do have some incorrect predictions that are the red users in the green region or vice versa, and on counting them, it comes out to be 10, which is exactly equal to the incorrect predictions that we calculated in the confusion matrix in the earlier step. Since the results are the same and it ended up in classifying correctly, so we can say it is actually a good classifier. We will obtain some more shapes of the contour (prediction region) in the next models.

Advantages of Naïve Bayes Algorithm

  1. It is the fastest algorithm and very easy to implement practically.
  2. It performs better classification than the logistic regression model, as very little data is required for training.
  3. It works well when the type of input data is categorical, but for numerical data, it presumes a normal distribution. 
  4. Both discrete, as well as continuous data, can be handled by the Naïve Bayes algorithm.
  5. It can be utilized for classification problems like binary and multiclass problems.

Disadvantages of Naïve Bayes Algorithm

  1. Feature Independence: It is one of the biggest disadvantages of the algorithm, as it is next to impossible to have independent sets of features.
  2. Zero Frequency: Suppose if a categorical variable has a specific category that is not witnessed in the training set, then, in that case, it will be assigned with a zero possibility that restricts it in making the prediction.

Applications of Naïve Bayes Algorithm

  1. It can be used for real-time prediction.
  2. It can be used for predicting multiple class variable's posterior probabilities.
  3. It is used for problems like text classification, filtering spam emails, and emotion analysis.

Related Topics

Azure Machine Learning

Machine learning algorithms are powerful methods and techniques which are high in terms of probability and used to give computers high power to compute the solution for large numbers of...

3 minutes read.

Diabetes Prediction using Machine Learning

Diabetes Mellitus (shortly known as Diabetes) is one of the fastest-growing diseases. Nowadays, many people are affected with diabetes for many reasons, irrespective of age. Recently, many people who belong...

6 minutes read.

Machine Learning Life Cycle

Machine learning has made it possible for computer systems to learn without explicit programming. However, how do machine learning systems operate? Therefore, it might be explained by the machine learning...

5 minutes read.

Perceptron in Machine Learning

Machine Learning is becoming a very important part of the technology industry day by day. Perceptron is one of the key things of Machine Learning. Mr. Frank Rosenblatt invented this...

3 minutes read.

Recommendation System in Machine Learning

In today's world, we are surrounded by machines and different types of gadgets. Machine learning is a technique which prepares machines to think and learn. A recommendation system is an...

2 minutes read.

Standardization in Machine Learning

In machine learning, we train our data to anticipate or categorize things in ways that aren't pre-programmed into the computer. As a result, firstly, the dataset or input data must...

6 minutes read.

Machine Learning Engineer Salary in Different Organisation

We are very familiar with the word Machine Learning nowadays. Machine Learning technologies are used everywhere. Many IT companies use this type of technologies to improve their product. This branch...

3 minutes read.

Machine Learning Projects for the Final Year Students

We live in a technologically advanced age where machines and various technologies are all around us. Machine learning is a method for teaching computers to think and learn. In the...

7 minutes read.

Decision Trees in Machine Learning

Introduction to Decision Trees Decision trees are one of the most powerful classification algorithm that falls under supervised learning-based algorithms. It is used as a tool for making predictions and can...

6 minutes read.

Epoch in Machine Learning

Machine Learning is one of the most used modern technologies in the world. Machine Learning helps human a lot to do their jobs at ease. Epoch is an important concept...

3 minutes read.

Overfitting and Underfitting in Machine Learning

We actually talk about prediction errors, which are a measure of a machine learning model's performance and accuracy. Think about the possibility that we are developing a machine learning model....

3 minutes read.

Chi-Square Test in Machine Learning

A statistical technique called the chi-square test is used to compare actual outcomes to predictions. This test aims to determine if a discrepancy between actual and projected data is caused...

6 minutes read.

Decision Tree Algorithm in Machine Learning

We are very familiar with the word Machine Learning nowadays. Machine Learning technologies are used everywhere. Many IT companies use this type of technologies to improve their product. Decision tree...

6 minutes read.

PCA in Machine Learning

Machine Learning is one of the most used modern technologies in our world. Machine Learning helps human a lot to do their jobs at ease. PCA is widely used Machine...

3 minutes read.

Student Performance Prediction Using Machine Learning

Machine learning is a powerful tool that can be used to analyze and make predictions about student performance. One of the key advantages of using machine learning for student performance...

11 minutes read.

Data Preprocessing in Machine Learning

Before starting a machine learning project, data is an essential thing needed before starting a project. The data used in ML projects is in CSV (Comma Separated Value) format. It...

9 minutes read.

Support Vector Machines

Introduction to SVM Support Vector Machines are part of the supervised learning model with an associated learning algorithm. It is the most powerful and flexible algorithm used for classification, regression, and...

8 minutes read.

Difference between AI/ ML/ Deep Learning

Nowadays, people are very much confused with the terms artificial intelligence, machine learning, and deep learning. For them, it seems to be that all these three concepts are the same,...

11 minutes read.

Logistic Regression in Machine learning

The Logistic regression model is a supervised learning model that is used to forecast the possibility of a target variable. The dependent variable would have two classes, or we can...

9 minutes read.

Machine Learning Clustering Algorithm

Introduction to ML Clustering Algorithm Clustering falls under unsupervised learning methods. In this, the machine is provided with a set of unlabeled data, and the machine is required to extract the structure from...

2 minutes read.