0% found this document useful (0 votes)
571 views109 pages

Artifical Intelligence and Machine Learning Lab

Uploaded by

reshmap
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)
571 views109 pages

Artifical Intelligence and Machine Learning Lab

Uploaded by

reshmap
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
You are on page 1/ 109

ARTIFICIAL INTELLIGENCE & MACHINE LEARNING

LAB MANUAL

B.TECH

(III YEAR – I SEM)


(2023-24)

DEPARTMENT OF COMPUTATIONAL INTELLIGENCE


(AIML)

MALLA REDDY COLLEGE OF ENGINEERING & TECHNOLOGY


(Autonomous Institution – UGC, Govt. of India)

Recognized under 2(f) and 12 (B) of UGC ACT 1956


(Affiliated to JNTUH, Hyderabad, Approved by AICTE - Accredited by NBA & NAAC – ‘A’ Grade - ISO 9001:2015 Certified)
Maisammaguda, Dhulapally (Post Via. Hakimpet), Secunderabad – 500100, Telangana State, India
Department of Computer Science & Engineering
(Artificial Intelligence & Machine Learning)

Vision
To be a premier centre for academic excellence and research through innovative
interdisciplinary collaborations and making significant contributions to the
community, organizations, and society as a whole.

Mission
• To impart cutting-edge Artificial Intelligence technology in accordance with
industry norms.
• To instill in students a desire to conduct research in order to tackle challenging
technical problems for industry.
• To develop effective graduates who are responsible for their professional
growth, leadership qualities and are committed to lifelong learning.

Quality Policy

• To provide sophisticated technical infrastructure and to inspire students to


reach their full potential.
• To provide students with a solid academic and research environment for a
comprehensive learning experience.
• To provide research development, consulting, testing, and customized training
to satisfy specific industrial demands, thereby encouraging self-employment
and entrepreneurship among students.
Department of Computer Science & Engineering
(Artificial Intelligence & Machine Learning)

Programme Educational Objectives (PEO):

PEO1: To possess knowledge and analytical abilities in areas such as maths, science,
and fundamental engineering.
PEO2: To analyse, design, create products, and provide solutions to problems in
Computer Science and Engineering.
PEO3: To leverage the professional expertise to enter the workforce, seek higher
education, and conduct research on AI-based problem resolution.
PEO4: To be solution providers and business owners in the field of computer
science and engineering with an emphasis on artificial intelligence and machine
learning.

Programme Specific Outcomes (PSO):

After successful completion of the program a student is expected to have specific


abilities to:

PSO1: To understand and examine the fundamental issues with AI and ML


applications.
PSO2: To apply machine learning, deep learning, and artificial intelligence
approaches to address issues in social computing, healthcare, vision, language
processing, speech recognition, and other domains.
PSO3: Use cutting-edge AI and ML tools and technology to further your study and
research.
PROGRAM OUTCOMES (POs)

Engineering Graduates will be able to:

1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering


fundamentals, and an engineering specialization to the solution of complex engineering
problems.
2. Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.
3. Design / development of solutions: Design solutions for complex engineering problems and
design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, andthe cultural, societal, and environmental
considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and research
methods including design of experiments, analysis and interpretation of data, and synthesis of
the information to provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities
with an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to
the professional engineering practice.
7. Environment and sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and need
for sustainable development.
8. Ethics: Apply ethical principles and commit to professional ethics andresponsibilities and
norms of the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a memberor leader
in diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and write
effective reports and design documentation, make effective presentations, and give and
receive clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member and
leader in a team, to manage projects and in multi disciplinary environments.
12. Life- long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
Lab Objectives:

• Familiarity with the Prolog programming environment & Systematic introduction to Prolog
programming constructs
• Learning basic concepts of Prolog through illustrative examples and small exercises &
Understanding list data structure in Prolog.
• To introduce the basic concepts and techniques of Machine Learning and the need of
Machine Learning techniques in real-world problems.
• To provide understanding of various Machine Learning algorithms and the way to evaluate
performance of the Machine Learning algorithms.
• To apply Machine Learning to learn, predict and classify the real-world problems in the
Supervised Learning paradigms as well as discover the Unsupervised Learning paradigms of
Machine Learning.
• To understand, learn and design simple Artificial Neural Networks of Supervised Learning
for the selected problems.
• To understand the concept of Reinforcement Learning and Ensemble Methods.

• To inculcate in students professional and ethical attitude, multidisciplinary


approach and an ability to relate real-world issues and provide a cost effective
solution to it by developing ML applications.

• To provide student with an academic environment aware of excellence, written


ethical codes and guidelines and lifelong learning needed for a successful
professional career.

Lab Outcomes:
Upon successful completion of this course, the students will be able to:

• Apply various AI search algorithms (uninformed, informed, heuristic, constraint satisfaction,)


• Understand the fundamentals of knowledge representation, inference and theorem proving
using AI tools
• Understand the basic concepts and techniques of Machine Learning and the need of Machine
Learning techniques in real-world problems.

• Understand various Machine Learning algorithms and the way to evaluate


performance of the Machine Learning algorithms
• Apply Machine Learning to learn, predict and classify the real-world problems in the
Supervised Learning paradigms as well as discover the Unsupervised Learning paradigms of
Machine Learning.

• Understand, learn and design Artificial Neural Networks of Supervised Learning


for the selected problems.

• Understand the concept of Reinforcement Learning and Ensemble Methods.


B.Tech – CSE (Computational intelligence) R-20

Introduction about lab


System configurations are as follows:
• Hardware / Software’s installed: Intel® CORE™ i3-3240 [email protected]:
4GB / Anaconda Navigator or Python and Jupyter Notebook or Google Colab.

• Packages required to run the programs: Math, Scipy, Numpy, Matplotlib,


Pandas, Sklearn, Tensorflow, Keras etc.
• Systems are provided for students in the 1:1 ratio.
• Systems are assigned numbers and same system is allotted for students when they
do the lab.
• All Systems are configured in LINUX, it is open source and students can use any
different programming environments through package installation.

Guidelines to students
A. Standard operating procedure
a) Explanation on today’s experiment by the concerned faculty using PPT covering
the following aspects:
1) Name of the experiment
2) Aim
3) Software/Hardware requirements
4) Writing the python programs by the students
5) Commands for executing programs
Writing of the experiment in the Observation Book
The students will write the today’s experiment in the Observation book as per the
following format:
a) Name of the experiment
b) Aim
c) Writing the program
d) Viva-Voce Questions and Answers
e) Errors observed (if any) during compilation/execution

Signature of the Faculty

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

B. Guide Lines to Students in Lab

Disciplinary to be maintained by the students in the Lab

• Students are required to carry their lab observation book and record book with
completedexperiments while entering the lab.
• Students must use the equipment with care. Any damage is caused student is punishable.
• Students are not allowed to use their cell phones/pen drives/ CDs in labs.
• Students need to maintain proper dress code along with ID Card
• Students are supposed to occupy the computers allotted to them and are not supposed
totalk or make noise in the lab.
• Students, after completion of each experiment they need to be updated in observation
notes and same to be updated in the record.
• Lab records need to be submitted after completion of experiment and get it corrected
withthe concerned lab faculty.
• If a student is absent for any lab, they need to be completed the same experiment in the
free time before attending next lab.
Steps to perform experiments in the lab by the student

Step1: Students have to write the date, aim and for that experiment in the observation book.
Step2: Students have to listen and understand the experiment explained by the faculty and
note down the important points in the observation book.
Step3: Students need to write procedure/algorithm in the observation book.
Step4: Analyze and Develop/implement the logic of the program by the student in
respective platform
Step5: After approval of logic of the experiment by the faculty then the experiment has to be
executed on the system.

Step6: After successful execution the results are to be shown to the faculty and noted the
same in the observation book.
Step7: Students need to attend the Viva-Voce on that experiment and write the same in the
observation book.
Step8: Update the completed experiment in the record and submit to the concerned
facultyin-charge.

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Instructions to maintain the record


• Before start of the first lab they have to buy the record and bring the record to thelab.
• Regularly (Weekly) update the record after completion of the experiment and get itcorrected
with concerned lab in-charge for continuous evaluation. In case the record is lost inform the
same day to the faculty in charge and get the new record within 2 days the record has to be
submitted and get it corrected by the faculty.
• If record is not submitted in time or record is not written properly, the evaluation marks (5M)
will be deducted.

Awarding the marks for day to day evaluation


Total marks for day to day evaluation is 15 Marks as per Autonomous (JNTUH). These 15
Marks are distributed as:

Regularity 3 Marks
Program written 3 Marks
Execution & Result 3 Marks
Viva-Voce 3 Marks
Dress Code 3 Marks
Allocation of Marks for Lab Internal
Total marks for lab internal are 30 Marks as per Autonomous (JNTUH.)
These 30 Marks are distributed as:
Average of day to day evaluation marks: 15 Marks
Lab Mid exam: 10 Marks
VIVA & Observation: 5 Marks

Allocation of Marks for Lab External


Total marks for lab Internal and External are 70 Marks as per Autonomous / (JNTUH).
These 70 External Lab Marks are distributed as:
Program Written 30 Marks
Program Execution and Result 20 Marks
Viva-Voce 10 Marks
Record 10 Marks

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

C. General laboratory instructions


1. Students are advised to come to the laboratory at least 5 minutes before (to thestarting time),
those who come after 5 minutes will not be allowed into the lab.
2. Plan your task properly much before to the commencement, come prepared to thelab with
the synopsis / program / experiment details.
3. Student should enter into the laboratory with:
a. Laboratory observation notes with all the details (Problem statement, Aim, Algorithm,
Procedure, Program, Expected Output, etc.,) filled in for the lab session.
b. Laboratory Record updated up to the last session experiments and other utensils (if any)
needed in the lab.
c. Proper Dress code and Identity card.
4. Sign in the laboratory login register, write the TIME-IN, and occupy the computer system
allotted to you by the faculty.
5. Execute your task in the laboratory, and record the results / output in the lab observation note
book, and get certified by the concerned faculty.
6. All the students should be polite and cooperative with the laboratory staff, must maintain the
discipline and decency in the laboratory.
7. Computer labs are established with sophisticated and high end branded systems, which
should be utilized properly.
8. Students / Faculty must keep their mobile phones in SWITCHED OFF mode during the lab
sessions. Misuse of the equipment, misbehaviors with the staff and systems etc., will attract
severe punishment.
9. Students must take the permission of the faculty in case of any urgency to go out ; if anybody
found loitering outside the lab / class without permission during working hours will be
treated seriously and punished appropriately.
10. Students should LOG OFF/ SHUT DOWN the computer system before he/she leaves the
lab after completing the task (experiment) in all aspects. He/she must ensure the system / seat
is kept properly.

Head of the Department Principal

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

INDEX
Artificial Intelligence Programs Using PROLOG
S.No Name of the Program Page No

Study of PROLOG Programming language and its Functions. Write simple facts for
1.
the statements using PROLOG.

2. Implementation of Depth First Search for Water Jug problem.

3. Implementation of Breadth First Search for Tic-Tac-Toe problem.

4. Solve 8-puzzle problem using Best First Search. Write a program to Implement A*.

5. Write a PROLOG program to solve N-Queens problem.

6. Implementation of Traveling Salesman Problem.

Machine Learning Programs Using Python


S.No Name of the Program Page No
Implementation of Python Basic Libraries such as Statistics, Math, Numpy and
1. Scipy
a) Usage of methods such as floor(), ceil(), sqrt(), isqrt(), gcd() etc.
b) Usage of attributes of array such as ndim, shape, size, methods such as
sum(), mean(), sort(), sin() etc.
c) Usage of methods such as det(), eig() etc.
d) Consider a list datatype (1D) then reshape it into 2D, 3D matrix using
numpy
e) Generate random matrices using numpy
f) Find the determinant of a matrix using scipy
g) Find eigenvalue and eigenvector of a matrix using scipy

Implementation of Python Libraries for ML application such as Pandas and


2.
Matplotlib.
a) Create a Series using pandas and display
b) Access the index and the values of our Series
c) Compare an array using Numpy with a series using pandas
d) Define Series objects with individual indices
e) Access single value of a series
f) Load datasets in a Dataframe variable using pandas
g) Usage of different methods in Matplotlib.
a) Creation and Loading different types of datasets in Python using the required
3.
libraries.
i. Creation using pandas
ii. Loading CSV dataset files using Pandas
iii. Loading datasets using sklearn

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20
b) Write a python program to compute Mean, Median, Mode, Variance,
Standard Deviation using Datasets
c) Demonstrate various data pre-processing techniques for a given dataset.
Write a python program to compute
i. Reshaping the data,
ii. Filtering the data,
iii. Merging the data
iv. Handling the missing values in datasets
v. Feature Normalization: Min-max normalization
a) Write a program to demonstrate the working of the decision tree based ID3
4.
algorithm by considering a dataset.
b) Consider a dataset, use Random Forest to predict the output class. Vary the
number of trees as follows and compare the results:
i. 20
ii. 50
iii. 100
iv. 200
v. 500
a) Write a Python program to implement Simple Linear Regression and plot
5.
the graph.
b) Implementation of Logistic Regression for iris using sklearn.
a) Build KNN Classification model for a given dataset. Vary the number of k
6.
values as follows and compare the results:
i. 1
ii. 3
iii. 5
iv. 7
v. 11
b) Implement Support Vector Machine for a dataset and compare the
accuracy by applying the following kernel functions:
i. Linear
ii. Polynomial
iii. RBF
c) Write a python program to implement K-Means clustering Algorithm.
Vary the number of k values as follows and compare the results:
i. 1
ii. 3
iii. 5
iv. 7

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Artificial Intelligence Programs Using PROLOG


Week-1
Aim:
Study of PROLOG Programming language and its Functions. Write simple facts for the statements
using PROLOG.
OBJECTIVE: Study of PROLOG.
PROLOG-PROGRAMMING IN LOGIC
PROLOG stands for Programming, In Logic — an idea that emerged in the early 1970’s to use logic as
programming language. The early developers of this idea included Robert Kowaiski at Edinburgh (on the
theoretical side), Marrten van Emden at Edinburgh (experimental demonstration) and Alian Colmerauer at
Marseilles (implementation).
David D.H. Warren’s efficient implementation at Edinburgh in the mid -1970’s greatly contributed to the
popularity of PROLOG. PROLOG is a programming language centred around a small set of basic
mechanisms, Including pattern matching, tree based data structuring and automatic backtracking. This Small
set constitutes a surprisingly powerful and flexible programming framework. PROLOG is especially well
suited for problems that involve objects- in particular, structured objects- and relations between them.
SYMBOLIC LANGUAGE
PROLOG is a programming language for symbolic, non-numeric computation. It is especially well suited for
solving problems that involve objects and relations between objects.
For example, it is an easy exercise in PROLOG to express spatial relationship between objects, such as the
blue sphere is behind the green one. It is also easy to state a more general rule: if object X is closer to the
observer than object Y. and object Y is closer than Z, then X must be closer than Z. PROLOG can reason
about the spatial relationships and their consistency with respect to the general rule. Features like this make
PROLOG a powerful language for ArtJIcia1 LanguageA1,) and non- numerical programming.
There are well-known examples of symbolic computation whose implementation in other standard languages
took tens of pages of indigestible code, when the same algorithms were implemented in PROLOG, the result
was a crystal-clear program easily fitting on one page.

FACTS, RULES AND QUERIES


Programming in PROIOG is accomplished by creating a database of facts and rules about objects, their
properties, and their relationships to other objects. Queries then can be posed about the objects and valid
conclusions will be determined and returned by the program Responses to user queries are determined
through a form of inference control known as resolution.
FOR EXAMPLE:
a. Ram likes mango.
b. Seema is a girl.
c. Bill likes Cindy.
d. Rose is red.
e. John owns gold.

Program:
Clauses
likes(ram ,mango).
girl(seema).
red(rose).
likes(bill ,cindy).
owns(john ,gold).

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20
Output:
queries
?-likes(ram,What).
What= mango

?-likes(Who,cindy).
Who= cindy
?-red(What).
What= rose
?-owns(Who,What).
Who= john
What= gold.

Viva Questions:
1- First create a source file for the genealogical logicbase application. Start by adding a few members of your family
tree. It is important to be accurate, since we will be exploring family relationships. Your own knowledge of who your
relatives are will verify the correctness of your Prolog programs.
2- Enter a two-argument predicate that records the parent-child relationship. One argument represents the parent, and
the other the child. It doesn't matter in which order you enter the arguments, as long as you are consistent. Often
Prolog programmers adopt the convention that parent(A,B) is interpreted "A is the parent of B".
3- Create a source file for the customer order entry program. We will begin it with three record types (predicates). The
first is customer/3 where the three arguments are
arg1
Customer name
arg2
City
arg3
Credit rating (aaa, bbb, etc)
4- Next add clauses that define the items that are for sale. It should also have three arguments
arg1
Item identification number
arg2
Item name
arg3
The reorder point for inventory (when at or below this level, reorder)
5- Next add an inventory record for each item. It has two arguments.
arg1
Item identification number (same as in the item record)
arg2
Amount in stock
Assignment:
Aim: Write facts for following:
1. Ram likes apple.
2. Ram is taller then Mohan.
3. My name is Subodh.
4. Apple is fruit.
5. Orange is fruit.
6. Ram is male.
AIM: Write simple queries for following facts.
Simple Queries
Now that we have some facts in our Prolog program, we can consult the program in the listener and query, or call, the

Machine Learning MRCET CAMPUS


B.Tech
facts. This – CSE and
chapter, (Computational
the next, willintelligence)
assume the Prolog program contains only facts. Queries againstR-20
programs with
rules will be covered in a later chapter.
Prolog queries work by pattern matching. The query pattern is called a goal. If there is a fact that matches the goal,
then the query succeeds and the listener responds with 'yes.' If there is no matching fact, then the query fails and the
listener responds with 'no.'
Prolog's pattern matching is called unification. In the case where the logicbase contains only facts,
unification succeeds if the following three conditions hold.
● The predicate named in the goal and logic base are the same.
● Both predicates have the same arity.
● All of the arguments are the same.
Before proceeding, review figure 3.1, which has a listing of the program so far.
The first query we will look at asks if the office is a room in the game. To pose this, we would enter that
goal followed by a period at the listener prompt.

?- room(office).
yes
Prolog will respond with a 'yes' if a match was found. If we wanted to know if the attic was a room, we
would enter that goal.
?- room(attic).
No

Solution:-
clauses
likes(ram ,mango).
girl(seema).
red(rose).
likes(bill ,cindy).
owns(john ,gold).

