0% found this document useful (0 votes)
22 views29 pages

21UCS608 AI&ML Lab Manual

Aiml notes

Uploaded by

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

21UCS608 AI&ML Lab Manual

Aiml notes

Uploaded by

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

SETHU INSTITUTE OF TECHNOLOGY

(An Autonomous Institution | Accredited with ‘A++’ Grade by NAAC)


PULLOOR, KARIAPATTI - 626 115

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

LAB MANUAL

SEMESTER: VI

21UCS608 – ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING


LABORATORY
INSTITUTE VISION
To promote excellence in technical education and scientific research for the benefit of the
society.
INSTITUTE MISSION
 To provide quality technical education to fulfill the aspiration of the student and to meet
the needs of the Industry.
 To provide holistic learning ambience.
 To impart skills leading to employability and entrepreneurship.
 To establish effective linkage with industries.
 To promote Research and Development activities.
 To offer services for the development of society through education and technology.

CORE VALUES
 Quality  Commitment  Innovation  Team  Courtesy
work
QUALITY POLICY
 To provide Quality technical education to the students
 To produce competent professionals and contributing citizens
 To contribute for the upliftment of the society

DEPARTMENT VISION
To achieve excellence in technical education and scientific research in the field of computer
science and engineering to contribute to the society.

DEPARTMENT MISSION
 Transforming students into technocrats in computer technology confirming the industry
expectation.
 Imparting holistic learner centric environment.
 Cultivating interpersonal traits, problem solving skills, critical and rationale thinking
capabilities for the development of students leading to innovators, leaders and
entrepreneurs.
 Establishing collaboration with the industries for mutual benefits.
 Promoting Research activities among the students and the faculty to solve problems
related to industry and society.
 Offering computer applications life skill to society for better living.

CORE VALUES
 Quality  Dedication  Novelty Team work  Courtesy

PROGRAM EDUCATIONAL OBJECTIVES


Graduates will Practice as Competent Computer Engineers by exhibiting the state of the art
PEO 1 technical skills to cater to the needs of the industries.
Graduates will lead the team and function in a team of multi-cultural professionals with
PEO 2 effective interpersonal skills.
Graduates will hone their professional expertise engaging in research and sustained learning
PEO 3 activities.
PROGRAM SPECIFIC OUTCOMES
PSO – 1 Engineering graduates will demonstrate individual expertise in various
Programming programming languages to develop applications for static, internet, and mobile
Solutions domains.
PSO – 2 Engineering graduates will demonstrate the knowledge of analyzing, planning,
Database and constructing databases, ability to extract information using queries, and skills
Management to develop programming interfaces to synthesis databases.

PROGRAM OUTCOMES
Apply the knowledge of mathematics, basic sciences, engineering fundamentals, and
1.
Computer Science and Engineering to the solution of complex engineering problems.
(Engineering Knowledge)
Identify, formulate, review research literature and analyze complex engineering problems
2.
requiring computing solutions to reach substantiated conclusions using first principles of
mathematics, basic sciences, and Computer Science and Engineering. (Problem analysis)
Design solutions for computer applied complex engineering problems and design system
3. components or processes that meet the specified needs with appropriate consideration for
the public health and safety, and the cultural, societal, and environmental considerations.
(Design/development of solutions)
Use research-based knowledge and research methods including design of experiments,
4.
analysis and interpretation of data, and synthesis of the information to provide valid
conclusions. (Conduct investigations of complex problems)
Create, Select and apply appropriate techniques, resources, and modern IT tools including
5.
prediction and modeling to computing related complex engineering activities with an
understanding of the limitations. (Modern tool usage)
Apply reasoning informed by the contextual knowledge to assess societal, health, safety,
6.
legal and cultural issues and the consequent responsibilities relevant to the professional
computer science and engineering practice. (The Engineer and society)
Understand the impact of the professional computer science and engineering solutions in
7.
societal and environmental contexts, and demonstrate the knowledge of, and need for
sustainable development. (Environment and sustainability)
8. Apply ethical principles and commit to professional ethics and responsibilities and norms
of the computer science and engineering practice. (Ethics)
9. Function effectively as an individual, and as a member or leader in diverse teams, and in
multidisciplinary settings. (Individual and team work)
Communicate effectively on complex computer science and engineering activities with the
10. 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. (Communication)
Demonstrate knowledge and understanding of the engineering and management principles
11.
and apply these to one’s own work, as a member and leader in a team, to manage cost
effective projects in multidisciplinary environments.(Project management and finance)
12. 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. (Life-long learning)
21UCS608 ARTIFICIAL INTELLIGENCE AND L T P C
MACHINE LEARNING LABORATORY

