0% found this document useful (0 votes)
10 views46 pages

AIML Lab Report-1

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)
10 views46 pages

AIML Lab Report-1

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/ 46

PDA College of Engineering AI & ML Lab report

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

CERTIFICATE
This is to certify that the AI&ML Lab report is a Bonafide work carried out by
MAHEK SULTANA BAGHBAN (3PD21CS033) in partial fulfilment for the award of
Bachelor of Engineering in Computer Science & Engineering in Poojya Doddappa Appa
College of Engineering, Kalaburagi, an Autonomous Institution affiliated to Visvesvaraya
Technological University, Belagavi during the academic year 2023-2024. The AI&ML lab
report has been approved as it satisfies the academic requirements in respect of work
prescribed.

Dr. PRIYADARSHINI P Dr SUJATA TERDAL


Guide HOD
PDA College of Engineering AI & ML Lab report

DECLARATION

I, MAHEK SULTANA BAGHBAN USN: 3PD21CS033, student of Poojya Doddappa Appa


College of Engineering, Kalaburagi hereby declare that the dissertation embodies the
report of our AL&ML Lab carried out independently by me during 6thsem of Bachelor of
Engineering, Department of Computer Science and Engineering, Kalaburagi and this work
has been submitted for the fulfilment of the requirement for the award of BE Degree.

DATE: MAHEK SULTANA BAGHBAN


PLACE: (3PD21CS033)
PDA College of Engineering AI & ML Lab report

ACKNOWLEDGEMENT
I express my deep sense of gratitude and indebtedness to my esteemed institute “PDA
College of Engineering”, Kalaburagi which has provided me an opportunity to fulfil the
most cherished desire to reach my goal.
I express my foremost gratitude to our principal Dr. S. R. Patil for his constant support and
valuable guidance.
I am thankful to Dr. Sujata Terdal, head of the COMPUTER SCIENCE AND
ENGINEERING department, for her wholehearted support in completion of this AI&ML
Lab.

Additionally, I would like to acknowledge my AI&ML Lab guide at [PDA College of


Engineering], particularly [Dr. PRIYADARSHINI P], for her continued support and for
providing the foundational knowledge that has been crucial throughout this AI&ML Lab.

Lastly, I would like to thank my family and friends for their unwavering support and
encouragement, which have been a constant source of motivation for me.

This report is a culmination of the efforts and contributions of many individuals, and I am
deeply appreciative of all the support I have received.

MAHEK SULTANA BAGHBAN

(3PD21CS033)
PDA College of Engineering AI & ML Lab report

INDEX

SL.No Programs Signature

1. Write a Program to Implement Tic-Tac-Toe game


using Python.
2. Write a Program to implement 8-Puzzle problem
using Python.
3. Write a Program to Implement Water-Jug problem
using Python.
4. Write a Program to Implement AO* Algorithm
using Python.
5. Implement and demonstrate the FIND-S algorithm
for finding the most specific hypothesis based on a
given set of training data samples. Read the training
data from a .CSV file.
6. For a given set of training data examples stored in a
.CSV file, implement and demonstrate the Candidate-
Elimination algorithm to output a description of the
set of all hypotheses consistent with the training
examples.
7. Write a program to demonstrate the working of the
decision tree based ID3 algorithm.
8. Use an appropriate data set for building the decision
tree and apply this knowledge to classify a new sample.
Build an Artificial Neural Network by implementing the
Back propagation algorithm and test the same using
appropriate datasets.
9. 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. You can use
Java/Python ML library classes/API.
10. Apply EM algorithm to cluster a set of data stored in a
.CSV file. Use the same data set for clustering using
k-Means algorithm. Compare the results of these two
algorithms and comment on the quality of clustering. You
can add Java/Python ML library classes/API in the program.
11. Write a program to implement k-Nearest Neighbour algorithm
to classify the iris data set. Print both correct and wrong
predictions. Java/Python ML library classes can be used for
this problem.
12. Implement the non-parametric Locally Weighted Regression
algorithm in order to fit data points. Select appropriate data
set for your experiment and draw graphs.
13 OPEN-ENDED PROGRAM (Handwritten digit recognition)
PDA College of Engineering AI & ML Lab report

1. Write a Program to Implement Tic-Tac-Toe game using Python.

Program Code :
# A Program to Implement Tic-Tac-Toe game using Python

import os
import time
board=[' ',' ',' ',' ',' ',' ',' ',' ',' ',' ']
player=1
#############Win Flags################
Win =1
Draw=-1
Running=0
Stop=1
#################
Game=Running
Mark='X'
def DrawBoard():
print("%c |%c |%c " % (board[1],board[2],board[3]))
print("-----------------")
print("%c |%c |%c " % (board[4],board[5],board[6]))
print("-----------------")
print("%c |%c |%c " % (board[7],board[8],board[9]))
#print("----------")
def CheckPosition(x):
if(board[x]==' '):
return True
else:
return False

