0% found this document useful (0 votes)
6 views

Prelab work AI(KCS751A) Student

Uploaded by

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

Prelab work AI(KCS751A) Student

Uploaded by

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

ABES Engineering College,

Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

Experiment-1 (A): Study of Prolog.

OBJECTIVE: Prolog programming basics

What is Prolog?
• Prolog stands for programming in logic.
• Prolog is a declarative language, which means that a program consists of data
based on the facts and rules (Logical relationship) rather than computing how
to find a solution.
• A logical relationship describes the relationships which hold for the given
application.
• To obtain the solution, the user asks a question rather than running a program.
When a user asks a question, then to determine the answer, the run time
system searches through the database of facts and rules.
• Prolog is a declarative language that means we can specify what problem we
want to solve rather than how to solve it.
• Prolog is used in some areas like database, natural language processing,
artificial intelligence, but it is pretty useless in some areas like a numerical
algorithm or instance graphics.
• In artificial intelligence applications, prolog is used. The artificial intelligence
applications can be automated reasoning systems, natural language interfaces,
and expert systems. The expert system consists of an interface engine and a
database of facts. The prolog's run time system provides the service of an
interface engine.

Applications of Prolog
• Specification Language
• Robot Planning
• Natural language understanding
• Machine Learning
• Problem Solving
• Intelligent Database retrieval
• Expert System
• Automated Reasoning

Starting Prolog
• Prolog system is straightforward.
• Prolog will produce a number of lines of headings in the starting, which is
followed by a line. It contains just
• ?-
• The above symbol shows the system prompt. The prompt is used to show that
the Prolog system is ready to specify one or more goals of sequence to the
user.
• Using a full stop, we can terminate the sequence of goals.
Subject Code: KCS-751A Subject Name: Artificial Intelligence
Lab
Name of Student: Roll No.: Page 1
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

• For example:
• ?- write('Welcome to AI lab Class'),nl,write('Example of Prolog'),nl.
• nl indicates 'start a new line'. When we press 'return' key, the above line will
show the effect like this:
Welcome to AI Lab Class
Example of Prolog
yes
• ?- prompt shows the sequence of goal which is entered by the user. The user
will not type the prompt.
• Prolog system will automatically generate this prompt. It means that it is ready
to receive a sequence of goals.
• To show that the goals have succeeded, we will output yes.
• 'Query' is a sequence of one or more goals.

Prolog Programs
• To write a Prolog program, firstly, the user has to write a program which is
written in the Prolog language, load that program, and then specify a sequence
of one or more goals at the prompt.
• To create a program in Prolog, the simple way is to type it into the text editor
and then save it as a text file like prolog1.pl.
• The following example shows a simple program of Prolog. The program
contains three components, which are known as clauses. Each clause is
terminated using a full stop.
dog(rottweiler).
cat(munchkin).
animal(A) :- cat(A).
• Using the built-in predicate 'consult', the above program can be loaded in the
Prolog system.

• ?-consult('prolog1.pl').
• This shows that prolog1.pl file exists, and the prolog program is systemically
correct, which means it has valid clauses, the goal will succeed, and to confirm
that the program has been correctly read, it produces one or more lines of
output. e.g.,
• ?-
# 0.00 seconds to consult prolog1.pl
?-
• The alternative of 'consult' is 'Load', which will exist on the menu option if the
Prolog system has a graphical user interface.
• When the program is loaded, the clause will be placed in a storage area, and
that storage area is known as the Prolog database. In response to the system
prompt, specify a sequence of goals, and it will cause Prolog to search for and
use the clauses necessary to evaluate the goals.

Subject Code: KCS-751A Subject Name: Artificial Intelligence


Lab
Name of Student: Roll No.: Page 2
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

Terminology
In the following program, three lines show the clauses.
dog(rottweiler).
cat(munchkin).
animal(A) :- cat(A).
Using the full stop, each clause will be terminated. Prolog programs have a
sequence of clauses. Facts or rules are described by these clauses.

Example of facts is dog(rottweiler) and cat(munchkin). They mean that


'rottweiler is a dog' and 'munchkin is a cat'.

Dog is called a predicate. Dog contains one argument. Word


'rottweiler' enclosed in bracket( ). Rottweiler is called an atom.

The example of rule is the final line of the program.


animal(A) :- dog(A).

The colon(:-) character will be read as 'if'. Here A is a variable, and it


represents any value. In a natural way, the rule can be read as "If A is an
animal, then A is a dog".

The above clause shows that the rottweiler is an animal.


Such deduction can also make by Prolog:
?- animal(rottweiler).
yes
To imply that munchkin is an animal, there is no evidence of this.
?- animal(munchkin).
no

Experiment-1 (B)
OBJECTIVE: Write simple fact for the statements using PROLOG.

Facts

Subject Code: KCS-751A Subject Name: Artificial Intelligence


Lab
Name of Student: Roll No.: Page 3
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

A fact is like a predicate expression. It is used to provide a declarative statement about


the problem. In a Prolog expression, when a variable occurs, it is assumed to be
universally quantified. Facts are specified in the form of the head. Head is known as
the clause head. It will take in the same way as the goal entered at the prompt by the
user.
cat(bengal). /* bengal is a cat */
dog(rottweiler). /* rottweiler is a dog */
likes(Jolie, Kevin). /* Jolie likes Kevin */
likes(A, Kevin). /* Everyone likes Kevin */
likes(Jolie, B). /* Jolie likes everybody */
likes(B, Jolie), likes(Jolie, B). /* Everybody likes Jolie and Jolie likes everybody *
/
likes(Jolie, Kevin); likes(Jolie, Ray). /* Jolie likes Kevin or Jolie likes Ray */
not(likes(Jolie, pasta)). /* Jolie does not like pasta */

Queries
In Prolog, the query is the action of asking the program about the information which
is available within its database. When a Prolog program is loaded, we will get the
query prompt,
?-
After this, we can ask about the information to the run time system. Using the above
simple database, we can ask a question to the program like
?- 'It is sunny'.
and it will give the answer
yes
?-
The system responds to the query with yes if the database information is consistent to
answer the query. Using the available database information, we can also check that
the program is capable of proving the query true. No indicates that the fact is not
deducible based on the available information.
The system answers no to the query if the database does not have sufficient
information.
?- 'It is cold'.
no
?-

Write simple fact for following:


a.Ram likes mango.
b. Seema is a girl.
c. Bill likes Cindy.
d. Rose is red.
e. John owns gold.
Program:

Subject Code: KCS-751A Subject Name: Artificial Intelligence


Lab
Name of Student: Roll No.: Page 4
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

Output:

Example 2.
Statements:

Statements in Prolog:

Program Clauses:

Screenshot Output-

Subject Code: KCS-751A Subject Name: Artificial Intelligence


Lab
Name of Student: Roll No.: Page 5
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

Experiment-2 (A)
OBJECTIVE: Write predicates One converts centigrade temperatures to
Fahrenheit, the other checks if a temperature is below freezing.

Formula for Centigrade (C) temperatures to Fahrenheit (F) -


F = C * 9 / 5 + 32

Rule
Centigrade to Fahrenheit (c_to_f)F is C * 9 / 5 + 32

Program-

Goal to find Fahrenheit temperature and freezing point

Output:

Screenshot Output-

Experiment-2 (B)
OBJECTIVE: Write a program to solve the Monkey Banana problem.
Subject Code: KCS-751A Subject Name: Artificial Intelligence
Lab
Name of Student: Roll No.: Page 6
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

Monkey wants the bananas but he can’t reach them.


What shall he do? The monkey is in the room.
Suspended from the roof, just out of his reach, is a bunch of bananas.
In the corner of the room is a box. The monkey desperately wants to grasp bananas.
After several unsuccessful attempts to reach the bananas:
1. The monkey walks to the box.
2. Pushes it under the bananas.
3. Climb on the box.
4. Picks the banana & eats them.

Program:

OUTPUT:

Screenshot Output:

Experiment-3
OBJECTIVE: Write a program to design medical diagnosis expert system in SWI
Prolog. The system must work for diagnosis of following diseases:-
a) measles
Subject Code: KCS-751A Subject Name: Artificial Intelligence
Lab
Name of Student: Roll No.: Page 7
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