0 0 2 1
PRE-REQUISITE :
COURSE OBJECTIVES:
 Implementing the basic concepts in Artificial Intelligence.
 Implementing Machine Learning Algorithms.

LIST OF EXPERIMENTS
1. Implement Breadth First Search (for 8 puzzle problem or Water jug problem or any AI search
problem).
2. Implement Depth First Search (for 8-queen problem or 8 puzzle problem or Water jug problem
or any AI search problem).
3. Solve travelling salesperson problem using Best First Search.
4. Build a Knowledge based system for forecasting the weather.
5. Write a program to construct a Bayesian network considering medical data. Use this model to
demonstrate the diagnosis of heart patients using standard Heart Disease Data Set.
6. Demonstrate the working of decision tree based on ID3 algorithm. Use an appropriate data
set for building the decision tree and apply this knowledge to classify the new sample.
7. Construct model to predict the residential home prize as a function of the homes living area.
8. Develop a model to determine the likelihood of a patient’s successful response to a specific
medical treatment.
9. Develop an algorithm to predict whether a particular customer buy a computer or not based on
the following attribute age, income, student and credit rating.
10. Develop a model to predict stock market using machine learning algorithm.

TOTAL : 30 Periods
COURSE OUTCOMES:
After the successful completion of this course, the student will be able to
 Implement the concepts of Artificial Intelligence and Machine Learning to solve the real world
problems. (Apply)
 Analyze various Artificial Intelligence and Machine Learning techniques for the effective
solution for a given problem. (Analyze)
 Evaluate the performance of a machine learning model using various parameters. (Evaluate)
 Develop machine learning models to find the optimal solution for the given real world
scenario. (Create)
 Communicate effectively to justify the solutions to the given problems based on legal and
ethical principles. (Affective domain)
 Work effectively as an individual or in teams to develop machine learning models for the
given problem. (Affective domain)
LESSON PLANNING SHEET

Methodology
(D-Demo,
L-Lab visit)
S.No Lesson /Topic Covered Session
Lab Support
(LS)
D L
Write a python program to construct the water jug problem using
1. 1  
Breadth First Search
Write a python program to construct N-Puzzle problem using Depth
2. 1  
First Search
3. Solve travelling salesperson problem using Best First Search 1  
4. Build a Knowledge based system for forecasting the weather 1  
Write a program to construct a Bayesian network considering
5. medical data. Use this model to demonstrate the diagnosis of heart 1  
patients using standard Heart Disease Data Set
Develop an algorithm to predict whether a particular customer buy a
6. computer or not based on the following attribute age, income, 1  
student and credit rating.
Demonstrate the working of decision tree based on ID3 algorithm.
7. Use an appropriate data set for building the decision tree and apply 1  
this knowledge to classify the new sample
Construct model to predict the residential home prize as a function
8.  
of the homes Living area
1
Develop a model to determine the likelihood of a patient’s
9.  
successful response to a specific medical treatment

10. Develop a model to predict stock market using machine learning 


1 
algorithm.
11. Develop a model to predict the values using clustering method
1  

12. Model Lab exam / Mini Project


1  
EXPERIMENTS – CO MAPPING

Course Outcome Experiment Assessment Programme


