Pract 8: Implement Naive Bayes algorithm
It is a classification technique based on Bayes' Theorem with an
independence assumption among predictors. In simple terms, a Naive
Bayes classifier assumes that the presence of a particular feature in a
class is unrelated to the presence of any other feature.
The Naïve Bayes classifier is a popular supervised machine learning
algorithm used for classification tasks such as text classification. It belongs to
the family of generative learning algorithms, which means that it models the
distribution of inputs for a given class or category. This approach is based on
the assumption that the features of the input data are conditionally
independent given the class, allowing the algorithm to make predictions
quickly and accurately.
Example of Naive Bayes Algorithm
For example, if a fruit is red, round, and about 3 inches wide, we might call it
an apple. Even if these things are related, each one helps us decide it’s
probably an apple. That’s why it’s called ‘Naive.
An NB model is easy to build and particularly useful for very large data sets.
Along with simplicity, Naive Bayes is known to outperform even highly
sophisticated classification methods.
Bayes theorem provides a way of computing posterior probability P(c|x) from
P(c), P(x) and P(x|c).
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the dataset
dataset = pd.read_csv('Social_Network_Ads.csv')
X = dataset.iloc[:, [2, 3]].values
y = dataset.iloc[:, -1].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.20,
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)
# Training the Naive Bayes model on the Training set
from sklearn.naive_bayes import GaussianNB
classifier = GaussianNB()
classifier.fit(X_train, y_train)
# Predicting the Test set results
y_pred = classifier.predict(X_test)
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix, accuracy_score
ac = accuracy_score(y_test,y_pred)
cm = confusion_matrix(y_test, y_pred)