queries
?-likes(ram,What).
What= mango
?-likes(Who,cindy).
Who= cindy
?-red(What).
What= rose
?-owns(Who,What).
Who= john
What= gold
Viva Questions:

1- Consider the following Prolog logic base


easy(1).
easy(2).
easy(3).
gizmo(a,1).
gizmo(b,3).
gizmo(a,2).
gizmo(d,5).
gizmo(c,3).
gizmo(a,3).
gizmo(c,4).

Machine Learning MRCET CAMPUS


B.Techthe
and predict – CSE (Computational
answers intelligence)
to the queries R-20
below, including all alternatives when the semicolon (;) is entered
after an answer.
?- easy(2).
?- easy(X).
?- gizmo(a,X).
?- gizmo(X,3).
?- gizmo(d,Y).
?- gizmo(X,X).
2- Consider this logicbase,
harder(a,1).
harder(c,X).
harder(b,4).
harder(d,2).
and predict the answers to these queries.
?- harder(a,X).
?- harder(c,X).
?- harder(X,1).
?- harder(X,4).
3- Enter the listener and reproduce some of the example queries you have seen against location/2. List or
print location/2 for reference if you need it. Remember to respond with a semicolon (;) for multiple answers.
Trace the query.
4- Pose queries against the genealogical logicbase that:
● Confirm a parent relationship such as parent(dennis, diana)
● Find someone's parent such as parent(X, diana)
● Find someone's children such as parent(dennis, X)
● List all parent-children such as parent(X,Y)
5- If parent/2 seems to be working, you can add additional family members to get a larger logicbase.
Remember to include the corresponding male/1 or female/1 predicate for each individual added.

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Week-2
Aim:
Implementation of Depth First Search for Water Jug problem.
Theory/Description:
In the water jug problem in Artificial Intelligence, we are provided with two jugs: one having the capacity to
hold 3 gallons of water and the other has the capacity to hold 4 gallons of water. There is no other measuring
equipment available and the jugs also do not have any kind of marking on them. So, the agent’s task here is
to fill the 4-gallon jug with 2 gallons of water by using only these two jugs and no other material. Initially,
both our jugs are empty.
So, to solve this problem, following set of rules were proposed:
Production rules for solving the water jug problem
Solution:
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

Initialization:
Start State: (0,0)
Apply Rule 2:
(X,Y | Y<3) ->
(X,3)
{Fill 3-gallon jug}
Now the state is (X,3)
Iteration 1:
Current State: (X,3)
Apply Rule 7:
(X,Y | X+Y<=4 ^Y>0)
(X+Y,0)
{Pour all water from 3-gallon jug into 4-gallon jug}
Now the state is (3,0)

Machine Learning MRCET CAMPUS


B.Tech
Iteration 2: – CSE (Computational intelligence) R-20

Current State : (3,0)


Apply Rule 2:
(X,Y | Y<3) ->
(3,3)
{Fill 3-gallon jug}
Now the state is (3,3)
Iteration 3:
Current State:(3,3)
Apply Rule 5:
(X,Y | X+Y>=4 ^ Y>0)
(4,Y-(4-X))
{Pour water from 3-gallon jug into 4-gallon jug until 4-gallon jug is full}
Now the state is (4,2)
Iteration 4:
Current State : (4,2)
Apply Rule 3:
(X,Y | X>0)
(0,Y)
{Empty 4-gallon jug}
Now state is (0,2)
Iteration 5:
Current State : (0,2)
Apply Rule 9:
(0,2)
(2,0)
{Pour 2 gallon water from 3 gallon jug into 4 gallon jug}
Now the state is (2,0)
Goal Achieved.

Progaram:
solve_dfs(State, History, []) :- final_state(State).
solve_dfs(State, History, [Move|Moves]) :-

Machine Learning MRCET CAMPUS


B.Tech – CSE
move(State, (Computational intelligence)
Move), R-20

update(State, Move, State1),


legal(State1),
not(member(State1, History)),
solve_dfs(State1, [State1|History], Moves).

test_dfs(Problem, Moves) :-
initial_state(Problem, State), solve_dfs(State, [State], Moves).

capacity(1, 10).
capacity(2, 7).

initial_state(jugs, jugs(0, 0)).


final_state(jugs(6, 0)).
%final_state(jugs(4, 0)).

legal(jugs(V1, V2)).

move(jugs(V1, V2), fill(1)) :- capacity(1, C1), V1 < C1, capacity(2, C2), V2 < C2.
move(jugs(V1, V2), fill(2)) :- capacity(2, C2), V2 < C2, capacity(1, C1), V1 < C1.

move(jugs(V1, V2), empty(1)) :- V1 > 0.


move(jugs(V1, V2), empty(2)) :- V2 > 0.
move(jugs(V1, V2), transfer(1, 2)).
move(jugs(V1, V2), transfer(2, 1)).

adjust(Liquid, Excess, Liquid, 0) :- Excess =< 0.


adjust(Liquid, Excess, V, Excess) :- Excess > 0, V is Liquid - Excess.

update(jugs(V1, V2), fill(1), jugs(C1, V2)) :- capacity(1, C1).


update(jugs(V1, V2), fill(2), jugs(V1, C2)) :- capacity(2, C2).
update(jugs(V1, V2), empty(1), jugs(0, V2)).
update(jugs(V1, V2), empty(2), jugs(V1, 0)).

update(jugs(V1, V2), transfer(1, 2),jugs(NewV1, NewV2)) :-


capacity(2, C2),
Liquid is V1 + V2,
Excess is Liquid - C2,
adjust(Liquid, Excess, NewV2, NewV1).

update(jugs(V1, V2), transfer(2, 1),jugs(NewV1, NewV2)) :-


capacity(1, C1),
Liquid is V1 + V2,

Machine Learning MRCET CAMPUS


B.Techis– Liquid
Excess CSE (Computational
- C1, intelligence) R-20

adjust(Liquid, Excess, NewV1, NewV2).

Output:
Query: test_dfs(jugs, Moves).
Moves = [fill(1),
transfer(1,2),
empty(1),
transfer(2,1),
fill(2),
transfer(2,1),
empty(1),
transfer(2,1),
fill(2),
transfer(2,1),
empty(1),
transfer(2,1),
fill(2),
transfer(2,1),
fill(2),
transfer(2,1),
empty(1),
transfer(2,1),
fill(2),
transfer(2,1),
empty(1),
transfer(2,1),
fill(2),
transfer(2,1),
fill(2),
transfer(2,1),
empty(1),
transfer(2,1)]

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Week-3
Aim:
Implementation of Breadth First Search for Tic-Tac-Toe problem.
Theory/Description:
The game Tic Tac Toe is also known as Noughts and Crosses or Xs and Os ,the player needs to take turns
marking the spaces in a 3x3 grid with their own marks,if 3 consecutive marks
(Horizontal, Vertical,Diagonal) are formed then the player who owns these moves get won.

Assume ,

Player 1 - X
Player 2 - O

So,a player who gets 3 consecutive marks first,they will win the game .

Let's have a discussion about how a board's data structure looks and how the Tic Tac Toe algorithm works.

Board's Data Structure:

The cells could be represent as Center square,Corner,Edge as like below

Number each square starts from 1 to 9 like following image

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Consider a Board having nine elements vector.Each element will contain


• 0 for blank
• 1 indicating X player move
• 2 indicating O player move
Computer may play as X or O player. First player is always X.

Move Table
It is a vector of 3^9 elements, each element of which is a nine element vector representing board
position.
Total of 3^9(19683) elements in move table
Move Table
Index Current Board position New Board position
0 000000000 000010000
1 000000001 020000001
2 000000002 000100002
3 000000010 002000010
.
.
Algorithm
To make a move, do the following:
1. View the vector (board) as a ternary number and convert it to its corresponding decimal number.
2. Use the computed number as an index into the move table and access the vector stored there.
3. The vector selected in step 2 represents the way the board will look after the move that should be
made. So set board equal to that vector.
Let's start with empty board

Step 1:Now our board looks like 000 000 000 (tenary number) convert it into decimal no.The
decimal no is 0
Step 2:Use the computed number ie 0 as an index into the move table and access the vector stored in
New Board Position.
The new board position is 000 010 000
Step 3:The vector selected in step 2(000 010 000 ) represents the way the board will look after the
move that should be made. So set board equal to that vector.
After complete the 3rd step your board looks like\

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Program:

win(Board, Player) :- rowwin(Board, Player).


win(Board, Player) :- colwin(Board, Player).
win(Board, Player) :- diagwin(Board, Player).

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20
rowwin(Board, Player) :- Board = [Player,Player,Player,_,_,_,_,_,_].
rowwin(Board, Player) :- Board = [_,_,_,Player,Player,Player,_,_,_].
rowwin(Board, Player) :- Board = [_,_,_,_,_,_,Player,Player,Player].

colwin(Board, Player) :- Board = [Player,_,_,Player,_,_,Player,_,_].


colwin(Board, Player) :- Board = [_,Player,_,_,Player,_,_,Player,_].
colwin(Board, Player) :- Board = [_,_,Player,_,_,Player,_,_,Player].

diagwin(Board, Player) :- Board = [Player,_,_,_,Player,_,_,_,Player].


diagwin(Board, Player) :- Board = [_,_,Player,_,Player,_,Player,_,_].

% Helping predicate for alternating play in a "self" game:

other(x,o).
other(o,x).

game(Board, Player) :- win(Board, Player), !, write([player, Player, wins]).