def CheckWin():
PDA College of Engineering AI & ML Lab report
global Game

if(board[1]==board[2] and board[2]==board[3] and board[1]!=' '):

Game=Win
elif(board[4]==board[5] and board[5]==board[6] and board[4]!=' '):
Game=Win
elif(board[7]==board[8] and board[8]==board[9] and board[7]!=' '):
Game=Win
elif(board[1]==board[4] and board[4]==board[7] and board[1]!=' '):
Game=Win
elif(board[2]==board[5] and board[5]==board[8] and board[2]!=' '):
Game=Win
elif(board[3]==board[6] and board[6]==board[9] and board[3]!=' '):
Game=Win
elif(board[1]==board[5] and board[5]==board[9] and board[1]!=' '):
Game=Win
elif(board[3]==board[5] and board[5]==board[7] and board[3]!=' '):
Game=Win
elif(board[1]!=' ' and board[2]!=' ' and board[3]!=' ' and board[4]!=' ' and board[5]!=' ' and board[6]!=' '
and board[7]!=' ' and board[8]!=' ' and board[9]!=' '):
Game=Draw
else:
Game=Running
print("Tic-Tac-Toe game")
print("Player 1 [X]-------Player 2 [O]\n")
print()
print()
print("Please wait......")
time.sleep(3)
while(Game==Running):
os.system('cls')
DrawBoard()
PDA College of Engineering AI & ML Lab report

if(player % 2 !=0):
print("Player 1's chance")
Mark='X'

else:

print("Player 2's chance")


Mark='O'
choice=int(input("Enter the position between[1-9] where you want to mark:"))

if(CheckPosition(choice)):
board[choice]=Mark
player+=1
CheckWin()
os.system('cls')
DrawBoard()
if(Game==Draw):
print("Game Draw")
elif(Game==Win):
player-=1
if(player % 2 !=0):
print("Player 1 has Won")
else:
print("Player 2 has Won")

OUTPUT:
Tic-Tac-Toe game
Player 1 [X]-------Player 2 [O]
Please wait......
PDA College of Engineering AI & ML Lab report

| |
----------------------
| |
----------------------
| |
Player 1's chance
Enter the position between[1-9] where you want to mark:5

| |
----------------------
| X |
----------------------
| |
Player 2's chance

Enter the position between[1-9] where you want to mark:2


| O |
----------------------
| X |
----------------------
| |
Player 1's chance
Enter the position between[1-9] where you want to mark:3
| O |X
----------------------
| X |
----------------------
| |
Player 2's chance
Enter the position between[1-9] where you want to mark:7
PDA College of Engineering AI & ML Lab report

| O |X
----------------------
| X |
----------------------
O| |
Player 1's chance

Enter the position between[1-9] where you want to mark:6


| O |X
----------------------
| X |X
----------------------
O| |
Player 2's chance
Enter the position between[1-9] where you want to mark:4
| O |X
----------------------
O| X |X
----------------------
O| |
Player 1's chance
Enter the position between[1-9] where you want to mark:9
| O |X
----------------------
O| X |X
----------------------
O| |X
Player 1 has Won
PDA College of Engineering AI & ML Lab report

2. Write a Program to Implement 8-Puzzle problem using Python.

Program Code :
import numpy as np
import pandas as pd
import os

def bfs(src, target):


queue = []
queue.append(src)
exp = []
while len(queue) > 0:
source = queue.pop(0)
exp.append(source)
print(source)
if source == target:
print("Success")
return
poss_moves_to_do = possible_move(source, exp)
for move in poss_moves_to_do:
if move not in exp and move not in queue:
queue.append(move)

def possible_move(state, visited_states):


b = state.index(0)
d = []
if b not in [0, 1, 2]:
d.append('U') # Up
if b not in [6, 7, 8]:
d.append('D') # Down
PDA College of Engineering AI & ML Lab report
if b not in [0, 3, 6]:

d.append('L') # Left
if b not in [2, 5, 8]:
d.append('R') # Right

pos_moves_it_can = []
for i in d:
pos_moves_it_can.append(gen(state, i, b))

return [move_it_can for move_it_can in pos_moves_it_can if move_it_can not in visited_states]

def gen(state, m, b):


temp = state.copy()
if m == 'D':
temp[b + 3], temp[b] = temp[b], temp[b + 3]
if m == 'U':
temp[b - 3], temp[b] = temp[b], temp[b - 3]
if m == 'L':
temp[b - 1], temp[b] = temp[b], temp[b - 1]
if m == 'R':
temp[b + 1], temp[b] = temp[b], temp[b + 1]
return temp

