PRANVEER SINGH INSTITUTE OF TECHNOLOGY, KANPUR
DEPARTMENT OF
ARTIFICIAL INTELLIGENCE & MACHINE LEARNING
Even Semester 2025-26
B. Tech. - Third Year
Semester- VI
Lab File
Machine Learning Lab
(BCAI 651)
Submitted To: Submitted By:
Faculty Name: Name:
Designation: Roll No.:
Section:
Table of Contents
➢ Vision and Mission Statements of the Institute
➢ Vision and Mission Statements of the Department
➢ PEOs, POs, PSOs of the Department
➢ Course Objective and Outcomes
➢ List of Experiments
➢ Index
Department Vision Statement
To be a recognized Department of Artificial Intelligence & Machine Learning that produces versatile
computer engineers, capable of adapting to the changing needs of computer and related industry.
Department Mission Statements
The mission of the Department of Artificial Intelligence & Machine Learning is:
i. To provide broad based quality education with knowledge and attitude to succeed in Artificial
Intelligence & Machine Learning careers.
ii. To prepare students for emerging trends in computer and related industry.
iii. To develop competence in students by providing them skills and aptitude to foster culture of
continuous and lifelong learning.
iv. To develop practicing engineers who investigate research, design, and find workable
solutions to complex engineering problems with awareness & concern for society as well as
environment.
Program Educational Objectives (PEOs)
i. The graduates will be efficient leading professionals with knowledge of Artificial Intelligence &
Machine Learning discipline that enables them to pursue higher education and/or successful careers
in various domains.
ii. Graduates will possess capability of designing successful innovative solutions to real life
problems that are technically sound, economically viable and socially acceptable.
iii. Graduates will be competent team leaders, effective communicators and capable of working
in multidisciplinary teams following ethical values.
iv. The graduates will be capable of adapting to new technologies/tools and constantly upgrading
their knowledge and skills with an attitude for lifelong learning
Department Program Outcomes (POs)
The students of Artificial Intelligence & Machine Learning Department will be able:
1. Engineering knowledge: Apply the knowledge of mathematics, science, Artificial Intelligence
& Machine Learning 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 Artificial Intelligence & Machine Learning sciences.
3. Design/development of solutions: Design solutions for complex Artificial Intelligence &
Machine Learning problems and design system components or processes that meet the specified
needs with appropriate consideration for the public health and safety, and the cultural, societal, and
environmental considerations.
4. Investigation: 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 modelling to complex Artificial Intelligence &
Machine Learning activities with an understanding of the limitations.
6. The Engineering 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 in the field of Computer Science and Engineering.
7. Environment and sustainability: Understand the impact of the professional Artificial
Intelligence & Machine Learning 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 and responsibilities and
norms of the Artificial Intelligence & Machine Learning practice.
9. Individual and team work: Function effectively as an individual, and as a member or
leader in diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex Artificial Intelligence & Machine
Learning 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 Computer
Science & 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 multidisciplinary 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.
Department Program Specific Outcomes (PSOs)
The students will be able to:
1. Use python to understand the concept of ML and implement different ML techniques.
2. Understand the Natural Language Tool Kit and its applications in solving ML problems.
Course Outcomes
*Level of Bloom’s Taxonomy Level to be met *Level of Bloom’s Level to be
Taxonomy met
L1: Remember 1 L2: Understand 2
L3: Apply 3 L4: Analyze 4
L5: Evaluate 5 L6: Create 6
CO Number Course Outcomes
BCAI-651.1 Apply suitable machine learning techniques to design solutions for diverse real-world
problems and perform experiments using real-world datasets.
BCAI-651.2 Analyze and evaluate the performance of different machine learning models, compare
their effectiveness, and justify optimizations for improved outcomes in practical
implementations.
List of Experiments
S. No. Name of Experiment Date of Experiment
Implement and demonstrate the FIND-S algorithm for finding the most
1 specific hypothesis based on a given set of training data samples. Read
the training data from a .CSV file.
For a given set of training data examples stored in a .CSV file,
implement and demonstrate the Candidate-Elimination algorithm to
2 output a description of the set of all hypotheses consistent with the
training examples.
Write a program to demonstrate the working of the decision tree based
3 ID3 algorithm. 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
4 Backpropagation algorithm and test the same using appropriate data sets.
Write a program to implement the naïve Bayesian classifier for a sample
5 training data set stored as a .CSV file. Compute the accuracy of the
classifier, considering few test data sets.
Assuming a set of documents that need to be classified, use the naïve
Bayesian Classifier model to perform this task. Built-in Java classes/API
6 can be used to write the program. Calculate the accuracy, precision, and
recall for your data set.
Write a program to construct a Bayesian network considering medical
data. Use this model to demonstrate the diagnosis of heart patients using
7 standard Heart Disease Data Set. You can use Java/Python ML library
classes/API.
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
8 results of these two algorithms and comment on the quality of clustering.
You can add Java/Python ML library classes/API in the program.
Write a program to implement k-Nearest Neighbour algorithm to classify
9 the iris data set. Print both correct and wrong predictions. Java/Python
ML library classes can be used for this problem.
Implement the non-parametric Locally Weighted Regression algorithm
10 in order to fit data points. Select appropriate data set for your experiment
and draw graphs.
Program 1
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.
Answer: -
import pandas as pd
data = pd.read_csv("[Link]")
print("Dataset:\n")
print(data)
print("")
def find_s(concepts, target):
hypothesis = None
for i in range(len(target)):
if target[i].lower() == "yes":
if hypothesis is None:
hypothesis = concepts[i].copy()
else:
for j in range(len(hypothesis)):
if hypothesis[j] != concepts[i][j]:
hypothesis[j] = '?'
print(f"After Positive Example {i+1}, Hypothesis = {hypothesis}")
return hypothesis
concepts = [Link][:, :-1].[Link]()
target = [Link][:, -1].[Link]()
final_hypothesis = find_s(concepts, target)
print("\nMost Specific Hypothesis Found by FIND-S:")
print(final_hypothesis)
Output: -
Program 2
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.
Answer: -
import csv
# Step 1: Read CSV file
data = []
with open('[Link]', 'r') as file:
reader = [Link](file)
for row in reader:
[Link](row)
# Step 2: Separate data
attributes = data[0][:-1]
examples = data[1:]
n = len(attributes)
# Step 3: Initialize S and G
S = ['0'] * n # most specific
G = [['?'] * n] # most general
# Helper function
def is_consistent(h, x):
for i in range(len(h)):
if h[i] != '?' and h[i] != x[i]:
return False
return True
# Step 4: Candidate-Elimination
for example in examples:
x = example[:-1]
label = example[-1]
if label == "Yes": # Positive example
# Remove inconsistent hypotheses from G
G = [g for g in G if is_consistent(g, x)]
# Update S
for i in range(n):
if S[i] == '0':
S[i] = x[i]
elif S[i] != x[i]:
S[i] = '?'
else: # Negative example
new_G = []
for g in G:
if is_consistent(g, x):
# Specialize g
for i in range(n):
if g[i] == '?':
if S[i] != '?':
new_h = [Link]()
new_h[i] = S[i]
new_G.append(new_h)
else:
new_G.append(g)
G = new_G
# Step 5: Output
print("Final Specific Boundary (S):")
print(S)
print("\nFinal General Boundary (G):")
for g in G:
print(g)
Output: -
Program 3
Write a program to demonstrate the working of the decision tree based ID3
algorithm. Use an appropriate data set for building the decision tree and apply this
knowledge to classify a new sample.
Answer: -
import csv
import math
from collections import Counter
# Step 1: Read CSV
data = []
with open('[Link]', 'r') as file:
reader = [Link](file)
for row in reader:
[Link](row)
attributes = data[0]
dataset = data[1:]
# Step 2: Entropy
def entropy(data):
labels = [row[-1] for row in data]
total = len(labels)
count = Counter(labels)
ent = 0
for c in [Link]():
p = c / total
ent -= p * math.log2(p)
return ent
# Step 3: Information Gain
def info_gain(data, attr_index):
total_entropy = entropy(data)
values = set([row[attr_index] for row in data])
weighted_entropy = 0
for v in values:
subset = [row for row in data if row[attr_index] == v]
weighted_entropy += (len(subset)/len(data)) * entropy(subset)
return total_entropy - weighted_entropy
# Step 4: Build Tree (ID3)
def id3(data, attrs):
labels = [row[-1] for row in data]
# If all same → return label
if [Link](labels[0]) == len(labels):
return labels[0]
# If no attributes → return majority
if len(attrs) == 0:
return Counter(labels).most_common(1)[0][0]
# Select best attribute
gains = [info_gain(data, i) for i in attrs]
best_attr = attrs[[Link](max(gains))]
tree = {attributes[best_attr]: {}}
values = set([row[best_attr] for row in data])
for v in values:
subset = [row for row in data if row[best_attr] == v]
if not subset:
tree[attributes[best_attr]][v] = Counter(labels).most_common(1)[0][0]
else:
new_attrs = [i for i in attrs if i != best_attr]
tree[attributes[best_attr]][v] = id3(subset, new_attrs)
return tree
# Step 5: Train Model
attr_indices = list(range(len(attributes)-1))
tree = id3(dataset, attr_indices)
print("Decision Tree:")
print(tree)
# Step 6: Classification
def classify(tree, sample):
if not isinstance(tree, dict):
return tree
root = list([Link]())[0]
root_index = [Link](root)
value = sample[root_index]
if value in tree[root]:
return classify(tree[root][value], sample)
else:
return "Unknown"
# Step 7: Test Sample
sample = ["Sunny", "Cool", "High", "Strong"]
result = classify(tree, sample)
print("\nNew Sample:", sample)
print("Prediction:", result)
Output: -
Program 4
Build an Artificial Neural Network by implementing the Backpropagation algorithm
and test the same using appropriate data sets.
Answer: -
import math
import random
# Sigmoid function
def sigmoid(x):
return 1 / (1 + [Link](-x))
# Derivative of sigmoid
def sigmoid_derivative(x):
return x * (1 - x)
# Training data (simple)
data = [(0,0), (1,1)]
# Initialize weights and biases
w1, w2, w3 = [Link](), [Link](), [Link]()
b1, b2, b3 = [Link](), [Link](), [Link]()
learning_rate = 0.5
# Training
for epoch in range(1000):
for x, y in data:
# -------- Forward Pass --------
z1 = w1 * x + b1
a1 = sigmoid(z1)
z2 = w2 * a1 + b2
a2 = sigmoid(z2)
z3 = w3 * a2 + b3
a3 = sigmoid(z3)
# -------- Error --------
error = (y - a3)
# -------- Backpropagation --------
delta3 = error * sigmoid_derivative(a3)
delta2 = delta3 * w3 * sigmoid_derivative(a2)
delta1 = delta2 * w2 * sigmoid_derivative(a1)
# -------- Update Weights --------
w3 += learning_rate * delta3 * a2
b3 += learning_rate * delta3
w2 += learning_rate * delta2 * a1
b2 += learning_rate * delta2
w1 += learning_rate * delta1 * x
b1 += learning_rate * delta1
# Testing
print ("Trained Weights:")
print (w1, w2, w3)
# Test new input
test_input = 1
z1 = w1 * test_input + b1
a1 = sigmoid(z1)
z2 = w2 * a1 + b2
a2 = sigmoid(z2)
z3 = w3 * a2 + b3
a3 = sigmoid(z3)
print ("\nInput:", test_input)
print ("Output:", a3)
Output: -
Program 5
Write a program to implement the naïve Bayesian classifier for a sample training
data set stored as a .CSV file. Compute the accuracy of the classifier, considering few
test data sets.
Answer: -
import csv
from collections import defaultdict
# Step 1: Read CSV
data = []
with open('[Link]', 'r') as file:
reader = [Link](file)
for row in reader:
[Link](row)
attributes = data[0]
dataset = data[1:]
# Step 2: Split into train and test
train = dataset[:10]
test = dataset[10:]
# Step 3: Calculate probabilities
class_counts = defaultdict(int)
feature_counts = {}
for row in train:
label = row[-1]
class_counts[label] += 1
for i in range(len(row)-1):
key = (i, row[i], label)
feature_counts[key] = feature_counts.get(key, 0) + 1
total = len(train)
# Step 4: Prediction function
def predict(sample):
probs = {}
for cls in class_counts:
# Prior probability
prob = class_counts[cls] / total
# Likelihood
for i in range(len(sample)):
count = feature_counts.get((i, sample[i], cls), 0)
# Laplace smoothing
prob *= (count + 1) / (class_counts[cls] + len(attributes))
probs[cls] = prob
return max(probs, key=[Link])
# Step 5: Testing and Accuracy
correct = 0
for row in test:
sample = row[:-1]
actual = row[-1]
predicted = predict(sample)
print("Sample:", sample, "Actual:", actual, "Predicted:", predicted)
if predicted == actual:
correct += 1
accuracy = (correct / len(test)) * 100
print("\nAccuracy:", accuracy, "%")
Output: -
Program 6
Assuming a set of documents that need to be classified, use the naïve Bayesian
Classifier model to perform this task. Built-in python classes/API can be used to write
the program. Calculate the accuracy, precision, and recall for your data set
Answer: -
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from [Link] import accuracy_score, precision_score, recall_score
# Step 1: Dataset
texts = [
"I love this product",
"This is amazing",
"I hate this",
"Very bad experience",
"I like it",
"Not good"
]
labels = ["Positive", "Positive", "Negative", "Negative", "Positive", "Negative"]
# Step 2: Convert text to numeric features
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(texts)
# Step 3: Split into train and test
X_train, X_test, y_train, y_test = train_test_split(X, labels, test_size=0.33, random_state=42)
# Step 4: Train Naïve Bayes model
model = MultinomialNB()
[Link](X_train, y_train)
# Step 5: Prediction
y_pred = [Link](X_test)
# Step 6: Evaluation
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, pos_label="Positive")
recall = recall_score(y_test, y_pred, pos_label="Positive")
print("Predictions:", y_pred)
print("Actual:", y_test)
print("\nAccuracy:", accuracy)
print("Precision:", precision)
print("Recall:", recall)
# Step 7: Test new document
new_doc = ["I love it"]
new_X = [Link](new_doc)
prediction = [Link](new_X)
print("\nNew Document:", new_doc[0])
print("Predicted Class:", prediction[0])
Output: -
Program 7
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.
Answer: -
pip install pgmpy
import pandas as pd
import numpy as np
from [Link] import LabelEncoder
from sklearn.model_selection import train_test_split
from [Link] import accuracy_score, precision_score, recall_score
from [Link] import DiscreteBayesianNetwork
from [Link] import MaximumLikelihoodEstimator
from [Link] import VariableElimination
# -----------------------------
# Step 1: Load dataset
# -----------------------------
data = pd.read_csv('[Link]')
# -----------------------------
# Step 2: Encode categorical
# -----------------------------
le_dict = {}
categorical_cols = ['ChestPain', 'Thal', 'AHD']
for col in categorical_cols:
le = LabelEncoder()
data[col] = le.fit_transform(data[col])
le_dict[col] = le
# -----------------------------
# Step 3: DISCRETIZE continuous (IMPORTANT for speed)
# -----------------------------
cont_cols = ['Age', 'RestBP', 'Chol', 'MaxHR']
for col in cont_cols:
data[col] = [Link](data[col], q=4, labels=False, duplicates='drop')
# -----------------------------
# Step 4: Select features
# -----------------------------
features = ['Age','Sex','ChestPain','RestBP','Chol','MaxHR','Thal']
data = data[features + ['AHD']]
# -----------------------------
# Step 5: Split
# -----------------------------
train_data, test_data = train_test_split(
data, test_size=0.3, random_state=42
)
# -----------------------------
# Step 6: Model (Naive Bayes structure)
# -----------------------------
model = DiscreteBayesianNetwork([(f, 'AHD') for f in features])
# -----------------------------
# Step 7: Train
# -----------------------------
[Link](train_data, estimator=MaximumLikelihoodEstimator)
# -----------------------------
# Step 8: Inference
# -----------------------------
inference = VariableElimination(model)
# -----------------------------
# Step 9: FAST prediction
# -----------------------------
def predict_batch(df):
preds = []
for _, row in [Link]():
evidence = row[features].to_dict()
q = [Link](variables=['AHD'], evidence=evidence, show_progress=False)
[Link](int([Link]([Link])))
return preds
# -----------------------------
# Step 10: Evaluation
# -----------------------------
y_true = test_data['AHD'].values
y_pred = predict_batch(test_data)
print("Accuracy:", accuracy_score(y_true, y_pred))
print("Precision:", precision_score(y_true, y_pred, zero_division=0))
print("Recall:", recall_score(y_true, y_pred, zero_division=0))
# -----------------------------
# Step 11: New Patient
# -----------------------------
new_patient = [Link]([{
'Age': 60,
'Sex': 1,
'ChestPain': 0,
'RestBP': 140,
'Chol': 250,
'MaxHR': 150,
'Thal': 1
}])
# Apply same discretization
for col in cont_cols:
new_patient[col] = [Link](
new_patient[col],
bins=4,
labels=False
)
result = predict_batch(new_patient)[0]
print("\nNew Patient Diagnosis (1 = Disease, 0 = No Disease):", result)
Output: -
Program 8
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.
Answer: -
import pandas as pd
import [Link] as plt
from [Link] import KMeans
from [Link] import GaussianMixture
from [Link] import silhouette_score
# Step 1: Load CSV
data = pd.read_csv('[Link]')
X = [Link]
# Step 2: Apply k-Means
kmeans = KMeans(n_clusters=2, random_state=42)
kmeans_labels = kmeans.fit_predict(X)
# Step 3: Apply EM (GMM)
gmm = GaussianMixture(n_components=2, random_state=42)
gmm_labels = gmm.fit_predict(X)
# Step 4: Evaluate (Silhouette Score)
kmeans_score = silhouette_score(X, kmeans_labels)
gmm_score = silhouette_score(X, gmm_labels)
print("k-Means Silhouette Score:", kmeans_score)
print("EM (GMM) Silhouette Score:", gmm_score)
# Step 5: Plot results
[Link](figsize=(10,5))
# k-Means Plot
[Link](1,2,1)
[Link](X[:,0], X[:,1], c=kmeans_labels)
[Link]("k-Means Clustering")
# GMM Plot
[Link](1,2,2)
[Link](X[:,0], X[:,1], c=gmm_labels)
[Link]("EM (GMM) Clustering")
[Link]()
Output: -
Program 9
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.
Answer: -
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from [Link] import KNeighborsClassifier
# Step 1: Load dataset
iris = load_iris()
X = [Link]
y = [Link]
# Step 2: Split data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42
)
# Step 3: Create k-NN model
k=3
model = KNeighborsClassifier(n_neighbors=k)
[Link](X_train, y_train)
# Step 4: Predictions
y_pred = [Link](X_test)
# Step 5: Print correct and wrong predictions
print("Correct Predictions:\n")
for i in range(len(y_test)):
if y_test[i] == y_pred[i]:
print(f"Actual: {y_test[i]}, Predicted: {y_pred[i]}")
print("\nWrong Predictions:\n")
for i in range(len(y_test)):
if y_test[i] != y_pred[i]:
print(f"Actual: {y_test[i]}, Predicted: {y_pred[i]}")
Output: -
Program 10
Implement the non-parametric Locally Weighted Regression algorithm in order to fit
data points. Select appropriate data set for your experiment and draw graphs.
Answer: -
import numpy as np
import [Link] as plt
# Step 1: Generate dataset
[Link](0)
X = [Link](0, 10, 50)
y = [Link](X) + [Link](0, 0.2, 50)
# Add bias
X_mat = [Link](([Link](len(X)), X)).T
# Step 2: Function to compute theta for a given query point
def compute_theta(x_query, X, y, tau):
W = [Link](-(X - x_query)**2 / (2 * tau**2))
W = [Link](W)
X_mat = [Link](([Link](len(X)), X)).T
theta = [Link](X_mat.T @ W @ X_mat) @ (X_mat.T @ W @ y)
return theta
# Step 3: Plot
tau = 0.5
[Link](X, y, label="Data Points")
# Select some query points (for visualization)
query_points = [Link](0, 10, 8)
for xq in query_points:
theta = compute_theta(xq, X, y, tau)
# Generate line around xq
x_local = [Link](xq - 1, xq + 1, 20)
y_local = theta[0] + theta[1] * x_local
[Link](x_local, y_local) # separate local line
[Link]("LWR: Multiple Local Regression Lines")
[Link]()
[Link]()
Output: -