game(Board, Player) :-
other(Player,Otherplayer),
move(Board,Player,Newboard),
!,
display(Newboard),
game(Newboard,Otherplayer).

move([b,B,C,D,E,F,G,H,I], Player, [Player,B,C,D,E,F,G,H,I]).


move([A,b,C,D,E,F,G,H,I], Player, [A,Player,C,D,E,F,G,H,I]).
move([A,B,b,D,E,F,G,H,I], Player, [A,B,Player,D,E,F,G,H,I]).
move([A,B,C,b,E,F,G,H,I], Player, [A,B,C,Player,E,F,G,H,I]).
move([A,B,C,D,b,F,G,H,I], Player, [A,B,C,D,Player,F,G,H,I]).
move([A,B,C,D,E,b,G,H,I], Player, [A,B,C,D,E,Player,G,H,I]).
move([A,B,C,D,E,F,b,H,I], Player, [A,B,C,D,E,F,Player,H,I]).
move([A,B,C,D,E,F,G,b,I], Player, [A,B,C,D,E,F,G,Player,I]).
move([A,B,C,D,E,F,G,H,b], Player, [A,B,C,D,E,F,G,H,Player]).

display([A,B,C,D,E,F,G,H,I]) :- write([A,B,C]),nl,write([D,E,F]),nl,
write([G,H,I]),nl,nl.

selfgame :- game([b,b,b,b,b,b,b,b,b],x).

% Predicates to support playing a game with the user:

x_can_win_in_one(Board) :- move(Board, x, Newboard), win(Newboard, x).

% The predicate orespond generates the computer's (playing o) reponse


% from the current Board.

orespond(Board,Newboard) :-
move(Board, o, Newboard),
win(Newboard, o),
!.
orespond(Board,Newboard) :-

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20
move(Board, o, Newboard),
not(x_can_win_in_one(Newboard)).
orespond(Board,Newboard) :-
move(Board, o, Newboard).
orespond(Board,Newboard) :-
not(member(b,Board)),
!,
write('Cats game!'), nl,
Newboard = Board.

% The following translates from an integer description


% of x's move to a board transformation.

xmove([b,B,C,D,E,F,G,H,I], 1, [x,B,C,D,E,F,G,H,I]).
xmove([A,b,C,D,E,F,G,H,I], 2, [A,x,C,D,E,F,G,H,I]).
xmove([A,B,b,D,E,F,G,H,I], 3, [A,B,x,D,E,F,G,H,I]).
xmove([A,B,C,b,E,F,G,H,I], 4, [A,B,C,x,E,F,G,H,I]).
xmove([A,B,C,D,b,F,G,H,I], 5, [A,B,C,D,x,F,G,H,I]).
xmove([A,B,C,D,E,b,G,H,I], 6, [A,B,C,D,E,x,G,H,I]).
xmove([A,B,C,D,E,F,b,H,I], 7, [A,B,C,D,E,F,x,H,I]).
xmove([A,B,C,D,E,F,G,b,I], 8, [A,B,C,D,E,F,G,x,I]).
xmove([A,B,C,D,E,F,G,H,b], 9, [A,B,C,D,E,F,G,H,x]).
xmove(Board, _, Board) :- write('Illegal move.'), nl.

% The 0-place predicate playo starts a game with the user.

playo :- explain, playfrom([b,b,b,b,b,b,b,b,b]).

explain :-
write('You play X by entering integer positions followed by a period.'),
nl,
display([1,2,3,4,5,6,7,8,9]).

playfrom(Board) :- win(Board, x), write('You win!').


playfrom(Board) :- win(Board, o), write('I win!').
playfrom(Board) :- read(N),
xmove(Board, N, Newboard),
display(Newboard),
orespond(Newboard, Newnewboard),
display(Newnewboard),
playfrom(Newnewboard).

Output:
Query: playo.
You play X by entering integer positions followed by a period.
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

Machine Learning MRCET CAMPUS


1 B.Tech – CSE (Computational intelligence) R-20
[x, b, b]
[b, b, b]
[b, b, b]

[x, o, b]
[b, b, b]
[b, b, b]

9
[x, o, b]
[b, b, b]
[b, b, x]

[x, o, b]
[b, o, b]
[b, b, x]

7
[x, o, b]
[b, o, b]
[x, b, x]

[x, o, b]
[b, o, b]
[x, o, x]

I win!
1true

Another Output:
[x, b, b]
[b, b, b]
[b, b, b]

[x, o, b]
[b, b, b]
[b, b, b]

[x, o, x]
[b, b, b]
[b, b, b]

[x, o, x]
[o, b, b]
[b, b, b]

[x, o, x]
[o, x, b]
[b, b, b]

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20
[x, o, x]
[o, x, o]
[b, b, b]

[x, o, x]
[o, x, o]
[x, b, b]

[x, o, x]
[o, x, o]
[x, o, b]

[player, x, wins]
1true

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Week:4
Aim:
Solve 8-puzzle problem using best first search.
Theory/Description:
Eight puzzle problem by the name of N puzzle problem or sliding puzzle problem.
N-puzzle that consists of N tiles (N+1 titles with an empty tile) where N can be 8, 15, 24 and so on.
In our example N = 8. (that is square root of (8+1) = 3 rows and 3 columns).
In the same way, if we have N = 15, 24 in this way, then they have Row and columns as follow (square root
of (N+1) rows and square root of (N+1) columns).
That is if N=15 than number of rows and columns= 4, and if N= 24 number of rows and columns= 5.
So, basically in these types of problems we have given a initial state or initial configuration (Start state) and
a Goal state or Goal Configuration.
Here We are solving a problem of 8 puzzle that is a 3x3 matrix.
Initial state Goal state

Solution:
The puzzle can be solved by moving the tiles one by one in the single empty space and thus achieving the
Goal state.
Rules of solving puzzle
Instead of moving the tiles in the empty space we can visualize moving the empty space in place of the tile.
The empty space can only move in four directions (Movement of empty space)
1. Up

2. Down

3. Right or

4. Left

The empty space cannot move diagonally and can take only one step at a time.
All possible move of a Empty tile

o- Position total possible moves are (2), x - position total possible moves are (3) and
#-position total possible moves are (4)
Let's solve the problem without Heuristic Search that is Uninformed Search or Blind Search
Solve Eight puzzle problem

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Note: If we solve this problem with depth first search, then it will go to depth instead of exploring layer wise
nodes.
Time complexity: In worst case time complexity in BFS is O(b^d) know as order of b raise to power d. In
this particular case it is (3^20).
b-branch factor
d-depth factor
Let's solve the problem with Heuristic Search that is Informed Search (A* , Best First Search (Greedy
Search))
To solve the problem with Heuristic search or informed search we have to calculate Heuristic values of each
node to calculate cost function. (f=g+h)
Initial state Goal state

Note: See the initial state and goal state carefully all values except (4,5 and 8) are at their
respective places. so, the heuristic value for first node is 3.(Three values are misplaced to reach the goal).
And let's take actual cost (g) according to depth.

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Program:
ids :-
start(State),
length(Moves, N),
dfs([State], Moves, Path), !,
show([start|Moves], Path),
format('~nmoves = ~w~n', [N]).

dfs([State|States], [], Path) :-


goal(State), !,
reverse([State|States], Path).

dfs([State|States], [Move|Moves], Path) :-


move(State, Next, Move),
not(memberchk(Next, [State|States])),
dfs([Next,State|States], Moves, Path).

show([], _).
show([Move|Moves], [State|States]) :-
State = state(A,B,C,D,E,F,G,H,I),
format('~n~w~n~n', [Move]),
format('~w ~w ~w~n',[A,B,C]),
format('~w ~w ~w~n',[D,E,F]),
format('~w ~w ~w~n',[G,H,I]),
show(Moves, States).

% Empty position is marked with '*'

start( state(6,1,3,4,*,5,7,2,0) ).

goal( state(*,0,1,2,3,4,5,6,7) ).

move( state(*,B,C,D,E,F,G,H,J), state(B,*,C,D,E,F,G,H,J), right).


move( state(*,B,C,D,E,F,G,H,J), state(D,B,C,*,E,F,G,H,J), down ).

Machine Learning MRCET CAMPUS