Competency Performance Indicators
Statements s Tools Outcomes
 Apply the concepts of
 Demonstrate
probability, statistics and
Competence in
queuing theory in
mathematical
Implement the concepts of modeling of computer-
modelling.
Artificial Intelligence and Performance in based system, data and
 Demonstrate
Machine Learning to solve 1 to 11 Lab classes, PO 1 network protocols.
competence in
the real world problems. Model Lab  Apply theory and
(Apply) specialized
principles of computer
engineering
science and engineering to
knowledge to the
solve an engineering
program
problem
Analyze various Artificial
 Demonstrate an ability
Intelligence and Machine Performance in  Analyze and interpret the
to execute a solution
Learning techniques for the 1 to 11 Lab classes, PO 2 results using
process and analyze
effective solution for a Model Lab contemporary tools.
results.
given problem. (Analyze)
 Demonstrate an ability Able to choose appropriate
to conduct procedure/algorithm,
investigations of dataset and test cases.
technical issues Design and develop
Evaluate the performance
consistent with their appropriate procedures /
of a machine learning
1 to 11 Mini project PO 4 level of knowledge methodologies based on
model using various
and understanding. the study Objectives.
parameters. (Evaluate)
 Demonstrate an ability
to design experiments
to solve open-ended
problems.
Develop machine learning 1 to 11 Mini project PO3, PO 5  Demonstrate an ability  Able to perform
to select optimal systematic evaluation of
design scheme for the degree to which
further development. several design concepts
 Demonstrate an ability meet the criteria.
to identify/create  Create/adapt/modify/
modern engineering extend tools and
tools, techniques and techniques to solve
models to find the optimal
resources. engineering problems.
solution for the given real and PO 6
 Demonstrate an ability  Identify and describe
world scenario. (Create)
to describe various engineering roles;
engineering roles in a particularly as pertains to
broader context, e.g. protection of the public
pertaining to the and public interest at the
environment, health, global, regional and local
safety, legal and level.
public welfare.
Communicate effectively to  Demonstrate an ability  Examine and apply moral
Performance in
justify the solutions to the to apply the Code of & ethical principles to
Lab classes, PO 8 and PO
given problems based on 1 to 11 Ethics. known case studies.
Model Lab, Mini 9
legal and ethical principles.
project
(Affective domain)
 Demonstrate effective
Work effectively as an
individual and team  Demonstrate effective
individual or in teams to Performance in
operations communication, problem-
develop machine learning Lab classes,
1 to 11 PO 10 communication, solving, conflict
models for the given Model Lab, Mini
problem solving, resolution and leadership
problem. (Affective project
conflict resolution and skills.
domain)
leadership skills.
CO-PO/PSO MAPPING

Program Outcomes PSO


CO Course Outcome Statements
1 2 3 4 5 6 7 8 9 10 11 12 I II
Implement the concepts of Artificial Intelligence
1 and Machine Learning to solve the real world 3 3
problems. (Apply)
Analyze various Artificial Intelligence and Machine
2 Learning techniques for the effective solution for a 3 3
given problem. (Analyze)
Evaluate the performance of a machine learning
3 3 3
model using various parameters. (Evaluate)
Develop machine learning models to find the
4 optimal solution for the given real world scenario. 3 3 2 3
(Create)
Communicate effectively to justify the solutions to
5 the given problems based on legal and ethical 2 2
principles. (Affective domain)
Work effectively as an individual or in teams to
6 develop machine learning models for the given 2
problem. (Affective domain)
SETHU INSTITUTE OF TECHNOLOGY
(An Autonomous Institution | Accredited with ‘A++’ Grade by NAAC)
PULLOOR, KARIAPATTI - 626 115
List of Experiment
Ex.N
Date Title of the experiment Marks Signature
o
Write a python program to construct the water jug
1.
problem using Breadth First Search
Write a python program to construct N-Puzzle problem
2.
using Depth First Search
Solve travelling salesperson problem using Best First
3.
Search
Build a Knowledge based system for forecasting the
4.
weather
Write a program to construct a Bayesian network
considering medical data. Use this model to demonstrate
5.
the diagnosis of heart patients using standard Heart
Disease Data Set
Develop an algorithm to predict whether a particular
6. customer buy a computer or not based on the following
attribute age, income, student and credit rating.
Demonstrate the working of decision tree based on ID3
algorithm. Use an appropriate data set for building the
7.
decision tree and apply this knowledge to classify the
new sample
Construct model to predict the residential home prize as
8.
a function of the homes Living area
Develop a model to determine the likelihood of a
9. patient’s successful response to a specific medical
treatment
Develop a model to predict stock market using machine
10.
learning algorithm.
Develop a model to predict the values using clustering
11.
method
Ex. No.1 Water Jug Problem using Breadth First Search

Problem: There are two jugs of volume A litre and B litre. 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 x
litre of water into the A litre jug. Assuming that we have unlimited supply of water.
Let us assume, we have A=4 litre and B= 3 litre jugs. And we want exactly 2 Litre water into
jug A (i.e 4 litre jug) how we will do this.
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 and x= 0,1,2,3,4; y represents the
quantity of water in 3-gallon jug and y = 0,1,2,3
Start State: (0,0)
Goal State: (2,0)
Generate production rules for the water jug problem. We basically perform three operations to
achieve the goal.
1. Fill water jug.
2. Empty water jug
3. and Transfer water jug

Rule State Process


(4,Y)
1 (X,Y | X<4)
{Fill 4-gallon jug}
(X,3)
2 (X,Y |Y<3)
{Fill 3-gallon jug}
(0,Y)
3 (X,Y |X>0)
{Empty 4-gallon jug}
(X,0)
4 (X,Y | Y>0)
{Empty 3-gallon jug}
(4,Y-(4-X))
5 (X,Y | X+Y>=4 ^ Y>0) {Pour water from 3-gallon jug into 4-gallon jug until 4-
gallon jug is full}
(X-(3-Y),3)
6 (X,Y | X+Y>=3 ^X>0) {Pour water from 4-gallon jug into 3-gallon jug until 3-
gallon jug is full}
(X+Y,0)
7 (X,Y | X+Y<=4 ^Y>0)
{Pour all water from 3-gallon jug into 4-gallon jug}
(0,X+Y)
8 (X,Y | X+Y <=3^ X>0)
{Pour all water from 4-gallon jug into 3-gallon jug}
(2,0)
9 (0,2)
{Pour 2 gallon water from 3 gallon jug into 4 gallon jug}
Initialization:
Start State: (0,0)
Apply Rule 2:
Fill 3-gallon jug. Now the state is (x,3)
Iteration 1:
Current State: (x,3)
Apply Rule 7:
Pour all water from 3-gallon jug into 4-gallon jug. Now the state is (3,0)
Iteration 2:
Current State : (3,0)
Apply Rule 2:
Fill 3-gallon jug. Now the state is (3,3)
Iteration 3:
Current State:(3,3)
Apply Rule 5:
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:
Empty 4-gallon jug. Now state is (0,2)
Iteration 5:
Current State : (0,2)
Apply Rule 9:
Pour 2 gallon water from 3 gallon jug into 4 gallon jug

Now the state is (2,0)-- Goal Achieved.


Running of the algorithm:

We start at an initial state in the queue where both the jugs are empty. We then continue to
explore all the possible intermediate states derived from the current jug state using the operations
provided.

We also, maintain a visited matrix of states so that we avoid revisiting the same state of jugs
again and again.

Cases Jug 1 Jug 2 Is Valid


Case 1 Fill it Empty it √
Case 2 Empty it Fill it √
Case 3 Fill it Fill it Redundant case
Case 4 Empty it Empty it Already visited (Initial State)
Case 5 Unchanged Fill it √
Case 6 Fill it Unchanged √
Case 7 Unchanged Empty √
Case 8 Empty Unchanged √
Case 9 Transfer water from this Transfer water into this √
Case 10 Transfer water into this Transfer water from this √
From the table above, we can observe that the state where both the jugs are filled is redundant
as we won’t be able to continue ahead / do anything with this state in any possible way.

So, we proceed, keeping in mind all the valid state cases (as shown in the table above) and we
do a BFS on them.

In the BFS, we skip the state which was already visited or if the amount of water in either of the
jugs exceeded the jug quantity. If we continue further, then we mark the current state as visited
and check if in this state, if we have obtained the target quantity of water in either of the jugs, we
can empty the other jug and return the current state’s entire path.

But, if we have not yet found the target quantity, we then derive the intermediate states from the
current state of jugs i.e. we derive the valid cases, mentioned in the table above. We keep
repeating all the above steps until we have found our target or there are no more states left to
proceed with.
Ex. No.2 8-Puzzle Problem using Breadth First Search

Problem statement:

The N-puzzle consists of N tiles (N+1 titles with an empty tile) where N can be 8, 15, 24 and so
on. Let us assume that N = 8. (that is square root of (8+1) = 3 rows and 3 columns). In the
same way, if we have N = 15, 24, then they have Row and columns as follows (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.

In these types of problems, we have given an initial state or initial configuration and a Goal
state or Goal Configuration. Here, we solve a problem of 8 puzzle that is a 3x3 matrix.
1 2 3
4 6
7 5 8

Initial state Goal state 1 2 3


4 5 6
7 8

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 for solving the 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)

Breath First Search to solve Eight puzzle problem


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
Ex. No.3 Traveling Salesman Problem using Best First Search

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.

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.

The best-first algorithm can be written as


Ex. No.4
Build a Knowledge based system for forecasting the weather

Knowledge
Humans reason based on existing knowledge and draw conclusions. The concept of representing
knowledge and drawing conclusions from it is also used in AI, and in this lecture we will explore
how we can achieve this behaviour.
Knowledge-Based Agents
These are agents that reason by operating on internal representations of knowledge. Consider the
Harry Potter example with the following sentences:
1. If it didn’t rain, Harry visited Hagrid today.
2. Harry visited Hagrid or Dumbledore today, but not both.
3. Harry visited Dumbledore today.
Based on these three sentences, we can answer the question “did it rain today?”, even though
none of the individual sentences tells us anything about whether it is raining today. Here is how
we can go about it: looking at sentence 3, we know that Harry visited Dumbledore. Looking at
sentence 2, we know that Harry visited either Dumbledore or Hagrid, and thus we can conclude
4. Harry did not visit Hagrid.
Now, looking at sentence 1, we understand that if it didn’t rain, Harry would have visited Hagrid.
However, knowing sentence 4, we know that this is not the case. Therefore, we can conclude
5. It rained today.
To come to this conclusion, we used logic, and today’s lecture explores how AI can use logic to
reach to new conclusions based on existing information.
Ex. No.5 Bayesian Network for Medical Data

Write a program to construct a Bayesian network considering medical data. Use this model
to demonstrate the diagnosis of heart patients using standard Heart Disease Data Set.

A Bayesian network is a directed acyclic graph in which each edge corresponds to a


conditional dependency, and each node corresponds to a unique random variable.
Bayesian network consists of two major parts: a directed acyclic graph and a set of conditional
probability distributions
 The directed acyclic graph is a set of random variables represented by nodes.
 The conditional probability distribution of a node (random variable) is defined for every