src = [1, 2, 3, 4, 5, 6, 0, 7, 8]
target = [1, 2, 3, 4, 5, 6, 7, 8, 0]
bfs(src, target)
PDA College of Engineering AI & ML Lab report

OUTPUT:
[1, 2, 3, 4, 5, 6, 0, 7, 8]
[1, 2, 3, 0, 5, 6, 4, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 0, 8]
[0, 2, 3, 1, 5, 6, 4, 7, 8]
[1, 2, 3, 5, 0, 6, 4, 7, 8]
[1, 2, 3, 4, 0, 6, 7, 5, 8]
[1, 2, 3, 4, 5, 6, 7, 8, 0]
Success
PDA College of Engineering AI & ML Lab report

3. Write a Program to Implement Water-Jug problem using Python.

Program Code :
print("Water Jug Problem")
x=int(input("Enter X:"))
y=int(input("Enter Y:"))
while True:
rno=int(input("Enter the Rule no"))
if rno==1:
if x<4:
x=4
if rno==2:
if y<3:
y=3
if rno==3:
d=int(input("Enter how much wwater from x"))
if x>0:
x,y=x-d,y
if rno==4:
d=int(input("Enter how much water from y"))
if y>0:
x,y=x,y-d
if rno==5 or rno==12:
if x>0:
x=0
if rno==6:
if y>0:
y=0
if rno==7:
if x+y>=4 and y>0:
PDA College of Engineering AI & ML Lab report
x,y=4,y-(4-x)

if rno==8:
if x+y>=3 and x>0:
x,y=x-(3-y),3
if rno==9 or rno==11:
if x+y<=4 and y>0:
x,y=x+y,0
if rno==10:
if x+y<=3 and x>0:
x,y=0,x+y
print("X= ",x)
print("Y= ",y)
if(x==2):
print("The result is a Goal state ")
break

OUTPUT:
Initial State = (0,0)
Capacities = (4,3)
Goal State = (2,y)

Water Jug Problem


Enter X:4
Enter Y:3
Enter the Rule no 1
X= 4
Y= 3
Enter the Rule no 5
X= 0
Y= 3
Enter the Rule no 11
X= 3
PDA College of Engineering AI & ML Lab report
Y= 0

Enter the Rule no 2


X= 3
Y= 3
Enter the Rule no 7
X= 4
Y= 2
Enter the Rule no 5
X= 0
Y= 2
Enter the Rule no 11
X= 2
Y= 0
The result is a Goal state
PDA College of Engineering AI & ML Lab report

4. Write a Program to Implement AO* Algorithm using Python.


Program Code :
M='D'
I='C'
H='B'
A='A'
L='AND'
F='OR'
E=len
D=print
def J(n):
R=False;I=True;global X;D('Expanding Node:',n);G=[];H=[]
if n in B:
if L in B[n]:G=B[n][L]
if F in B[n]:H=B[n][F]
if E(G)==0 and E(H)==0:return
M=R;Q={}
while not M:
if E(Q)==E(G)+E(H):S,T=O(G,H,{});M=I;P(n,S);C[n]=T;continue
U,A=O(G,H,Q);N=R
if E(A)>1:
if A[0]in B:N=I;J(A[0])
if A[1]in B:N=I;J(A[1])
elif A in B:N=I;J(A)
if N:
V,W=O(G,H,{})
if A==W:M=I;P(n,V);C[n]=A
else:M=I;P(n,U);C[n]=A
Q[A]=1
return K(n)
PDA College of Engineering AI & ML Lab report
def O(and_nodes,or_nodes,marked):
G=marked;C={}
for B in and_nodes:
if not B[0]+B[1]in G:A=0;A=A+K(B[0])+K(B[1])+2;C[B[0]+B[1]]=A
for D in or_nodes:
if not D in G:A=0;A=A+K(D)+1;C[D]=A
E=999999;H=None
for F in C:
if C[F]<E:E=C[F];H=F
return[E,H]
def K(n):return N[n]
def P(n,cost):N[n]=cost;return
def G(node):
B='->';A=node;D(C[A],end='');A=C[A]
if E(A)>1:
if A[0]in C:D(B,end='');G(A[0])
if A[1]in C:D(B,end='');G(A[1])
elif A in C:D(B,end='');G(A)
N={A:-1,H:4,I:2,M:3,'E':6,'F':8,'G':2,'H':0,'I':0,'J':0}
B={A:{L:[(I,M)],F:[H]},H:{F:['E','F']},I:{F:['G'],L:[('H','I')]},M:{F:['J']}}
C={}
Q=J(A)
D('Nodes which gives optimal cost are')
G(A)
D('\nOptimal Cost is :: ',Q)

OUTPUT:
Expanding Node: A
Expanding Node: B
Expanding Node: C
Expanding Node: D
Nodes which gives optimal cost are
CD->HI->J
PDA College of Engineering AI & ML Lab report
Optimal Cost is :: 5

5. Implement and demonstrate the FIND-S algorithm for finding the most
specific hypothesis based on a given set of training data samples. Read the
training data from a .CSV file.
Dataset :
Sky Temperature Humidity Wind Water Forecast EnjoySport
sunny warm normal strong warm same yes
sunny warm high strong warm same yes
rainy cold high strong warm change no
sunny warm high strong cool change yes

Program Code :
import csv
num_attribute = 6
a = []
with open('enjoysport.csv', 'r') as file:
reader = csv.reader(file)
a = list(reader)

hypothesis = a[0][:-1]

for i in a:
if i[-1] == 'yes':
for j in range(num_attribute):
if i[j] != hypothesis[j]:
hypothesis[j] = '?'
print(hypothesis)
print("\nThe Maximally Specific Hypothesis for a given Training
Examples :\n")
print(hypothesis)

OUTPUT:
['sunny', 'warm', '?', 'strong', '?', '?']

The Maximally Specific Hypothesis for a given Training Examples

['sunny', 'warm', '?', 'strong', '?', '?']


PDA College of Engineering AI & ML Lab report

6. For a given set of training data examples stored in a .CSV file, implement
and demonstrate the Candidate-Elimination algorithm to output a
description of the set of all hypotheses consistent with the training
examples.
Dataset:
Sky Temperature Humidity Wind Water Forecast EnjoySport
sunny warm normal strong warm same yes
sunny warm high strong warm same yes
rainy cold high strong warm change no
sunny warm high strong cool change yes

Program Code :
import csv

with open("program06.csv") as f:
csv_file = csv.reader(f)
data = list(csv_file)

s = data[1][:-1]
g = [["?" for _ in range(len(s))] for _ in range(len(s))]

for example in data[1:]:


attributes = example[:-1]
label = example[-1]

if label.lower() == "yes":
for j in range(len(s)):
if attributes[j] != s[j]:
s[j] = "?"
g[j][j] = "?"
elif label.lower() == "no":
for j in range(len(s)):
PDA College of Engineering AI & ML Lab report
if attributes[j] != s[j]:

g[j][j] = s[j]
else:
g[j][j] = "?"

gh = [h for h in g if any(val != "?" for val in h)]

print("\nSteps of Candidate Elimination Algorithm", len(data) - 1)


print(s)
print(g)
print("\nFinal specific hypothesis:\n", s)
print("\nFinal general hypothesis:\n", gh)

OUTPUT:
Steps of Candidate Elimination Algorithm 4
['sunny', 'warm', '?', 'strong', '?', '?']
[['sunny', '?', '?', '?', '?', '?'],
['?', 'warm', '?', '?', '?', '?'],
['?', '?', '?', '?', '?', '?'],
['?', '?', '?', '?', '?', '?'],
['?', '?', '?', '?', '?', '?'],
['?', '?', '?', '?', '?', '?']]

Final specific hypothesis:


['sunny', 'warm', '?', 'strong', '?', '?']

Final general hypothesis:


[['sunny', '?', '?', '?', '?', '?'], ['?', 'warm', '?', '?', '?', '?']]
PDA College of Engineering AI & ML Lab report

7. Write a program to demonstrate the working of the decision tree based


ID3 algorithm.
Dataset:
Outlook Temperature Humidity Windy PlayTennis
sunny hot high FALSE no
sunny hot high TRUE no
overcast hot high FALSE yes
rainy mild high FALSE yes
rainy cool normal FALSE yes
rainy cool normal TRUE no
overcast cool normal TRUE yes
sunny mild high FALSE no
sunny cool normal FALSE yes
rainy mild normal FALSE yes
sunny mild normal TRUE yes
overcast mild high TRUE yes
overcast hot normal FALSE yes
rainy mild high TRUE no

Program Code :
import pandas as pd
from sklearn import tree
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import DecisionTreeClassifier

# Load and display the data


data = pd.read_csv("program07.csv")
print("The first 5 values of data is \n", data.head())

# Split data into features and labels


X = data.iloc[:, :-1]
print("\nThe first 5 values of Train data is \n", X.head())
y = data.iloc[:, -1]
print("\nThe first 5 values of Train output is \n", y.head())
PDA College of Engineering AI & ML Lab report

# Encode categorical features


le_outlook = LabelEncoder()

X.Outlook = le_outlook.fit_transform(X.Outlook)
le_Temperature = LabelEncoder()
X.Temperature = le_Temperature.fit_transform(X.Temperature)
le_Humidity = LabelEncoder()
X.Humidity = le_Humidity.fit_transform(X.Humidity)
le_Windy = LabelEncoder()
X.Windy = le_Windy.fit_transform(X.Windy)

print("\nNow the Train data is", X.head())

# Encode target variable


le_PlayTennis = LabelEncoder()
y = le_PlayTennis.fit_transform(y)
print("\nNow the Train output is\n", y)

# Train the classifier


classifier = DecisionTreeClassifier()
classifier.fit(X, y)

def labelEncoderForInput(list1):
list1[0] = le_outlook.transform([list1[0]])[0]
list1[1] = le_Temperature.transform([list1[1]])[0]
list1[2] = le_Humidity.transform([list1[2]])[0]
list1[3] = le_Windy.transform([list1[3]])[0]
return [list1]

# Prepare input data


PDA College of Engineering AI & ML Lab report
inp1 = ["rainy", "cool", "high", "FALSE"]

pred1 = labelEncoderForInput(inp1)

# Predict and inverse transform

y_pred = classifier.predict(pred1)
print(
"\nfor input {0}, we obtain {1}".format(
inp1, le_PlayTennis.inverse_transform(y_pred)[0]
)
)

OUTPUT:
The first 5 values of data is
Outlook Temperature Humidity Windy PlayTennis
0 sunny hot high False no
1 sunny hot high True no
2 overcast hot high False yes
3 rainy mild high False yes
4 rainy cool normal False yes

The first 5 values of Train data is


Outlook Temperature Humidity Windy
0 sunny hot high False
1 sunny hot high True
2 overcast hot high False
3 rainy mild high False
4 rainy cool normal False
PDA College of Engineering AI & ML Lab report

The first 5 values of Train output is


0 no
1 no
2 yes
3 yes
4 yes

Name: PlayTennis, dtype: object

Now the Train data is


Outlook Temperature Humidity Windy
0 2 1 0 0
1 2 1 0 1
2 0 1 0 0
3 1 2 0 0
4 1 0 1 0

Now the Train output is


[0 0 1 1 1 0 1 0 1 1 1 1 1 0]
for input [1, 0, 0, 1], we obtain no
PDA College of Engineering AI & ML Lab report

8. Use an appropriate data set for building the decision tree and apply this
knowledge to classify anew sample. Build an Artificial Neural Network by
implementing the Back propagation algorithm and test the same using
appropriate datasets.

Program Code :
import numpy as np
X = np.array(([2, 9], [1, 5], [3, 6]), dtype=float)
y = np.array(([92], [86], [89]), dtype=float)
X = X/np.amax(X, axis=0) # maximum of X array longitudinally
y = y/100
def sigmoid(x):
return 1/(1 + np.exp(-x))
def derivatives_sigmoid(x):
return x * (1 - x)
epoch = 5
lr = 0.1
inputlayer_neurons = 2
hiddenlayer_neurons = 3
output_neurons = 1
wh = np.random.uniform(size=(inputlayer_neurons, hiddenlayer_neurons))
bh = np.random.uniform(size=(1, hiddenlayer_neurons))
wout = np.random.uniform(size=(hiddenlayer_neurons, output_neurons))
bout = np.random.uniform(size=(1, output_neurons))
for i in range(epoch):
hinp1 = np.dot(X, wh)
hinp = hinp1 + bh
hlayer_act = sigmoid(hinp)
outinp1 = np.dot(hlayer_act, wout)
outinp = outinp1+bout
PDA College of Engineering AI & ML Lab report
output = sigmoid(outinp)

EO = y-output
outgrad = derivatives_sigmoid(output)
d_output = EO * outgrad
EH = d_output.dot(wout.T)
hiddengrad = derivatives_sigmoid(hlayer_act)
d_hiddenlayer = EH * hiddengrad
wout += hlayer_act.T.dot(d_output) * lr
wh += X.T.dot(d_hiddenlayer) * lr
print("\n-----------Epoch-", i+1, "Starts----------")
print("Input: \n" + str(X))
print("Actual Output: \n" + str(y))
print("Predicted Output: \n", output)
print("-----------Epoch-", i+1, "Ends----------\n")

OUTPUT:
-----------Epoch- 1 Starts----------
Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
Actual Output:
[[0.92]
[0.86]
[0.89]]
Predicted Output:
[[0.83318889]
[0.81973967]
[0.83201138]]
-----------Epoch- 1 Ends----------
PDA College of Engineering AI & ML Lab report
-----------Epoch- 2 Starts----------

Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
Actual Output:
[[0.92]
[0.86]
[0.89]]
Predicted Output:
[[0.83388223]
[0.82041527]
[0.83270457]]
-----------Epoch- 2 Ends----------
-----------Epoch- 3 Starts----------
Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
Actual Output:
[[0.92]
[0.86]
[0.89]]

Predicted Output:
[[0.83456351]
[0.82107935]
[0.83338571]]
-----------Epoch- 3 Ends----------
-----------Epoch- 4 Starts----------
Input:
PDA College of Engineering AI & ML Lab report
[[0.66666667 1. ]

[0.33333333 0.55555556]
[1. 0.66666667]]
Actual Output:
[[0.92]
[0.86]
[0.89]]
Predicted Output:
[[0.83523303]
[0.82173221]
[0.83405513]]
-----------Epoch- 4 Ends----------
-----------Epoch- 5 Starts----------
Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
Actual Output:
[[0.92]
[0.86]
[0.89]]
Predicted Output:
[[0.8358911 ]
[0.82237412]
[0.83471311]]
-----------Epoch- 5 Ends----------
PDA College of Engineering AI & ML Lab report

9. 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. You can use Java/Python ML library
classes/API
Dataset:
age gender family diet lifestyle cholestrol heartdisease

0 0 1 1 3 0 1
0 1 1 1 3 0 1
1 0 0 0 2 1 1
4 0 1 1 3 2 0
3 1 1 0 0 2 0
2 0 1 1 1 0 1
4 0 1 0 2 0 1
0 0 1 1 3 0 1
3 1 1 0 0 2 0
1 1 0 0 0 2 1
4 1 0 1 2 0 1
4 0 1 1 3 2 0
2 1 0 0 0 0 0
2 0 1 1 1 0 1
3 1 1 0 0 1 0
0 0 1 0 0 2 1
1 1 0 1 2 1 1
3 1 1 1 0 1 0
4 0 1 1 3 2 0

Program Code :
import pandas as pd
from pgmpy.models import BayesianModel
from pgmpy.estimators import MaximumLikelihoodEstimator
from pgmpy.inference import VariableElimination
# Load the data
data = pd.read_csv('program9.csv')
heartdisease = pd.DataFrame(data)
print(heartdisease)
# Define the Bayesian Model
PDA College of Engineering AI & ML Lab report
model = BayesianModel([

('age', 'lifestyle'),
('gender', 'lifestyle'),
('family', 'heartdisease'),
('diet', 'cholestrol'),
('lifestyle', 'diet'),
('cholestrol', 'heartdisease')
])
# Fit the model
model.fit(heartdisease, estimator=MaximumLikelihoodEstimator)
# Perform inference
heartdisease_infer = VariableElimination(model)
# Input prompts
print("""For age, enter:
SuperSeniorCitizen: 0,
SeniorCitizen: 1,
MiddleAge: 2,
Youth: 3,
Teen: 4
""")
print("""For gender, enter:
Male: 0,
Female: 1
""")
print("""For Family History, enter:
Yes: 1,
No: 0
""")
print("""For Diet, enter:
High: 0,
Medium: 1
PDA College of Engineering AI & ML Lab report
""")

print("""For Lifestyle, enter:


Athlete: 0,
Active: 1,
Moderate: 2,
Sedentary: 3
""")
print("""For Cholesterol, enter:
High: 0,
BorderLine: 1,
Normal: 2
""")
# Get user input and make a prediction
evidence = {
'age': int(input('Enter Age: ')),
'gender': int(input('Enter Gender: ')),
'family': int(input('Enter Family History: ')),
'diet': int(input('Enter Diet: ')),
'lifestyle': int(input('Enter Lifestyle: ')),
'cholestrol': int(input('Enter Cholesterol: '))
}
# Query the model
q = heartdisease_infer.query(variables=['heartdisease'], evidence=evidence)
print(q)
PDA College of Engineering AI & ML Lab report

OUTPUT:
Query for 'heartdisease' given 'cholestrol=2':
+-----------------+----------------------+
| heartdisease | phi(heartdisease) |
+=================+======================+
| heartdisease(0) | 0.5072 |
+-----------------+----------------------+
| heartdisease(1) | 0.4928 |
+-----------------+----------------------+

Query for 'heartdisease' given 'diet=1':


+-----------------+----------------------+
| heartdisease | phi(heartdisease) |
+=================+======================+
| heartdisease(0) | 0.4952 |
+-----------------+----------------------+
| heartdisease(1) | 0.5048 |
+-----------------+----------------------+
PDA College of Engineering AI & ML Lab report

10. Apply EM algorithm to cluster a set of data stored in a .CSV file. Use
the same data set for clustering using k-Means algorithm. Compare the
results of these two algorithms and comment on the quality of clustering.
You can add Java/Python ML library classes/API in the program.
Program Code :
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
from sklearn.mixture import GaussianMixture
from sklearn.datasets import load_iris
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
dataset = load_iris();
x = pd.DataFrame(dataset.data);
x.columns = ['Sepal_Length' , 'Sepal_Width' ,'Petal_Length' , 'Petal_Width'];
y = pd.DataFrame(dataset.target)
y.columns = ['Target'];
plt.figure(figsize=(14,7));
colormap = np.array(['red' , 'lime' , 'black']);
plt.subplot(1,3,1);
plt.scatter(x.Petal_Length , x.Petal_Width , c =colormap[y.Target]);
plt.title('Real');
plt.subplot(1,3,2);
model = KMeans(n_clusters=3);
model.fit(x);
predy = np.choose(model.labels_ , [0,1,2]);
plt.scatter(x.Petal_Length , x.Petal_Width , c=colormap[predy]);
plt.title('KMeans');
scaler = StandardScaler();
scaler.fit(x);
xsa = scaler.transform(x)
PDA College of Engineering AI & ML Lab report
xs = pd.DataFrame(xsa , columns = x.columns);

gmm = GaussianMixture(n_components=3);
gmm.fit(xs)
y_cluster_gmm = gmm.predict(xs);
plt.subplot(1,3,3);
plt.scatter(x.Petal_Length , x.Petal_Width , c=colormap[y_cluster_gmm]);
plt.title('GMM classification');
plt.show();

OUTPUT:
PDA College of Engineering AI & ML Lab report

11. Write a program to implement k-Nearest Neighbour algorithm to


classify the iris data set. Print both correct and wrong predictions.
Java/Python ML library classes can be used for this problem.

Program Code :
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report , confusion_matrix
from sklearn import datasets
from sklearn.neighbors import KNeighborsClassifier

iris = datasets.load_iris();
x = iris.data
y = iris.target

print("Feature :" , iris.feature_names);


print(x)
print("Labesl " , iris.target_names);
print(y)

x_train , x_test , y_train , y_test = train_test_split(x,y);


classifier = KNeighborsClassifier();
classifier.fit(x_train , y_train)
y_pred = classifier.predict(x_test)
print('confusion matrx')
print(confusion_matrix(y_test, y_pred))
print('Accuracy metics')
print(classification_report(y_test , y_pred));
PDA College of Engineering AI & ML Lab report

OUTPUT:
Feature : ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
[[5.1 3.5 1.4 0.2]
[4.9 3. 1.4 0.2]
[4.7 3.2 1.3 0.2]
[4.6 3.1 1.5 0.2]
[5. 3.6 1.4 0.2]
[5.4 3.9 1.7 0.4]
[4.6 3.4 1.4 0.3]
[5. 3.4 1.5 0.2]
[4.4 2.9 1.4 0.2]
[4.9 3.1 1.5 0.1]
[5.4 3.7 1.5 0.2]
[4.8 3.4 1.6 0.2]
[4.8 3. 1.4 0.1]
[4.3 3. 1.1 0.1]
[5.8 4. 1.2 0.2]
[5.7 4.4 1.5 0.4]
[5.4 3.9 1.3 0.4]
[5.1 3.5 1.4 0.3]
[5.7 3.8 1.7 0.3]
[5.1 3.8 1.5 0.3]
[5.4 3.4 1.7 0.2]
[5.1 3.7 1.5 0.4]
[4.6 3.6 1. 0.2]
[5.1 3.3 1.7 0.5]
[4.8 3.4 1.9 0.2]
[5. 3. 1.6 0.2]
[5. 3.4 1.6 0.4]
[5.2 3.5 1.5 0.2]
PDA College of Eng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1 5.6 2.4]
[6.9 3.1 5.1 2.3]
[5.8 2.7 5.1 1.9]
[6.8 3.2 5.9 2.3]
[6.7 3.3 5.7 2.5]
[6.7 3. 5.2 2.3]
[6.3 2.5 5. 1.9]
[6.5 3. 5.2 2. ]
[6.2 3.4 5.4 2.3]
[5.9 3. 5.1 1.8]]
Labesl ['setosa' 'versicolor' 'virginica']
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0000000000000111111111111111111111111
1111111111111111111111111122222222222
2222222222222222222222222222222222222
2 2]
PDA College of Engineering AI & ML Lab report

Confusion matrix
[[10 0 0]
[ 0 11 1]
[ 0 1 15]]
Accuracy matrix
precision recall f1-score support

0 1.00 1.00 1.00 10


1 0.92 0.92 0.92 12
2 0.94 0.94 0.94 16

accuracy 0.95 38
macro avg 0.95 0.95 0.95 38
weighted avg 0.95 0.95 0.95 38
PDA College of Engineering AI & ML Lab report

12. Implement the non-parametric Locally Weighted Regression algorithm


in order to fit data points. Select appropriate data set for your experiment
and draw graphs.
Program Code :
import numpy as np
import math
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
n = 100
xpoints = np.linspace(0,2 * math.pi , n).reshape(-1,1);
ypoints = np.sin(xpoints);
linreg = LinearRegression();
linreg.fit(xpoints , ypoints);
prediction = linreg.predict(xpoints)
plt.scatter(xpoints , ypoints , color='red');
plt.plot(xpoints , prediction)
plt.show();

OUTPUT:
PDA College of Engineering AI & ML Lab report

OPEN-ENDED PROGRAM
(HANDWRITTEN DIGIT RECOGNITION)
Introduction: The handwritten digit recognition program is a fundamental example of machine learning,
specifically focusing on image classification. Its goal is to accurately identify digits from 0 to 9 that are
written by hand. This is achieved through training a Convolutional Neural Network (CNN) on the well-
known MNIST dataset, which contains thousands of labeled examples of handwritten digits.

Dataset: The program utilizes the MNIST dataset, which includes 70,000 grayscale images of handwritten
digits, each 28x28 pixels in size. The dataset is split into 60,000 training images and 10,000 test images.
Each image is labeled with the correct digit, which allows the model to learn during training and be
evaluated during testing.

Data Preprocessing: Before training, the images are preprocessed by normalizing the pixel values to a
range between 0 and 1. This step ensures that the model's training process is efficient and that the inputs are
on a consistent scale. The images are also reshaped to include a single color channel, as required by the
CNN architecture.

Model Architecture: The core of the program is a CNN, designed to effectively recognize patterns in image
data. The model includes:

 Convolutional Layers: These layers apply filters to the input images to detect features like edges
and textures. The model uses progressively more filters to capture increasingly complex patterns.
 MaxPooling Layers: These layers reduce the spatial dimensions of the data, helping to minimize
computational load while retaining important features.
 Flatten Layer: This layer converts the 2D output of the convolutional layers into a 1D vector,
preparing it for the fully connected layers.
 Dense Layers: These layers process the flattened data, with the final layer outputting probabilities
for each of the 10 digit classes.

Training the Model: The model is trained using the training portion of the MNIST dataset. It adjusts its
internal weights over several epochs to minimize the difference between its predictions and the actual digit
labels. The Adam optimizer is used to efficiently update the model’s weights, and the categorical cross-
entropy loss function guides this process.