move(B.Tech – CSE (Computational intelligence)
state(A,*,C,D,E,F,G,H,J), state(*,A,C,D,E,F,G,H,J), left ). R-20
move( state(A,*,C,D,E,F,G,H,J), state(A,C,*,D,E,F,G,H,J), right).
move( state(A,*,C,D,E,F,G,H,J), state(A,E,C,D,*,F,G,H,J), down ).
move( state(A,B,*,D,E,F,G,H,J), state(A,*,B,D,E,F,G,H,J), left ).
move( state(A,B,*,D,E,F,G,H,J), state(A,B,F,D,E,*,G,H,J), down ).
move( state(A,B,C,*,E,F,G,H,J), state(*,B,C,A,E,F,G,H,J), up ).
move( state(A,B,C,*,E,F,G,H,J), state(A,B,C,E,*,F,G,H,J), right).
move( state(A,B,C,*,E,F,G,H,J), state(A,B,C,G,E,F,*,H,J), down ).
move( state(A,B,C,D,*,F,G,H,J), state(A,*,C,D,B,F,G,H,J), up ).
move( state(A,B,C,D,*,F,G,H,J), state(A,B,C,D,F,*,G,H,J), right).
move( state(A,B,C,D,*,F,G,H,J), state(A,B,C,D,H,F,G,*,J), down ).
move( state(A,B,C,D,*,F,G,H,J), state(A,B,C,*,D,F,G,H,J), left ).
move( state(A,B,C,D,E,*,G,H,J), state(A,B,*,D,E,C,G,H,J), up ).
move( state(A,B,C,D,E,*,G,H,J), state(A,B,C,D,*,E,G,H,J), left ).
move( state(A,B,C,D,E,*,G,H,J), state(A,B,C,D,E,J,G,H,*), down ).
move( state(A,B,C,D,E,F,*,H,J), state(A,B,C,D,E,F,H,*,J), left ).
move( state(A,B,C,D,E,F,*,H,J), state(A,B,C,*,E,F,D,H,J), up ).
move( state(A,B,C,D,E,F,G,*,J), state(A,B,C,D,E,F,*,G,J), left ).
move( state(A,B,C,D,E,F,G,*,J), state(A,B,C,D,*,F,G,E,J), up ).
move( state(A,B,C,D,E,F,G,*,J), state(A,B,C,D,E,F,G,J,*), right).
move( state(A,B,C,D,E,F,G,H,*), state(A,B,C,D,E,*,G,H,F), up ).
move( state(A,B,C,D,E,F,G,H,*), state(A,B,C,D,E,F,G,*,H), left ).
Output:
Query: ids.
start

613
4*5
720

left

613
*45
720

up

*13
645
720

right

1*3
645
720

down

143
6*5

Machine Learning MRCET CAMPUS


7 2 0 B.Tech – CSE (Computational intelligence) R-20

right

143
65*
720

down

143
650
72*

left

143
650
7*2

*67

up

143
*02
567

right

143
0*2
567

right

143
02*
567

up

14*
023
567

left

1*4
023
567

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20
left

*14
023
567

down

014
*23
567

right

014
2*3
567

right

014
23*
567

up

01*
234
567

left

0*1
234
567

left

*01
234
567

moves = 26

1true

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Week:5
Aim:
Write a PROLOG program to solve N-Queens problem.
Theory/Description:
N-Queens Problem
N - Queens problem is to place n - queens in such a manner on an n x n chessboard that no queens attack
each other by being in the same row, column or diagonal.
It can be seen that for n =1, the problem has a trivial solution, and no solution exists for n =2 and n =3. So
first we will consider the 4 queens problem and then generate it to n - queens problem.
Given a 4 x 4 chessboard and number the rows and column of the chessboard 1 through 4.

Since, we have to place 4 queens such as q1 q2 q3 and q4 on the chessboard, such that no two queens attack
each other. In such a conditional each queen must be placed on a different row, i.e., we put queen "i" on row
"i."

Now, we place queen q1 in the very first acceptable position (1, 1). Next, we put queen q2 so
that both these queens do not attack each other. We find that if we place q2 in column 1 and
2, then the dead end is encountered. Thus the first acceptable position for q2 in column 3, i.e.
(2, 3) but then no position is left for placing queen 'q3' safely. So we backtrack one step and
place the queen 'q2' in (2, 4), the next best possible solution. Then we obtain the position for
placing 'q3' which is (3, 2). But later this position also leads to a dead end, and no place is
found where 'q4' can be placed safely. Then we have to backtrack till 'q1' and place it to (1, 2)
and then all other queens are placed safely by moving q2 to (2, 4), q3 to (3, 1) and q4 to (4, 3).
That is, we get the solution (2, 4, 1, 3). This is one possible solution for the 4-queens

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20
problem. For another possible solution, the whole method is repeated for all partial solutions.
The other solutions for 4 - queens problems is (3, 1, 4, 2) i.e.

The implicit tree for 4 - queen problem for a solution (2, 4, 1, 3) is as follows:

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Fig shows the complete state space for 4 - queens problem. But we can use backtracking
method to generate the necessary node and stop if the next node violates the rule, i.e., if two
queens are attacking.

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

4 - Queens solution space with nodes numbered in DFS


It can be seen that all the solutions to the 4 queens problem can be represented as 4 - tuples
(x1, x2, x3, x4) where xi represents the column on which queen "qi" is placed.
One possible solution for 8 queens problem is shown in fig:

1. Thus, the solution for 8 -queen problem for (4, 6, 8, 2, 7, 1, 3, 5).


2. If two queens are placed at position (i, j) and (k, l).
3. Then they are on same diagonal only if (i - j) = k - l or i + j = k + l.
4. The first equation implies that j - l = i - k.
5. The second equation implies that j - l = k - i.
6. Therefore, two queens lie on the duplicate diagonal if and only if |j-l|=|i-k|

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20
Place (k, i) returns a Boolean value that is true if the kth queen can be placed in column i. It
tests both whether i is distinct from all previous costs x1, x2,....xk-1 and whether there is no
other queen on the same diagonal.
Using place, we give a precise solution to then n- queens problem.

Program:
% render solutions nicely.
:-use_rendering(chess).

%% queens(+N, -Queens) is nondet.


%
% @param Queens is a list of column numbers for placing the queens.
% @author Richard A. O'Keefe (The Craft of Prolog)

queens(N, Queens) :-
length(Queens, N),
board(Queens, Board, 0, N, _, _),
queens(Board, 0, Queens).

board([], [], N, N, _, _).


board([_|Queens], [Col-Vars|Board], Col0, N, [_|VR], VC) :-
Col is Col0+1,
functor(Vars, f, N),
constraints(N, Vars, VR, VC),
board(Queens, Board, Col, N, VR, [_|VC]).

constraints(0, _, _, _) :- !.
constraints(N, Row, [R|Rs], [C|Cs]) :-
arg(N, Row, R-C),
M is N-1,
constraints(M, Row, Rs, Cs).

queens([], _, []).
queens([C|Cs], Row0, [Col|Solution]) :-
Row is Row0+1,
select(Col-Vars, [C|Cs], Board),
arg(Row, Vars, Row-Row),
queens(Board, Row, Solution).

/** <examples>

?- queens(8, Queens).

*/
Output:
Query: queens(8, Queens).

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20
Week:6
Aim:
Implementation of Traveling Salesman Problem.
Theory/Description:
In a state-space representation method, each state of the problem is represented by a node of a graph or tree
structure. In this case, the problem is translated as a search problem to determine the goal under specific
operators and restrains. In this post, I will introduce Traveling Salesman Problem (TSP) as an example.
Representation a problem with the state-space representation needs:
(1). A set of states of the problem
(2). A set of operators to operate between states of the problem
(3). Initial state and final state(goal)
Traveling Salesman Problem (TSP)
Consider the following situation. You are given a list of n cities with the distance between any two cities.
Now, you have to start with your office and to visit all the cities only once each and return to your office.
What is the shortest path can you take? This problem is called the Traveling Salesman Problem (TSP).

Problem Formulation of TSP


To make the problem simple, we consider 3-city-problem.
Let’s call the office ( A )and the 3 cities ( B ) ( C ) ( D ) respectively. We initialize the problem state by {A}
means the salesman departed from his office. As an operator, when he visited city-B, the problem state is
updated to {A, B}, where the order of elements in { } is considered. When the salesman visited all the cities,
{A, B, C, D} in this case, the departed point A is automatically added to the state which means {A, B, C, D,
A}. Therefore, the initial state of this TSP is {A} and the final state(goal) is {A, X1, X2, X3, A} where
traveled distance is minimized.
Taking each state as a node of a tree structure, we can represent this TSP as the following tree search
problem.

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Depth-first search
The depth-first search algorithm starts at the root node and explores as deep as possible along each branch
before taking backtracking. In our TSP, when a state node with all city labels is visited, its total distance is
memorized. This information will later be used to define the shortest path.
Let VISIT be a stack to save visited nodes, PATH be a set to save distances from the root node to the goal.
The depth-first algorithm can be written as

Breadth-first search
The depth-first search algorithm starts at the root node and explores all of the nodes at the present depth
level before moving on to the nodes at the next depth level. In our TSP, when a state node with all city labels
is visited, its total distance is memorized. This information will later be used to define the shortest path.
Let VISIT be a queue to save visited nodes, PATH be a set to save distances from the root node to the goal.
The breadth-first algorithm can be written as

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Heuristic Search
In Brute-force search, all nodes are visited and the information from each node (distance from a node to a
node) is not considered. This leads to a large amount of time and memory consumption. To solve this
problem, a heuristic search is a solution. The information of each state node is used to consider visiting a
node or not. This information is represented by a heuristic function which commonly set up by user’s
experiences. For example, we can define the heuristic function by the distance from the root node to the
present visit node, or the distance from the present visit node to the goal node.
Best-first search
In the Best-first search, we use the information of distance from the root node to decide which node to visit
first. Let g(X) be the distance from the root node to node-X. Therefore, the distance from the root node to
node-Y by visiting node-X is g(Y)=g(X)+d(X, Y), where d(X, Y) is the distance between X and Y.
Let VISIT be a list to save visited nodes. The best-first algorithm can be written as

