0% found this document useful (0 votes)
11 views9 pages

Exp 10

The document outlines the use of the Naïve Bayes Classifier for document classification, detailing its principles based on Bayes' theorem and its application in text classification. It explains the algorithm's assumptions of feature independence and provides a step-by-step guide on implementing the classifier using a dataset. Additionally, it includes code snippets for training the model and calculating accuracy, precision, and recall metrics.

Uploaded by

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

Exp 10

The document outlines the use of the Naïve Bayes Classifier for document classification, detailing its principles based on Bayes' theorem and its application in text classification. It explains the algorithm's assumptions of feature independence and provides a step-by-step guide on implementing the classifier using a dataset. Additionally, it includes code snippets for training the model and calculating accuracy, precision, and recall metrics.

Uploaded by

8367748261durga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

10 Assuming a set of documents that need to be classified, use the naïve

Bayesian Classifier model to perform this task. Built-in Java classes/API can
be used to write the program. Calculate the accuracy, precision, and recall
for your data set.

Naïve Bayes Classifier Algorithm


o Naïve Bayes algorithm is a supervised learning algorithm, which is
based on Bayes theorem and used for solving classification
problems.
o It is mainly used in text classification that includes a high-
dimensional training dataset.
o Naïve Bayes Classifier is one of the simple and most effective
Classification algorithms which helps in building the fast machine
learning models that can make quick predictions.
o It is a probabilistic classifier, which means it predicts on the
basis of the probability of an object.
o Some popular examples of Naïve Bayes Algorithm are spam
filtration, Sentimental analysis, and classifying articles.

Why is it called Naïve Bayes?


The Naïve Bayes algorithm is comprised of two words Naïve and Bayes,
Which can be described as:

o Naïve: It is called Naïve because it assumes that the occurrence of


a certain feature is independent of the occurrence of other features.
Such as if the fruit is identified on the bases of color, shape, and
taste, then red, spherical, and sweet fruit is recognized as an apple.
Hence each feature individually contributes to identify that it is an
apple without depending on each other.
o Bayes: It is called Bayes because it depends on the principle
of Bayes' Theorem.

Bayes' Theorem:
o Bayes' theorem is also known as Bayes' Rule or Bayes' law, which
is used to determine the probability of a hypothesis with prior
knowledge. It depends on the conditional probability.
o The formula for Bayes' theorem is given as:

Where,

P(A|B) is Posterior probability: Probability of hypothesis A on the


observed event B.

P(B|A) is Likelihood probability: Probability of the evidence given that


the probability of a hypothesis is true.

P(A) is Prior Probability: Probability of hypothesis before observing the


evidence.

P(B) is Marginal Probability: Probability of Evidence.

Working of Naïve Bayes' Classifier:


Working of Naïve Bayes' Classifier can be understood with the help of the
below example:

Suppose we have a dataset of weather conditions and corresponding


target variable "Play". So using this dataset we need to decide that
whether we should play or not on a particular day according to the
weather conditions. So to solve this problem, we need to follow the below
steps:

1. Convert the given dataset into frequency tables.


2. Generate Likelihood table by finding the probabilities of given
features.
3. Now, use Bayes theorem to calculate the posterior probability.
# Importing the libraries

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

# Importing the dataset

dataset = pd.read_csv('/content/Social_Network_Ads.csv')

dataset.head()

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)

print(" Actual output \n {}".format(y_test),"\n predict outputs:\n {}".format(y_pred))

import seaborn as sns

from sklearn.metrics import confusion_matrix , accuracy_score

cm = confusion_matrix(y_test , y_pred)

sns.heatmap(cm , annot=True)

from sklearn.metrics import accuracy_score

print ("Accuracy : ", accuracy_score(y_test, y_pred)*100)

# precision_score

from sklearn.metrics import precision_score

print("the precision_score of given model:",precision_score(y_test, y_pred)*100)

# recall_score

from sklearn.metrics import recall_score

print("the recall_score of given model:",recall_score(y_test, y_pred)*100)

# f1_score

from sklearn.metrics import f1_score

print("the f1_score of given model:",f1_score(y_test, y_pred)*100)

You might also like