Evaluation and Testing: After training, the model's performance is evaluated on the test dataset. The
accuracy metric is used to determine how well the model generalizes to new, unseen data. A high test
accuracy indicates that the model has learned to recognize digits effectively.

Making Predictions: Once trained, the model can be used to predict digits from new images. It outputs a
probability distribution over the 10 digit classes, and the digit with the highest probability is chosen as the
prediction. The program also includes functionality to visualize predictions, allowing users to see how well
the model performs by comparing predicted and actual labels on test images.

Conclusion: The handwritten digit recognition program demonstrates the power of deep learning in solving
image classification tasks. By leveraging a CNN trained on the MNIST dataset, the program can accurately
identify handwritten digits, making it a valuable educational tool and a practical example of applied machine
learning.
PDA College of Engineering AI & ML Lab report

Program Code :
import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
import matplotlib.pyplot as plt
import numpy as np

# Load and preprocess the dataset


(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = x_train.reshape((x_train.shape[0], 28, 28, 1)).astype('float32') / 255


x_test = x_test.reshape((x_test.shape[0], 28, 28, 1)).astype('float32') / 255
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

# Build the CNN model


model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
MaxPooling2D((2, 2)),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Conv2D(64, (3, 3), activation='relu'),
Flatten(),
Dense(64, activation='relu'),
Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
loss='categorical_crossentropy',
PDA College of Engineering AI & ML Lab report

metrics=['accuracy'])

# Train the model


history = model.fit(x_train, y_train, epochs=5, batch_size=64, validation_split=0.2)

# Evaluate the model


test_loss, test_acc = model.evaluate(x_test, y_test)
print(f'Test accuracy: {test_acc}')

# Plot training & validation accuracy


plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label='val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0, 1])
plt.legend(loc='lower right')
plt.show()

# Make predictions on the test set


predictions = model.predict(x_test)
# Function to plot images with their predicted and actual labels
def plot_predictions(images, labels, predictions, num=10):
plt.figure(figsize=(10, 4))
for i in range(num):
plt.subplot(2, 10, i + 1)
plt.imshow(images[i].reshape(28, 28), cmap='gray')
plt.title(f'Pred: {np.argmax(predictions[i])}\nTrue: {np.argmax(labels[i])}')
plt.axis('off')
plt.show()
# Plot the first 10 predictions
plot_predictions(x_test, y_test, predictions)
PDA College of Engineering AI & ML Lab report

OUTPUT:

You might also like