A-algorithm (A*-algorithm)
In the A algorithm search, we use the information of distance from the present visit node to the goal as a
heuristic function, h(X). Let g(X) be the distance from the root node to node-X. In this case, we consider the
priority of node visit order by f(X)=g(X)+h(X).
In real-world problems, it is impossible to obtain the exact value of h(X). In that case, an estimation value of
h(X), h’(X), is used. However, setting h’(X) takes risks in falling into a local optimal answer. To prevent this
problem, choosing h’(X) which h’(X)≤h(X) for all X is recommended. In this case, it is known as A*-
algorithm and it can be shown that the obtained answer is the global optimal answer.
In our experiment described in the following part, we are setting h’(X) as the sum of the minimum distance
of all possible routes from each city which is not presented in the current visit node label, and the present

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20
city. For example, if the present node is {A, D}, then city-B and C is not present in the label. Therefore,
h’(D)=min(all possible route distance from C)+min(all possible route distance from B)+min(all possible
route distance from D).
Let VISIT be a list to save visited nodes. The A-algorithm can be written as

Comparison between Search Algorithms


We simulate the TSP with each introduced algorithm and focus on effectiveness as a search problem. Here,
the effectiveness of a search problem is evaluated by the number of nodes visited to reach the answer and the
effective branch number comparing to the tree’s depth level. The effective branch number b* of a problem
where N nodes have been visited and the answer is a d depth-level of the tree, is calculated by
N+1 = 1 + b* + (b*)²+(b*)³+…+(b*)^d . An algorithm with a smaller b* is a more effective algorithm. In
this experiment, we use Newton’s method to solve this equation to obtain the approximate value of b*.
Discussion on the number of visited nodes

Discussion on the effective branch number

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20
Program:
/*Jacob Besse
Prolog*/

/*This is the data set.*/

edge(a, b, 3).
edge(a, c, 4).
edge(a, d, 2).
edge(a, e, 7).
edge(b, c, 4).
edge(b, d, 6).
edge(b, e, 3).
edge(c, d, 5).
edge(c, e, 8).
edge(d, e, 6).
edge(b, a, 3).
edge(c, a, 4).
edge(d, a, 2).
edge(e, a, 7).
edge(c, b, 4).
edge(d, b, 6).
edge(e, b, 3).
edge(d, c, 5).
edge(e, c, 8).
edge(e, d, 6).
edge(a, h, 2).
edge(h, d, 1).

/* Finds the length of a list, while there is something in the list it increments N
when there is nothing left it returns.*/

len([], 0).
len([H|T], N):-len(T, X), N is X+1 .

/*Best path, is called by shortest_path. It sends it the paths found in a


path, distance format*/

best_path(Visited, Total):- path(a, a, Visited, Total).

/*Path is expanded to take in distance so far and the nodes visited */


path(Start, Fin, Visited, Total) :- path(Start, Fin, [Start], Visited, 0, Total).

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20
/*This adds the stopping location to the visited list, adds the distance and then calls recursive
to the next stopping location along the path */

path(Start, Fin, CurrentLoc, Visited, Costn, Total) :-


edge(Start, StopLoc, Distance), NewCostn is Costn + Distance, \+ member(StopLoc,
CurrentLoc),
path(StopLoc, Fin, [StopLoc|CurrentLoc], Visited, NewCostn, Total).

/*When we find a path back to the starting point, make that the total distance and make
sure the graph has touch every node*/

path(Start, Fin, CurrentLoc, Visited, Costn, Total) :-


edge(Start, Fin, Distance), reverse([Fin|CurrentLoc], Visited), len(Visited, Q),
(Q\=7 -> Total is 100000; Total is Costn + Distance).

/*This is called to find the shortest path, takes all the paths, collects them in holder.
Then calls pick on that holder which picks the shortest path and returns it*/

shortest_path(Path):-setof(Cost-Path, best_path(Path,Cost), Holder),pick(Holder,Path).

/* Is called, compares 2 distances. If cost is smaller than bcost, no need to go on. Cut it.*/

best(Cost-Holder,Bcost-_,Cost-Holder):- Cost<Bcost,!.
best(_,X,X).

/*Takes the top path and distance off of the holder and recursively calls it.*/

pick([Cost-Holder|R],X):- pick(R,Bcost-Bholder),best(Cost-Holder,Bcost-Bholder,X),!.
pick([X],X).
Output:
Query: shortest_path(Path).
Path = 20-[a, h, d, e, b, c, a]

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Machine Learning Programs Using Python

Week-1:
Implementation of Python Basic Libraries such as Math, Numpy and Scipy

Theory/Description:

• Python Libraries
There are a lot of reasons why Python is popular among developers and one of them is that it has an
amazingly large collection of libraries that users can work with. In this Python Library, we will discuss
Python Standard library and different libraries offered by Python Programming Language: scipy, numpy,
etc.
We know that a module is a file with some Python code, and a package is a directory for sub packages and
modules. A Python library is a reusable chunk of code that you may want to include in your programs/
projects. Here, a library loosely describes a collection of core modules. Essentially, then, a library is a
collection of modules. A package is a library that can be installed using a package manager like npm.

• Python Standard Library


The Python Standard Library is a collection of script modules accessible to a Python program to simplify the
programming process and removing the need to rewrite commonly used commands. They can be used by
'calling/importing' them at the beginning of a script. A list of the Standard Library modules that are most
important
time
sys
csv
math
random
pip
os
statistics
tkinter
socket

To display a list of all available modules, use the following command in the Python console:
>>> help('modules')
• List of important Python Libraries
o Python Libraries for Data Collection
▪ Beautiful Soup
▪ Scrapy
▪ Selenium
o Python Libraries for Data Cleaning and Manipulation
▪ Pandas
▪ PyOD
▪ NumPy
▪ Scipy
▪ Spacy
o Python Libraries for Data Visualization
▪ Matplotlib

Machine Learning MRCET CAMPUS


▪ B.Tech – CSE (Computational intelligence)
Seaborn R-20

▪ Bokeh
o Python Libraries for Modeling
▪ Scikit-learn
▪ TensorFlow
▪ Keras
▪ PyTorch

a) Implementation of Python Basic Libraries such as Math, Numpy and Scipy


• Python Math Library

The math module is a standard module in Python and is always available. To use mathematical functions
under this module, you have to import the module using import math. It gives access to the underlying C
library functions. This module does not support complex datatypes. The cmath module is the complex
counterpart.

List of Functions in Python Math Module


Function Description
ceil(x) Returns the smallest integer greater than or equal to x.
copysign(x,y) Returns x with the sign of y

fabs(x) Returns the absolute value of x


factorial(x) Returns the factorial of x
floor(x) Returns the largest integer less than or equal to x
fmod(x, y) Returns the remainder when x is divided by y
frexp(x) Returns the mantissa and exponent of x as the pair (m,
e)
fsum(iterable) Returns an accurate floating point sum of values in the
iterable
isfinite(x) Returns True if x is neither an infinity nor a NaN (Not a
Number)
isinf(x) Returns True if x is a positive or negative infinity
isnan(x) Returns True if x is a NaN
ldexp(x, i) Returns x * (2**i)
modf(x) Returns the fractional and integer parts of x
trunc(x) Returns the truncated integer value of x
exp(x) Returns e**x
expm1(x) Returns e**x - 1

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Program-1

Program-2

Program-3

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Program-4

Program-5
o We also have a boolean function isNaN() which returns true if the given argument is a NaN and
returns false otherwise. We can also take a value and convert it to float to check whether it is NaN.
o A missing value is denoted as NaN (Stands for Not a Number eg. 0/0).
o Inf: 1/0 is one example of Inf. We can define the negative infinity as -inf and the positive infinity as inf
in Python.

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Program-6

• Python Numpy Library

NumPy is an open source library available in Python that aids in mathematical, scientific, engineering, and
data science programming. NumPy is an incredible library to perform mathematical and statistical operations.
It works perfectly well for multi-dimensional arrays and matrices multiplication

For any scientific project, NumPy is the tool to know. It has been built to work with the N- dimensional
array, linear algebra, random number, Fourier transform, etc. It can be integrated to C/C++ and Fortran.

NumPy is a programming language that deals with multi-dimensional arrays and matrices. On top of the
arrays and matrices, NumPy supports a large number of mathematical operations.

NumPy is memory efficient, meaning it can handle the vast amount of data more accessible than any other
library. Besides, NumPy is very convenient to work with, especially for matrix multiplication and reshaping.
On top of that, NumPy is fast. In fact, TensorFlow and Scikitlearn use NumPy array to compute the matrix
multiplication in the back end.

• Arrays in NumPy: NumPy’s main object is the homogeneous multidimensional array.

It is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers.
In NumPy dimensions are called axes. The number of axes is rank.
NumPy’s array class is called ndarray. It is also known by the alias array.

We use python numpy array instead of a list because of the below three reasons:
1. Less Memory
2. Fast
3. Convenient

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Numpy Functions

Numpy arrays carry attributes around with them. The most important ones are:
ndim: The number of axes or rank of the array. ndim returns an integer that tells us how many dimensions the
array have.
shape: A tuple containing the length in each dimensionsize: The total number of elements

Program-1

Can be used just like Python lists


x[1] will access the second row
x[-1] will access the last row
x[2]?
x[3]?
x[0]?

Program-2

Arithmetic operations apply element wise

• Built-in Methods

Many standard numerical functions are available as methods out of the box:
Program-3

Machine Learning MRCET CAMPUS


B.Tech – CSE
Exercise: (Computational
Apply intelligence)
x.sort() and display the sorted array as output. R-20

• Python Scipy Library

SciPy is an Open Source Python-based library, which is used in mathematics, scientific computing,
Engineering, and technical computing. SciPy also pronounced as "Sigh Pi."

SciPy contains varieties of sub packages which help to solve the most common issuerelated to Scientific
Computation.
SciPy is the most used Scientific library only second to GNU Scientific Library forC/C++ or Matlab's.
Easy to use and understand as well as fast computational power.
It can operate on an array of NumPy library.