b) germanmeasles
c) Flu
d) commoncold
e) mumps
f). chickenpox

Program Code:

OUTPUT:

Screenshot Output:

Subject Code: KCS-751A Subject Name: Artificial Intelligence


Lab
Name of Student: Roll No.: Page 8
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

Experiment-4 (A)
OBJECTIVE: WAP to implement factorial, Fibonacci of a given number.

Program-

Goal- To find Factorial of the number.

Output-

Scrennshot output:

The Fibonacci sequence is a sequence where the next term is the sum of the previous
two terms. The first two terms of the Fibonacci sequence are 0 followed by 1.

The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21


Fibonacci-

Program-

Goal- To find Fibonacci number.

Output:

Scrennshot output:

Experiment-4 (B)
OBJECTIVE: Write a program to solve 4-Queen problem.
Subject Code: KCS-751A Subject Name: Artificial Intelligence
Lab
Name of Student: Roll No.: Page 9
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

N queens problem is one of the most common examples of backtracking. Our goal is
to arrange N queens on an NxN chessboard such that no queen can strike down any
other queen. A queen can attack horizontally, vertically, or diagonally.

So, we start by placing the first queen anywhere arbitrarily and then place the next
queen in any of the safe places. We continue this process until the number of unplaced
queens becomes zero (a solution is found) or no safe place is left. If no safe place is
left, then we change the position of the previously placed queen.

Let's test this algorithm on a 4x4 chessboard.

Using Backtracking to Solve N Queens

The above picture shows a 4x4 chessboard and we have to place 4 queens on it.
So, we will start by placing the first queen in the first row.

Subject Code: KCS-751A Subject Name: Artificial Intelligence


Lab
Name of Student: Roll No.: Page 10
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

Now, the second step is to place the second queen in a safe position. Also, we
can't place the queen in the first row, so we will try putting the queen in the
second row this time.

Let's place the third queen in a safe position, somewhere in the third row.

Now, we can see that there is no safe place where we can put the last queen.
So, we will just change the position of the previous queen i.e., backtrack and
change the previous decision.

Also, there is no other position where we can place the third queen, so we will
go back one more step and change the position of the second queen.

Subject Code: KCS-751A Subject Name: Artificial Intelligence


Lab
Name of Student: Roll No.: Page 11
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

And now we will place the third queen again in a safe position other than
the previously placed position in the third row.

We will continue this process and finally, we will get the solution as shown
below.

Program:

OUTPUT:

Subject Code: KCS-751A Subject Name: Artificial Intelligence


Lab
Name of Student: Roll No.: Page 12
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

Screenshot Output:

Experiment-5
OBJECTIVE: Write a program to solve traveling salesman problem.

Subject Code: KCS-751A Subject Name: Artificial Intelligence


Lab
Name of Student: Roll No.: Page 13
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

Given a set of cities and distances between every pair of cities, the problem is to find
the shortest possible route that visits every city exactly once and returns to the starting
point.

Note the difference between Hamiltonian Cycle and TSP. The Hamiltonian cycle
problem is to find if there exists a tour that visits every city exactly once. Here we
know that Hamiltonian Tour exists (because the graph is complete) and in fact, many
such tours exist, the problem is to find a minimum weight Hamiltonian Cycle.

For example, consider the graph shown in the figure on the right side. A TSP tour in
the graph is 1-2-4-3-1. The cost of the tour is 10+25+30+15 which is 80.

The problem is a famous NP-hard problem. There is no polynomial-time known


solution for this problem.

Output of Given Graph:


minimum weight Hamiltonian Cycle :
10 + 25 + 30 + 15 := 80

Steps for implementation:


1. Consider city 1 as the starting and ending point. Since the
route is cyclic, we can consider any point as a starting point.
2. Generate all (n-1)! Permutations of cities.
3. Calculate the cost of every permutation and keep track of the
minimum cost permutation.
4. Return the permutation with minimum cost.

Program:

Subject Code: KCS-751A Subject Name: Artificial Intelligence


Lab
Name of Student: Roll No.: Page 14
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

Output

Screenshot Output:

Experiment-6
OBJECTIVE: Write a program to solve water jug problem.

Problem: You are given two jugs, a 4-gallon one and a 3-gallon one. Neither has any
measuring mark on it. There is a pump that can be used to fill the jugs with water.
How can you get exactly 2 gallons of water into the 4-gallon jug.

Solution:
Subject Code: KCS-751A Subject Name: Artificial Intelligence
Lab
Name of Student: Roll No.: Page 15
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

• The state space for this problem can be described as the set of ordered pairs of
integers (x,y)
• Where,
• X represents the quantity of water in the 4-gallon jug X= 0,1,2,3,4
• Y represents the quantity of water in 3-gallon jug Y=0,1,2,3
• Start State: (0,0)
• Goal State: (2,0)

Generate production rules for the water jug problem

Production Rules:

OUTPUT:

Screenshot Output:

Experiment-7
OBJECTIVE: Write a Python program to Support vector Machine algorithm.

SVM is one of the most popular Supervised Learning algorithms, used for
Classification as well as Regression problems. However, primarily, it is used for
Classification problems in Machine Learning.

The goal of the SVM algorithm is to create the best line or decision boundary that can
segregate n-dimensional space into classes so that we can easily put the new data
Subject Code: KCS-751A Subject Name: Artificial Intelligence
Lab
Name of Student: Roll No.: Page 16
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

point in the correct category in the future. This best decision boundary is called a
hyperplane.
SVM chooses the extreme points/vectors that help in creating the hyperplane. These
extreme cases are called as support vectors, and hence algorithm is termed as
Support Vector Machine. Consider the below diagram in which there are two different
categories that are classified using a decision boundary or hyperplane:

Types of SVM
SVM can be of two types:
o Linear SVM: Linear SVM is used for linearly separable data, which means if
a dataset can be classified into two classes by using a single straight line, then
such data is termed as linearly separable data, and classifier is used called as
Linear SVM classifier.
o Non-linear SVM: Non-Linear SVM is used for non-linearly separated data,
which means if a dataset cannot be classified by using a straight line, then
such data is termed as non-linear data and classifier used is called as Non-
linear SVM classifier.
Hyperplane and Support Vectors in the SVM algorithm:
o Hyperplane: There can be multiple lines/decision boundaries to segregate the
classes in n-dimensional space, but we need to find out the best decision
boundary that helps to classify the data points. This best boundary is known as
the hyperplane of SVM.
The dimensions of the hyperplane depend on the features present in the
dataset, which means if there are 2 features (as shown in image), then
hyperplane will be a straight line. And if there are 3 features, then hyperplane
will be a 2-dimension plane.
We always create a hyperplane that has a maximum margin, which means the
maximum distance between the data points.
o Support Vectors:

Subject Code: KCS-751A Subject Name: Artificial Intelligence


Lab
Name of Student: Roll No.: Page 17
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

The data points or vectors that are the closest to the hyperplane and which
affect the position of the hyperplane are termed as Support Vector. Since these
vectors support the hyperplane, hence called a Support vector.

How does SVM works?

Linear SVM:
The working of the SVM algorithm can be understood by using an example. Suppose
we have a dataset that has two tags (green and blue), and the dataset has two features
x1 and x2. We want a classifier that can classify the pair(x1, x2) of coordinates in
either green or blue. Consider the below image:

So as it is 2-d space so by just using a straight line, we can easily separate these two
classes. But there can be multiple lines that can separate these classes. Consider the
below image:

Hence, the SVM algorithm helps to find the best line or decision boundary; this best
boundary or region is called as a hyperplane. SVM algorithm finds the closest point of
the lines from both the classes. These points are called support vectors. The distance
between the vectors and the hyperplane is called as margin. And the goal of SVM is
to maximize this margin. The hyperplane with maximum margin is called the optimal
hyperplane.

Subject Code: KCS-751A Subject Name: Artificial Intelligence


Lab
Name of Student: Roll No.: Page 18
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

Non-Linear SVM:
If data is linearly arranged, then we can separate it by using a straight line, but for
non-linear data, we cannot draw a single straight line. Consider the below image:

