0% found this document useful (0 votes)
13 views5 pages

Naïve Bayes Classifier Implementation

Uploaded by

Ritesh Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Naïve Bayes Classifier Implementation

Uploaded by

Ritesh Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Faculty of Engineering & Technology

Machine Learning Laboratory (203105403)


B. Tech CSE 4rd Year 7th Semester

Practical - 3
Aim: Write a program to implement the naïve Bayesian classifier for a sample
training data set stored as a .CSV file. Compute the accuracy of the classifier,
considering few test data sets.
Theory:
What is Naïve Bayesian classifier?
- The Naive Bayes classifier is a simple yet powerful probabilistic machine learning
algorithm that is commonly used for classification tasks. It is based on Bayes' theorem and
assumes that the features are conditionally independent given the class label. This
assumption is known as the "naive" assumption, which simplifies the calculation of
probabilities.
- In machine learning, Naïve Bayes classification is a straightforward and powerful
algorithm for the classification task. Naïve Bayes classification is based on applying Bayes’
theorem with strong independence assumption between the features. Naïve Bayes
classification produces good results when we use it for textual data analysis such as Natural
Language Processing.
- Naïve Bayes models are also known as simple Bayes or independent Bayes. All these
names refer to the application of Bayes’ theorem in the classifier’s decision rule. Naïve
Bayes classifier applies the Bayes’ theorem in practice. This classifier brings the power of
Bayes’ theorem to machine learning.

Given a feature vector X and a class label y, Bayes' theorem states:

Naive Bayes algorithm calculations:


- Naïve Bayes Classifier uses the Bayes’ theorem to predict membership probabilities for
each class such as the probability that given record or data point belongs to a particular class.
The class with the highest probability is considered as the most likely class. This is also
known as the Maximum

Enrollment No.: 2203051057087 Page no:


Div: 7A9(CSE)
Faculty of Engineering & Technology
Machine Learning Laboratory (203105403)
B. Tech CSE 4rd Year 7th Semester

-A Posteriori (MAP). The MAP for a hypothesis with 2 events A and B is MAP (A).
MAP (A) = max (P (A | B))
= max (P (B | A) * P (A))/P (B)
= max (P (B | A) * P (A))
Here, P (B) is evidence probability. It is used to normalize the result. It remains the same. So,
removing it would not affect the result. Naïve Bayes Classifier assumes that all the features
are unrelated to each other. Presence or absence of feature does not affect the other features.
The Naive Bayes classifier works as follows:
1. Training: Given a labelled training dataset, the classifier calculates the prior probability
P(y) for each class in the dataset. It also estimates the likelihood probability P(X|y) for
each feature given each class. This is done by assuming conditional independence
between the features.
2. Prediction: When a new unlabelled instance is presented, the
3. classifier calculates the posterior probability P(y|X) for each class using Bayes' theorem.
It then assigns the class label with the highest posterior probability as the predicted class
for that instance.
4. Handling Continuous Features: For continuous features, the Naive Bayes classifier
typically assumes a probability distribution, often Gaussian (hence called Gaussian
Naive Bayes), to estimate the likelihood probability.
5. Laplace Smoothing: To avoid zero probabilities when a feature value in the testing data
was not observed in the training data, Laplace smoothing (also known as additive
smoothing) is often applied. It adds a small constant to numerator and adjusts the
denominator accordingly.
6. Decision Rule: In some cases, the Naive Bayes classifier can be used for decision making by
considering the posterior probabilities. For example, in binary classification, if P(y=1|X) >
P(y=0|X), the instance is assigned to class 1; otherwise, it is assigned to class 0.

Types of Naive Bayes algorithm


- There are 3 types of Naïve Bayes algorithm. The 3 types are listed below:- 1).
Gaussian Naïve Bayes algorithm:

- When we have continuous attribute values, we made an assumption that the


values associated with each class are distributed according to Gaussian or Normal
distribution. For example, suppose the training data contains a continuous attribute x.
We first segment the data by the class, and then compute the mean and variance of x in
each class:

Enrollment No.: 2203051057087 Page no:


Div: 7A9(CSE)
Faculty of Engineering & Technology
Machine Learning Laboratory (203105403)
B. Tech CSE 4rd Year 7th Semester

2.) Multinomial Naïve Bayes algorithm:

- With a Multinomial Naïve Bayes model, samples (feature vectors) represent the frequencies
with which certain events have been generated by a multinomial (p1, . . . ,pn) where pi is the
probability that event i occurs. Multinomial Naïve Bayes algorithm is preferred to use on data
that is multinomially distributed. It is one of the standard algorithms which is used in text
categorization classification.
3.) Bernoulli Naïve Bayes algorithm:

- In multivariate Bernoulli event model, features are independent boolean variables describing
inputs. Just like the multinomial model, this model is also popular for document classification
tasks where binary term occurrence features are used rather than term frequencies.

❖ Dataset taken: IRIS Dataset.