possible outcome of the preceding causal node(s). For illustration, consider the following
example. Suppose we attempt to turn on our computer, but the computer does not start
(observation/evidence). We would like to know which of the possible causes of computer failure
is more likely.
In this simplified illustration, we assume only two possible causes of this misfortune:
electricity failure and computer malfunction. The corresponding directed acyclic graph is
depicted in below figure.
Fig: Directed acyclic graph representing two independent possible causes of a computer failure.
The goal is to calculate the posterior conditional probability distribution of each of the possible
unobserved causes given the observed evidence, i.e. P [Cause | Evidence].
Data Set:
Title: Heart Disease Databases
The Cleveland database contains 76 attributes, but all published experiments refer to using a
subset of 14 of them. In particular, the Cleveland database is the only one that has been used
by ML researchers to this date. The "Heartdisease" field refers to the presence of heart disease
in the patient. It is integer valued from 0 (no presence) to 4.
Database: 0 1 2 3 4 Total
Cleveland: 164 55 36 35 13 303
Attribute Information:
1. age: age in years
2. sex: sex (1 = male; 0 = female)
3. cp: chest pain type
 Value 1: typical angina
 Value 2: atypical angina
 Value 3: non-anginal pain
 Value 4: asymptomatic
4. trestbps: resting blood pressure (in mm Hg on admission to the hospital)
5. chol: serum cholestoral in mg/dl
6. fbs: (fasting blood sugar > 120 mg/dl) (1 = true; 0 = false)
7. restecg: resting electrocardiographic results
 Value 0: normal
 Value 1: having ST-T wave abnormality (T wave inversions and/or ST elevation
or depression of > 0.05 mV)
 Value 2: showing probable or definite left ventricular hypertrophy by Estes'
criteria
8. thalach: maximum heart rate achieved
9. exang: exercise induced angina (1 = yes; 0 = no)
10. oldpeak = ST depression induced by exercise relative to rest
11. slope: the slope of the peak exercise ST segment
 Value 1: upsloping
 Value 2: flat
 Value 3: downsloping
12. ca = number of major vessels (0-3) colored by flourosopy
13. thal: 3 = normal; 6 = fixed defect; 7 = reversable defect
14. Heartdisease: It is integer valued from 0 (no presence) to 4. Diagnosis of heart disease
(angiographic disease status)
Some instance from the dataset:

ag se cp trestbp cho fb restec thalac exan oldpea slop c thal Heartdisea


e x s l s g h g k e a se
63 1 1 145 233 1 2 150 0 2.3 3 0 6 0
67 1 4 160 286 0 2 108 1 1.5 2 3 3 2
67 1 4 120 229 0 2 129 1 2.6 2 2 7 1
41 0 2 130 204 0 2 172 0 1.4 1 0 3 0
62 0 4 140 268 0 2 160 0 3.6 3 2 3 3
60 1 4 130 206 0 2 132 1 2.4 2 2 7 4
Ex.No.6
Develop an algorithm to predict whether a particular customer buy a
computer or not based on the following attribute age, income, student and
credit rating.

AIM:- To write a python program to construct an algorithm to predict whether a particular


customer buy a computer or not based on the following attribute age, income, student and credit
rating.
PROCEDURE:-
Naïve Bayes is a classification algorithm that works based on the Bayes theorem. Bayes theorem
is used to find the probability of a hypothesis with given evidence.
In this, using Bayes theorem we can find the probability of A, given that B occurred. A is the
hypothesis and B is the evidence. P(B|A) is the probability of B given that A is True.
P(A) and P(B) is the independent probabilities of A and B. Consider a data set D with a tuple X
in Bayes Theorem X works as an evidence. Let H be some hypothesis such as that the data tuple
X belongs to a specified class C.
For classification problems, we want to determine P(H|X), the probability that the hypothesis H
holds given the evidence or observed data tuple X. We want to know the probability that tuple X
belongs to class C, given that we know the attribute description of X.
Ex.No.7
Demonstrate the working of decision tree based on ID3 algorithm. Use an
appropriate data set for building the decision tree and apply this knowledge to
classify the new sample
Problem statement:
Demonstrate the working of decision tree based on ID3 algorithm. Use an appropriate data set
for building the decision tree and apply this knowledge to classify the new sample

Procedure:

Step 1: Observing the dataset

Outloo Temperatu Humidi Play