So to separate these data points, we need to add one more dimension. For linear data,
we have used two dimensions x and y, so for non-linear data, we will add a third
dimension z. It can be calculated as:

z=x2 +y2
By adding the third dimension, the sample space will become as below image:

So now, SVM will divide the datasets into classes in the following way. Consider the
below image:

Subject Code: KCS-751A Subject Name: Artificial Intelligence


Lab
Name of Student: Roll No.: Page 19
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

Program:
In the model the building part, you can use the cancer dataset, which is a very famous
multi-class classification problem. This dataset is computed from a digitized image of
a fine needle aspirate (FNA) of a breast mass. They describe characteristics of the cell
nuclei present in the image.

This data has two types of cancer classes: malignant (harmful) and benign (not
harmful).

Here, you can build a model to classify the type of cancer. The dataset is available in
the scikit-learn library or you can also download it from the UCI Machine Learning
Library.

Loading Data
Let's first load the required dataset you will use.

Exploring Data
After you have loaded the dataset, you might want to know a little bit more about it.
You can check feature and target names.

You can also check the shape of the dataset using shape.

Let's check top 5 records of the feature set.

Let's take a look at the target set.

Splitting Data
To understand model performance, dividing the dataset into a training set and a test
set is a good strategy.

Split the dataset by using the function train_test_split(). you need to pass 3
parameters features, target, and test_set size. Additionally, you can
use random_state to select records randomly.
Subject Code: KCS-751A Subject Name: Artificial Intelligence
Lab
Name of Student: Roll No.: Page 20
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

Generating Model
Let's build support vector machine model. First, import the SVM module and create
support vector classifier object by passing argument kernel as the linear kernel in
SVC() function.
Then, fit your model on train set using fit() and perform prediction on the test set
using predict().

Evaluating the Model


Let's estimate how accurately the classifier or model can predict the breast cancer of
patients.
Accuracy can be computed by comparing actual test set values and predicted values.

Experiment-8
OBJECTIVE: Write a python program for Decision tree algorithm.

Introduction: Decision Tree is a Supervised learning technique that can be used for
both classification and Regression problems, but mostly it is preferred for solving
Classification problems. It is a tree-structured classifier, where internal nodes
represent the features of a dataset, branches represent the decision
rules and each leaf node represents the outcome.

Subject Code: KCS-751A Subject Name: Artificial Intelligence


Lab
Name of Student: Roll No.: Page 21
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

Below are the two reasons for using the Decision tree:
1. Decision Trees usually mimic human thinking ability while making a decision,
so it is easy to understand.
2. The logic behind the decision tree can be easily understood because it shows a
tree-like structure.

Decision Tree Terminologies


 Root Node: Root node is from where the decision tree starts. It represents the
entire dataset, which further gets divided into two or more homogeneous sets.
 Leaf Node: Leaf nodes are the final output node, and the tree cannot be segregated
further after getting a leaf node.
 Splitting: Splitting is the process of dividing the decision node/root node into sub-
nodes according to the given conditions.
 Branch/Sub Tree: A tree formed by splitting the tree.
 Pruning: Pruning is the process of removing the unwanted branches from the tree.
 Parent/Child node: The root node of the tree is called the parent node, and other
nodes are called the child nodes.

How does the Decision Tree algorithm Work?


In a decision tree, for predicting the class of the given dataset, the algorithm starts
from the root node of the tree. This algorithm compares the values of root attribute
with the record (real dataset) attribute and, based on the comparison, follows the
branch and jumps to the next node.

For the next node, the algorithm again compares the attribute value with the other
sub-nodes and move further. It continues the process until it reaches the leaf node of
the tree. The complete process can be better understood using the below algorithm:
o Step-1: Begin the tree with the root node, says S, which contains the complete
dataset.
o Step-2: Find the best attribute in the dataset using Attribute Selection
Measure (ASM).

Subject Code: KCS-751A Subject Name: Artificial Intelligence


Lab
Name of Student: Roll No.: Page 22
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

o Step-3: Divide the S into subsets that contains possible values for the best
attributes.
o Step-4: Generate the decision tree node, which contains the best attribute.
o Step-5: Recursively make new decision trees using the subsets of the dataset
created in step -3. Continue this process until a stage is reached where you
cannot further classify the nodes and called the final node as a leaf node.