- This data sets consists of 3 different types of irises’ (Setosa, Versicolour, and Virginica)
petal and sepal length, stored in a 150x4 [Link].
- The rows being the samples and the columns being: Sepal Length, Sepal Width, Petal
Length and Petal Width.
- No. of Rows: 150
- No. of Columns: 4

❖ Procedure:
#Step-1: Import python libraries.
import numpy as np import pandas as pd from sklearn.model_selection import
train_test_split from sklearn.naive_bayes import GaussianNB from [Link]
import accuracy_score,confusion_matrix,classification_report import
[Link] as plt import seaborn as sns
Enrollment No.: 2203051057087 Page no:
Div: 7A9(CSE)
Faculty of Engineering & Technology
Machine Learning Laboratory (203105403)
B. Tech CSE 4rd Year 7th Semester

#Step-2: Import IRIS Dataset

df=pd.read_csv("/content/[Link]") print([Link]())

#Step-3: Prepare the data for modelling

x=[Link]("species",axis=1) #features
y=df["species"] #Target variable

#Step-4: splitting the data into training and testing sets

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=42)

#Step-5: Training the Gaussian Naïve Bayes model

model=GaussianNB()
[Link]( x_train,y_train)

#Step-6: Make prediction on the tet set

y_pred=[Link](x_test)

#Step-7: Evaluate the model’s performance.

print("Accuracy:",accuracy_score(y_test,y_pred)) print("Confusion
Matrix") print(confusion_matrix(y_test,y_pred))
print("Classification Report")
print(classification_report(y_test,y_pred))

#Step-8: Plot Confusion Matrix

confusion_matrix(y_test,y_pred)
[Link](confusion_matrix(y_test,y_pred),annot=True,cmap="Blues") [Link]()

Output:

Enrollment No.: 2203051057087 Page no:


Div: 7A9(CSE)
Faculty of Engineering & Technology
Machine Learning Laboratory (203105403)
B. Tech CSE 4rd Year 7th Semester

Enrollment No.: 2203051057087 Page no:


Div: 7A9(CSE)

Common questions

Powered by AI

Confusion matrices offer a detailed breakdown of prediction outcomes by showing true positive, false positive, true negative, and false negative counts, which help in assessing the classifier's precision, recall, and overall accuracy. Classification reports complement this by providing a summary of precision, recall, f1-score, and support for each class, which is crucial for understanding a model's performance, particularly in imbalanced datasets .

The Naïve Bayes classifier is based on Bayes' theorem, which calculates the posterior probability of a class given features. It is considered 'naive' because it assumes that all features are conditionally independent given the class label, which simplifies the computation but may not hold true for all datasets .

During prediction, the Naïve Bayes classifier computes the posterior probability P(y|X) for each class using Bayes' theorem, which involves calculating the product of the likelihood P(X|y) and the prior probability P(y). The class with the highest posterior probability is assigned to the new instance. The classifier assumes conditional independence between features, simplifying the likelihood computation .

The Gaussian Naïve Bayes algorithm handles continuous features by assuming they follow a Gaussian (normal) distribution within each class. It estimates the likelihood probabilities using the class-specific mean and variance of features, which is different from other Naïve Bayes variants like Multinomial and Bernoulli, which assume discrete feature distributions .

The Maximum A Posteriori (MAP) estimate is used in Naïve Bayes to determine the most probable class given a feature vector. It maximizes the posterior probability P(y|X), equivalent to maximizing the product of the likelihood P(X|y) and prior P(y). The evidence P(X) is ignored as it remains constant across classes, focusing the decision on likelihood and prior .

The process begins with importing necessary libraries like numpy, pandas, and scikit-learn. The IRIS dataset is loaded and prepared by separating features from the target variable. The dataset is partitioned into training and testing sets. A GaussianNB model is then trained on the training data. Predictions are made on the test set, and model performance is evaluated using metrics like accuracy, confusion matrix, and classification report, followed by visualizing the confusion matrix with a heatmap .

The assumption of feature independence may lead to suboptimal performance in scenarios where features are actually correlated. This can result in inaccurate probability estimations, particularly when important interactions between features are ignored, limiting the classifier's ability to separate classes effectively .

The Gaussian Naïve Bayes model is advantageous for continuous data as it naturally models the distribution using means and variances of a Gaussian distribution, making it computationally efficient. It handles feature variability effectively, providing robust performance even with small dataset sizes due to fewer distributional assumptions compared to non-parametric methods .

Laplace smoothing is used in the Naïve Bayes classifier to handle the issue of zero probabilities. When a test instance contains a feature value not observed in the training data, the likelihood calculation results in a zero probability, potentially affecting the entire prediction. Laplace smoothing adds a small constant to the frequency counts, ensuring that all feature probabilities stay positive .

The Multinomial Naïve Bayes algorithm is well-suited for text categorization because it models feature vectors as frequencies of events, which aligns with the nature of text data where feature frequencies (e.g., word counts) are integral. Its multinomial distribution assumption effectively captures the occurrence patterns of words in text documents .

You might also like