Numpy VS SciPy

Numpy:
1. Numpy is written in C and used for mathematical or numeric calculation.
2. It is faster than other Python Libraries
3. Numpy is the most useful library for Data Science to perform basic calculations.
4. Numpy contains nothing but array data type which performs the most basic operation like sorting, shaping,
indexing, etc.

SciPy:

1. SciPy is built in top of the NumPy


2. SciPy is a fully-featured version of Linear Algebra while Numpy contains only a few features.
3. Most new Data Science features are available in Scipy rather than Numpy.

Linear Algebra with SciPy


1. Linear Algebra of SciPy is an implementation of BLAS and ATLAS LAPACK libraries.
2. Performance of Linear Algebra is very fast compared to BLAS and LAPACK.
3. Linear algebra routine accepts two-dimensional array object and output is also a two- dimensional array.
Now let's do some test with scipy.linalg,

Calculating determinant of a two-dimensional matrix,

Program-1

Eigenvalues and Eigenvector – scipy.linalg.eig()


The most common problem in linear algebra is eigenvalues and eigenvector which can be easily solved
using eig() function.
Now lets we find the Eigenvalue of (X) and correspond eigenvector of a two-dimensional square
matrix.

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Program-2

Exercise programs:

1. Consider a list datatype (1D) then reshape it into 2D, 3D matrix using numpy
2. Generate random matrices using numpy
3. Find the determinant of a matrix using scipy
4. Find eigenvalue and eigenvector of a matrix using scipy

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20
Week-2:

Implementation of Python Libraries for ML application such as Pandas and Matplotlib.

• Pandas Library

The primary two components of pandas are the Series and DataFrame.
A Series is essentially a column, and a DataFrame is a multi-dimensional table made up of a collection of
Series.
DataFrames and Series are quite similar in that many operations that you can do with oneyou can do with the
other, such as filling in null values and calculating the mean.

Reading data from CSVs

With CSV files all you need is a single line to load in the data:
df = pd.read_csv('purchases.csv') df

Let's load in the IMDB movies dataset to begin:


movies_df = pd.read_csv("IMDB-Movie-Data.csv", index_col="Title")
We're loading this dataset from a CSV and designating the movie titles to be our index.

Viewing your data


The first thing to do when opening a new dataset is print out a few rows to keep as a visual reference. We
accomplish this with .head():
movies_df.head()

Another fast and useful attribute is .shape, which outputs just a tuple of (rows, columns):
movies_df.shape
Note that .shape has no parentheses and is a simple tuple of format (rows, columns). So we have 1000 rows
and 11 columns in our movies DataFrame.
You'll be going to .shape a lot when cleaning and transforming data. For example, you might filter some rows
based on some criteria and then want to know quickly how many rows were removed.

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence)
Program-1 R-20

We haven't defined an index in our example, but we see two columns in our output: The right column
contains our data, whereas the left column contains the index. Pandas created a default index starting with 0
going to 5, which is the length of the data minus 1.

dtype('int64'): The type int64 tells us that Python is storing each value within this column as a 64 bit integer

Program-2
We can directly access the index and the values of our Series S:

Program-3
If we compare this to creating an array in numpy, we will find lots of similarities:

So far our Series have not been very different to ndarrays of Numpy. This changes, as soon as we start
defining Series objects with individual indices:
Program-4

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence)
Program-5 R-20
A big advantage to NumPy arrays is obvious from the previous example: We can use arbitrary indices.
If we add two series with the same indices, we get a new series with the same index and the corresponding
values will be added:
fruits = ['apples', 'oranges', 'cherries', 'pears']

S = pd.Series([20, 33, 52, 10], index=fruits)


S2 = pd.Series([17, 13, 31, 32], index=fruits)
print(S + S2)
print("sum of S: ", sum(S))

O UTPUT :
apples 37
oranges 46
cherries 83
pears 42
dtype: int64
sum of S: 115

Program-6
The indices do not have to be the same for the Series addition. The index will be the "union" of both indices.
If an index doesn't occur in both Series, the value for this Series will be NaN:
fruits = ['peaches', 'oranges', 'cherries', 'pears']
fruits2 = ['raspberries', 'oranges', 'cherries', 'pears']

S = pd.Series([20, 33, 52, 10], index=fruits)


S2 = pd.Series([17, 13, 31, 32], index=fruits2)
print(S + S2)

O UTPUT :
cherries 83.0
oranges 46.0
peaches NaN
pears 42.0
raspberries NaN
dtype: float64

Program-7
In principle, the indices can be completely different, as in the following example. We have two indices. One
is the Turkish translation of the English fruit names:
fruits = ['apples', 'oranges', 'cherries', 'pears']

fruits_tr = ['elma', 'portakal', 'kiraz', 'armut']

S = pd.Series([20, 33, 52, 10], index=fruits)


S2 = pd.Series([17, 13, 31, 32], index=fruits_tr)
print(S + S2)

O UTPUT :
apples NaN

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20
armut NaN
cherries NaN
elma NaN
kiraz NaN
oranges NaN
pears NaN
portakal NaN
dtype: float64

Program-8
Indexing
It's possible to access single values of a Series.

print(S['apples'])

O UTPUT :
20

• Matplotlib Library

Pyplot is a module of Matplotlib which provides simple functions to add plot elementslike lines, images,
text, etc. to the current axes in the current figure.

Make a simple plot


import matplotlib.pyplot as plt
import numpy as np

List of all the methods as they appeared.

plot(x-axis values, y-axis values) — plots a simple line graph with x-axis valuesagainst y-axis values
show() — displays the graph
title(―string‖) — set the title of the plot as specified by the string
xlabel(―string‖) — set the label for x-axis as specified by the string
ylabel(―string‖) — set the label for y-axis as specified by the string
figure() — used to control a figure level attributes
subplot(nrows, ncols, index) — Add a subplot to the current figure
suptitle(―string‖) — It adds a common title to the figure specified by the string
subplots(nrows, ncols, figsize) — a convenient way to create subplots, in a single call.It returns a tuple
of a figure and number of axes.
set_title(―string‖) — an axes level method used to set the title of subplots in a figure
bar(categorical variables, values, color) — used to create vertical bar graphs
barh(categorical variables, values, color) — used to create horizontal bar graphs
legend(loc) — used to make legend of the graph
xticks(index, categorical variables) — Get or set the current tick locations and labelsof the x-axis
pie(value, categorical variables) — used to create a pie chart

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20
hist(values, number of bins) — used to create a histogram
xlim(start value, end value) — used to set the limit of values of the x-axis
ylim(start value, end value) — used to set the limit of values of the y-axis
scatter(x-axis values, y-axis values) — plots a scatter plot with x-axis values againsty-axis values
axes() — adds an axes to the current figure
set_xlabel(―string‖) — axes level method used to set the x-label of the plot specified as a string
set_ylabel(―string‖) — axes level method used to set the y-label of the plot specified as a string
scatter3D(x-axis values, y-axis values) — plots a three-dimensional scatter plot withx-axis values
against y-axis values
plot3D(x-axis values, y-axis values) — plots a three-dimensional line graph with x-axis values against
y-axis values

Here we import Matplotlib‘s Pyplot module and Numpy library as most of the data thatwe will be working
with arrays only.
Program-1

We pass two arrays as our input arguments to Pyplot‘s plot() method and use show() method to invoke the
required plot. Here note that the first array appears on the x-axis andsecond array appears on the y-axis of
the plot. Now that our first plot is ready, let us add the title, and name x-axis and y-axis using methods title(),
xlabel() and ylabel() respectively.

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20
Program-2

Program-3
We can also specify the size of the figure using method figure()and passing the values as a tuple of the length
of rows and columns to the argument figsize

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20
Program-4
With every X and Y argument, you can also pass an optional third argument in the formof a string which
indicates the colour and line type of the plot. The default format is b- which means a solid blue line. In the
figure below we use go which means green circles.Likewise, we can make many such combinations to
format our plot.

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20

EXERCISE:

1. Write a python program to declare two series data and also add the index names. Use division operator to divide one
series by another. In the output one of the series data must be NaN and another Inf.
2. Write a python program to consider some values as (x,y) co-ordinate values and plot the graph using a line graph. The
color of the line graph should be red.

Machine Learning MRCET CAMPUS


B.Tech – CSE (Computational intelligence) R-20
B.Tech – CSE (Computational intelligence) R-20

Week-3
1. Creation and loading different datasets in Python

Program-1
Method-I
B.Tech – CSE (Computational intelligence) R-20

Program-2
Method-II:

Program-3 Uploading csv file:


Method-III:
B.Tech – CSE (Computational intelligence) R-20

2. Write a python program to compute Mean, Median, Mode, Variance, Standard Deviation
using Datasets

• Python Statistics library


This module provides functions for calculating mathematical statistics of numeric (Real-valued)
data. The statistics module comes with very useful functions like: Mean, median, mode,
standard deviation, and variance.
The four functions we'll use are common in statistics:
1. mean - average value
2. median - middle value
3. mode - most often value
4. standard deviation - spread of values

• Averages and measures of central location


These functions calculate an average or typical value from a population or sample. mean()
Arithmetic mean (―average‖) of data.
harmonic_mean() Harmonic mean of data.
median() Median (middle value) of data. median_low() Low
median of data.
median_high() High median of data.
median_grouped() Median, or 50th percentile, of grouped data. mode() Mode
(most common value) of discrete data.