k re ty Wind Golf
Sunny Hot High Weak No
Stron
Sunny Hot High g No
Overca
st Hot High Weak Yes
Rain Mild High Weak Yes
Rain Cool Normal Weak Yes
Stron
Rain Cool Normal g No
Overca Stron
st Cool Normal g Yes
Sunny Mild High Weak No
Sunny Cool Normal Weak Yes
Rain Mild Normal Weak Yes
Stron
Sunny Mild High g Yes
Overca
st Hot Normal Weak Yes
Stron
Rain Mild High g No
Step 2: Importing the necessary basic python libraries
We are going to use pandas for manipulating the dataset and numpy library for mathematical
calculation.
Step 3: Reading the dataset
We are going to read the dataset (csv file) and load it into pandas dataframe. You can see below,
train_data_m is our dataframe. With the head() method of the dataframe we can view the first 5
rows.
Step 4: Calculating the entropy of the whole dataset

Step 5: Calculating the entropy for the filtered dataset

Using the table of step 1, The mathematics calculation will be like below for the feature Outlook:

Step 6: Calculating information gain for a feature


After calculating entropy, we have to calculate the information gain of that feature.

Step 7: Finding the most informative feature (feature with highest information gain)
Like Outlook feature, we have to calculate information gain for every feature in the dataset.
Then we have to select the feature with the highest information gain. As the feature Outlook has
the highest value, so it will be selected for our tree node.

Step 8: Adding a node to the tree


As we have found the feature name with the highest information gain, we have to generate a
node in the tree and its value as a branch. For example, we have selected Outlook, so we have to
add Outlook as a node in the tree and its value Sunny or Rain or Overcast as a branch.

Now, if any value of the feature represents only one class then we can say that the feature value
represents a pure class. If the value does not represent a pure value, we have to extend it further.

Outlook is selected as Node.


(Outlook = Sunny): Not pure class, contains both class Yes and No
(Outlook = Overcast): Pure class, contains only one class Yes
(Outlook = Rain): Not pure class, contains both class Yes and No
Step 9: Performing ID3 Algorithm and generating Tree
Now, we should ensemble the methods which should recursively do Step 4 — Step 8. So, the
overall step is:
 Finding the most informative feature
 Making a tree node with a feature name and feature values as branches
- If pure class, adding leaf node (= Class) to the tree node
- If impure class, adding an expandable node (= ‘?’) to the tree node
 Shrinking/Updating the dataset according to the pure class
 Adding the node with branches into a tree
 Expand the branch of the next impure class (= ‘?’) with an updated dataset
The recursion endpoint:
 The dataset becomes empty after updating
 There is no expandable branch (= all pure class)
Step 10: Finding unique classes of the label and Starting the algorithm
 First, we need to see what should be the expected tree:

Step 11: Predicting from the tree

We have generated the tree. Now, we can use the tree for prediction. We will recursively traverse
the nested dictionary until any leaf node (= class) is found. Remember that, the key of the
dictionary is feature name which is the datatype of string, and the value is either a dictionary
which means we have to traverse the tree more or any basic datatype like string or int or float
depending on the class data type, which means it’s a leaf node.

Step 12: Evaluating test dataset

To evaluate the model i.e. the tree we need a labeled test dataset. Then after predicting we can
calculate the difference of actual and predicted value in percentage.

Step 13: Checking test dataset and Evaluating it

The test dataset looks like below:

Outloo Temperatu Humidi Play


k re ty Wind Golf
Overca
st Hot High Weak Yes
Rain Mild High Weak Yes
Overca Stron
st Cool Normal g Yes
Sunny Mild High Weak No

If we print the accuracy, we will get 1.00 which means our accuracy is 100% correct.
Ex.No.8

Construct model to predict the residential home prize as a function of the


homes living area

Linear Regression is a Supervised Machine Learning Model for finding the relationship
between independent variables and dependent variable. Linear regression performs the task to
predict the response (dependent) variable value (y) based on a given (independent) explanatory
variable (x). So, this regression technique finds out a linear relationship between x (input) and y
(output).