Attribute Selection Measures


While implementing a Decision tree, the main issue arises that how to select the best
attribute for the root node and for sub-nodes. So, to solve such problems there is a
technique which is called as Attribute selection measure or ASM. By this
measurement, we can easily select the best attribute for the nodes of the tree. There
are two popular techniques for ASM, which are:
o Information Gain
o Gini Index

1. Information Gain:
o Information gain is the measurement of changes in entropy after the
segmentation of a dataset based on an attribute.
o It calculates how much information a feature provides us about a class.
o According to the value of information gain, we split the node and build the
decision tree.
o A decision tree algorithm always tries to maximize the value of information
gain, and a node/attribute having the highest information gain is split first. It
can be calculated using the below formula:
1. Information Gain= Entropy(S)- [(Weighted Avg) *Entropy(each feature)
Entropy: Entropy is a metric to measure the impurity in a given attribute. It specifies
randomness in data. Entropy can be calculated as:
Entropy(s)= -P(yes)log2 P(yes)- P(no) log2 P(no)
Where,
o S= Total number of samples
o P(yes)= probability of yes
o P(no)= probability of no

2. Gini Index:
o Gini index is a measure of impurity or purity used while creating a decision
tree in the CART(Classification and Regression Tree) algorithm.
o An attribute with the low Gini index should be preferred as compared to the
high Gini index.
o It only creates binary splits, and the CART algorithm uses the Gini index to
create binary splits.
o Gini index can be calculated using the below formula:
Gini Index= 1- ∑jPj2

Subject Code: KCS-751A Subject Name: Artificial Intelligence


Lab
Name of Student: Roll No.: Page 23
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

PROGRAM:

First, start with importing necessary python packages −

Output

Experiment-9
OBJECTIVE: Write a python program for Gaussian Naïve Bayes Classifier.
It is a variant of Naive Bayes that follows Gaussian normal distribution and supports
continuous data.

Naive Bayes are a group of supervised machine learning classification algorithms


based on the Bayes theorem. It is a simple classification technique, but has high
functionality. They find use when the dimensionality of the inputs is high.
Bayes Theorem
Bayes Theorem can be used to calculate conditional probability. Being a powerful
tool in the study of probability, it is also applied in Machine Learning.

Subject Code: KCS-751A Subject Name: Artificial Intelligence


Lab
Name of Student: Roll No.: Page 24
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

Naive Bayes Classifier

Naive Bayes Classifiers are based on the Bayes Theorem. These classifiers assume
that the value of a particular feature is independent of the value of any other feature.
In a supervised learning situation, Naive Bayes Classifiers are trained very efficiently.
Naive Bayed classifiers need a small training data to estimate the parameters needed
for classification

Gaussian Naive Bayes


When working with continuous data, an assumption often taken is that the continuous
values associated with each class are distributed according to a normal (or Gaussian)
distribution. The likelihood of the features is assumed to be-

Sometimes assume variance

 is independent of Y (i.e., σi),

 or independent of Xi (i.e., σk)

 or both (i.e., σ)
Gaussian Naive Bayes supports continuous valued features and models each as
conforming to a Gaussian (normal) distribution.

An approach to create a simple model is to assume that the data is described by a


Gaussian distribution with no co-variance (independent dimensions) between
dimensions. This model can be fit by simply finding the mean and standard deviation
Subject Code: KCS-751A Subject Name: Artificial Intelligence
Lab
Name of Student: Roll No.: Page 25
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

of the points within each label, which is all what is needed to define such a
distribution.

The above illustration indicates how a Gaussian Naive Bayes (GNB) classifier works.
At every data point, the z-score distance between that point and each class-mean is
calculated, namely the distance from the class mean divided by the standard deviation
of that class.

Program:

OUTPUT:

Subject Code: KCS-751A Subject Name: Artificial Intelligence


Lab
Name of Student: Roll No.: Page 26
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:

Subject Code: KCS-751A Subject Name: Artificial Intelligence


Lab
Name of Student: Roll No.: Page 27

You might also like