• Measures of spread
These functions calculate a measure of how much the population or sample tends to deviate
from the typical or average values.
pstdev() Population standard deviation of data.
pvariance() Population variance of data.
stdev() Sample standard deviation of data.
variance() Sample variance of data.
B.Tech – CSE (Computational intelligence) R-20

Program-1

Program-2

Program-3

Program-4
B.Tech – CSE (Computational intelligence) R-20

Program-5

Demonstrate various data pre-processing techniques for a given dataset. Write a python
program to compute
a) Reshaping the data,
b) Filtering the data,
c) Merging the data
d) Handling the missing values in datasets
e) Feature Normalization: Min-max normalization
Program-1
Reshaping the data:
Method-I
B.Tech – CSE (Computational intelligence) R-20

Program-2
Method:II
Assigning the data:
B.Tech – CSE (Computational intelligence) R-20

Program-3
ARTIFICAL INTELLIGENCE AND MACHINE LEARNING LAB MANUAL2022-2023

Filtering the data


Suppose there is a requirement for the details regarding name, gender, marks of the top-
scoring students. Here we need to remove some unwanted data.

Program-1

Program-2

Program-3

Department of Computational Intelligence 81


ARTIFICAL INTELLIGENCE AND MACHINE LEARNING LAB MANUAL2022-2023

Merge data:
Merge operation is used to merge raw data and into the desired format.
Syntax:
pd.merge( data_frame1,data_frame2, on="field ")

Program-4
First type of data:

Department of Computational Intelligence 82


ARTIFICAL INTELLIGENCE AND MACHINE LEARNING LAB MANUAL2022-2023

Program-5
Second type of data:

Program-6

Department of Computational Intelligence 83


ARTIFICAL INTELLIGENCE AND MACHINE LEARNING LAB MANUAL2022-2023

Handling the missing values:


Program-1

Program-2
In order to check null values in Pandas DataFrame, we use isnull() function. This function
return dataframe of Boolean values which are True for NaN values.

Department of Computational Intelligence 84


ARTIFICAL INTELLIGENCE AND MACHINE LEARNING LAB MANUAL2022-2023

Program-3
In order to check null values in Pandas Dataframe, we use notnull() function this function
return dataframe of Boolean values which are False for NaN values.

Program-4

Department of Computational Intelligence 85


ARTIFICAL INTELLIGENCE AND MACHINE LEARNING LAB MANUAL2022-2023

Program-5

Program-6

Department of Computational Intelligence 86


ARTIFICAL INTELLIGENCE AND MACHINE LEARNING LAB MANUAL2022-2023

Program-7
Method-I
Drop Columns with Missing Values

Program-8
Method-II
fillna() manages and let the user replace NaN values with some value of their own

Department of Computational Intelligence 87


ARTIFICAL INTELLIGENCE AND MACHINE LEARNING LAB MANUAL2022-2023

Program-9

Department of Computational Intelligence 88


ARTIFICAL INTELLIGENCE AND MACHINE LEARNING LAB MANUAL2022-2023

Program-10
Filling missing values with mean

Program-11
Filling missing values in csv files:
df=pd.read_csv(r'E:\mldatasets\Machine_Learning_Data_Preprocessing_Python-
master\Sample_real_estate_data.csv', na_values='NAN')

Department of Computational Intelligence 89


ARTIFICAL INTELLIGENCE AND MACHINE LEARNING LAB MANUAL2022-2023

Program-12

Program-13
Code:
missing_value = ["n/a","na","--"]
data1=pd.read_csv(r'E:\mldatasets\Machine_Learning_Data_Preprocessing_Python-
master\Sample_real_estate_data.csv', na_values = missing_value)
df = data1

Department of Computational Intelligence 90


ARTIFICAL INTELLIGENCE AND MACHINE LEARNING LAB MANUAL2022-2023

Exercise programs:
1. Load two standard ML datasets by using Method II and III shown in the above examples.
2. Write a python program to compute Mean, Median, Mode, Variance, Standard Deviation using
the first 5 or more rows from Iris dataset.
3. Load a real dataset (For example, Iris). Then apply Min-max normalization on the features of
the dataset.

Department of Computational Intelligence 91


ARTIFICAL INTELLIGENCE AND MACHINE LEARNING LAB MANUAL2022-2023

Department of Computational Intelligence 92


ARTIFICAL INTELLIGENCE AND MACHINE LEARNING LAB MANUAL2022-2023

Department of Computational Intelligence 93


ARTIFICAL INTELLIGENCE AND MACHINE LEARNING LAB MANUAL2022-2023

Week-4

Write a program to demonstrate the working of the decision tree based ID3 algorithm by
considering a dataset.

Decision Tree: A decision tree mainly contains of a root node, interior nodes, and leaf
nodes which are then connected by branches. The main idea of decision trees (ID3) is to find
those descriptive features which contain the most "information" regarding the target feature and
then split the dataset along the values of these features such that the target feature values for the
resulting sub-datasets are as pure as possible. The descriptive feature which leaves the target
feature most purely is said to be the most informative one. This process of finding the "most
informative" feature is done until we accomplish a stopping criteria where we then finally end
up in so called leaf nodes. Information gain is a measure of how good a descriptive feature is
suited to split a dataset on. o be able to calculate the information gain, we have to first introduce
the term entropy of a dataset. The entropy of a dataset is used to measure the impurity of a
dataset and we will use this kind of informativeness measure in our calculations.
#ID3 from sklearn

from sklearn.datasets import load_iris


from sklearn import tree
iris = load_iris()
X, y = iris.data, iris.target
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X, y)
tree.plot_tree(clf)

Department of Computational Intelligence 94


ARTIFICAL INTELLIGENCE AND MACHINE LEARNING LAB MANUAL2022-2023

Consider a dataset, use Random Forest to predict the output class.

Random Forest: The Random forest classifier creates a set of decision trees from a randomly
selected subset of the training set. It collects the votes from different decision trees to decide the
final prediction.

from sklearn import datasets


iris = datasets.load_iris()
print(iris.target_names)
print(iris.feature_names)
# dividing the datasets into two parts i.e. training datasets and test da
tasets
X, y = datasets.load_iris( return_X_y = True)

# Splitting arrays or matrices into random train and test subsets


from sklearn.model_selection import train_test_split
# i.e. 70 % training dataset and 30 % test datasets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3
0)
# importing random forest classifier from assemble module
from sklearn.ensemble import RandomForestClassifier
import pandas as pd
# creating dataframe of IRIS dataset
data = pd.DataFrame({'sepallength': iris.data[:, 0], 'sepalwidth': iris.d
ata[:, 1],
'petallength': iris.data[:, 2], 'petalwidth': iris.d
ata[:, 3],
'species': iris.target})
# creating a RF classifier
clf = RandomForestClassifier(n_estimators = 20)

# Training the model on the training dataset


# fit function is used to train the model using the training sets as para
meters
clf.fit(X_train, y_train)

# performing predictions on the test dataset


y_pred = clf.predict(X_test)

# metrics are used to find accuracy or error


from sklearn import metrics
print()

Department of Computational Intelligence 95


ARTIFICAL INTELLIGENCE AND MACHINE LEARNING LAB MANUAL2022-2023

# using metrics module for accuracy calculation


print("ACCURACY OF THE MODEL: ", metrics.accuracy_score(y_test, y_pred))

Exercise:
a) Apply ID3 on a different dataset.
b) Apply Random forest by varying the number of trees to 50, 100, 200, 500 and analyze the
variation in the accuracies obtained.

Department of Computational Intelligence 96


R-20
R-20
Week-5

Write a Python program to implement Simple Linear Regression and plot the graph.

Linear Regression: Linear regression is defined as an algorithm that provides a linear relationship between
an independent variable and a dependent variable to predict the outcome of future events. It is a statistical
method used in data science and machine learning for predictive analysis. Linear regression is a supervised
learning algorithm that simulates a mathematical relationship between variables and makes predictions for
continuous or numeric variables such as sales, salary, age, product price, etc.

Program:
R-20
Write a Python program to implement Logistic Regression for iris using sklearn

Exercise:
a) Implement Simple Linear Regression on a different dataset and plot the graph.
b) Implement Logistic Regression on a different dataset and plot the confusion matrix.
R-20
R-20
R-20
Week-6

Build KNN Classification model for a given dataset.

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import datasets
iris = datasets.load_iris()
X, y = datasets.load_iris( return_X_y = True)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.40)
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
from sklearn.neighbors import KNeighborsClassifier
classifier = KNeighborsClassifier(n_neighbors=1)
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
result = confusion_matrix(y_test, y_pred)
print("Confusion Matrix:")
print(result)
result1 = classification_report(y_test, y_pred)
print("Classification Report:",)
print (result1)
result2 = accuracy_score(y_test,y_pred)
print("Accuracy:",result2)
R-20

Implement Support Vector Machine for a dataset.


R-20

Write a python program to implement K-Means clustering Algorithm.

Program:
R-20
Exercise:
a) Write programs to implement the KNN for k=3,5,7,11 and compare the results.
b) Write programs to implement the Linear and Polynomial and RBF kernels for SVM on IRIS and compare
the results.
c) Vary the number of clusters k values as follows on Iris dataset and compare the results. Remove the y-labels
from the dataset as pre-processing.
i. 1
ii. 3
iii. 5
iv. 7
v. 11
R-20
R-20

You might also like