Predicting house prices can help to determine the selling price of a house of a particular
region and can help people to find the right time to buy a home. In this model on House Price
Prediction using machine learning, use data from the California census to create a machine
learning model to predict house prices in the State. The data includes features such as population,
median income, and median house prices for each block group in California.
Ex. No.9
Develop a model to determine the likelihood of a patient's successful
response to a specific medical treatment

AIM:-. To write a python program to analyze a medical data set . Breast cancer dataset has been
used for analysis purpose.

PROCEDURE:

Breast cancer (BC) is one of the most common cancers among women worldwide, representing
the majority of new cancer cases and cancer-related deaths according to global statistics, making
it a significant public health problem in todays society.

The early diagnosis of BC can improve the prognosis and chance of survival significantly, as it
can promote timely clinical treatment to patients.

Because of its unique advantages in critical features detection from complex BC datasets,
machine learning (ML) is widely recognized as the methodology of choice in BC pattern
classification and forecast modelling.
Classification and data mining methods are an effective way to classify data.
Especially in medical field, where those methods are widely used in diagnosis and analysis to
make decisions
Attribute Information:
1) ID number
2) Diagnosis (M = malignant, B = benign)
3-32) Ten real-valued features are computed for each cell nucleus:
a) radius (mean of distances from center to points on the perimeter)
b) texture (standard deviation of gray-scale values)
c) perimeter
d) area
e) smoothness (local variation in radius lengths)
f) compactness (perimeter^2 / area – 1.0)
g) concavity (severity of concave portions of the contour)
h) concave points (number of concave portions of the contour)
i) symmetry
j) fractal dimension (“coastline approximation” – 1)

Ex.No.10 Stock Market Prediction

A stock market is a public market where you can buy and sell shares for publicly listed
companies. The stocks, also known as equities, represent ownership in the company. The stock
exchange is the mediator that allows the buying and selling of shares.

Stock Price Prediction


Stock Price Prediction using machine learning helps to discover the future value of company
stock and other financial assets traded on an exchange. The entire idea of predicting stock prices
is to gain significant profits. Predicting how the stock market will perform is a hard task to do.
There are other factors involved in the prediction, such as physical and psychological factors,
rational and irrational behavior, and so on. All these factors combine to make share prices
dynamic and volatile. This makes it very difficult to predict stock prices with high accuracy.

Understanding Long Short Term Memory Network


LTSMs are a type of Recurrent Neural Network for learning long-term dependencies. It is
commonly used for processing and predicting time-series data.

From the image on the top, you can see LSTMs have a chain-like structure. General RNNs have
a single neural network layer. LSTMs, have four interacting layers communicating
extraordinarily. LSTMs work in a three-step process.
 The first step in LSTM is to decide which information to be omitted from the cell in that
particular time step. It is decided with the help of a sigmoid function. It looks at the previous
state (ht-1) and the current input xt and computes the function.
 There are two functions in the second layer. The first is the sigmoid function, and the second
is the tanh function. The sigmoid function decides which values to let through (0 or 1). The
tanh function gives the weightage to the values passed, deciding their level of importance
from -1 to 1.
 The third step is to decide what will be the final output. First, you need to run a sigmoid layer
which determines what parts of the cell state make it to the output. Then, you must put the
cell state through the tanh function to push the values between -1 and 1 and multiply it by the
output of the sigmoid gate.
Ex.No.11
Develop a model to predict the values using clustering method

AIM:-
To write a python program to construct the model using k means clustering method.
PROCEDURE:-
Step 1 – First, we need to specify the number of clusters, K, need to be generated by this
algorithm.
Step 2 – Next, randomly select K data points and assign each data point to a cluster. In simple
words, classify the data based on the number of data points.
Step 3 – Now it will compute the cluster centroids.
Step 4 – Next, keep iterating the following until we find optimal centroid which is the
assignment of data points to the clusters that are not changing any more.

You might also like