AIML Lab Report-1
AIML Lab Report-1
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.
DECLARATION
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.
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.
(3PD21CS033)
PDA College of Engineering AI & ML Lab report
INDEX
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
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:
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
| O |X
----------------------
| X |
----------------------
O| |
Player 1's chance
Program Code :
import numpy as np
import pandas as pd
import os
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))
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
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)
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', '?', '?']
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))]
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] = "?"
OUTPUT:
Steps of Candidate Elimination Algorithm 4
['sunny', 'warm', '?', 'strong', '?', '?']
[['sunny', '?', '?', '?', '?', '?'],
['?', 'warm', '?', '?', '?', '?'],
['?', '?', '?', '?', '?', '?'],
['?', '?', '?', '?', '?', '?'],
['?', '?', '?', '?', '?', '?'],
['?', '?', '?', '?', '?', '?']]
Program Code :
import pandas as pd
from sklearn import tree
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import DecisionTreeClassifier
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)
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]
pred1 = labelEncoderForInput(inp1)
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
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
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
""")
OUTPUT:
Query for 'heartdisease' given 'cholestrol=2':
+-----------------+----------------------+
| heartdisease | phi(heartdisease) |
+=================+======================+
| heartdisease(0) | 0.5072 |
+-----------------+----------------------+
| heartdisease(1) | 0.4928 |
+-----------------+----------------------+
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
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
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 Engineering AI & ML Lab report
[5.2 3.4 1.4 0.2]
[4.7 3.2 1.6 0.2]
[4.8 3.1 1.6 0.2]
[5.4 3.4 1.5 0.4]
[5.2 4.1 1.5 0.1]
[5.5 4.2 1.4 0.2]
[4.9 3.1 1.5 0.2]
[5. 3.2 1.2 0.2]
[5.5 3.5 1.3 0.2]
[4.9 3.6 1.4 0.1]
[4.4 3. 1.3 0.2]
[5.1 3.4 1.5 0.2]
[5. 3.5 1.3 0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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
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
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
model.compile(optimizer='adam',
loss='categorical_crossentropy',
PDA College of Engineering AI & ML Lab report
metrics=['accuracy'])
OUTPUT: