AIML Lab Record: Simplified Programs
This document contains shortened, easy-to-understand Python programs for all 17
experiments in the AIML Lab manual. Each program is designed to be functional while
keeping the logic clear for study purposes.
1(a). Breadth-First Search (BFS)
BFS explores a graph layer-by-layer using a Queue (First-In-First-Out).
graph = {'5': ['3', '7'], '3': ['2', '4'], '7': ['8'], '2': [], '4': ['8'],
'8': []}
visited = []
queue = []
def bfs(visited, graph, node):
[Link](node)
[Link](node)
while queue:
m = [Link](0)
print(m, end=' ')
for neighbor in graph[m]:
if neighbor not in visited:
[Link](neighbor)
[Link](neighbor)
bfs(visited, graph, '5')
1(b). Depth-First Search (DFS)
DFS explores as far as possible along a branch before backtracking using recursion.
graph = {'5': ['3', '7'], '3': ['2', '4'], '7': ['8'], '2': [], '4': ['8'],
'8': []}
visited = set()
def dfs(visited, graph, node):
if node not in visited:
print(node)
[Link](node)
for neighbour in graph[node]:
dfs(visited, graph, neighbour)
dfs(visited, graph, '5')
2(a). A* Search Algorithm
Uses f(n) = g(n) + h(n) to find the shortest path from start to goal.
def aStarAlgo(start, stop):
open_set = set([start])
g = {start: 0}
parents = {start: start}
while open_set:
n = min(open_set, key=lambda v: g[v] + H_dist[v])
if n == stop:
path = []
while parents[n] != n:
[Link](n); n = parents[n]
[Link](start); [Link]()
return print('Path found:', path)
open_set.remove(n)
for (m, weight) in Graph_nodes.get(n, []):
if m not in g or g[n] + weight < g[m]:
g[m] = g[n] + weight
parents[m] = n
open_set.add(m)
H_dist = {'A': 11, 'B': 6, 'C': 99, 'D': 1, 'E': 7, 'G': 0}
Graph_nodes = {'A': [('B', 2), ('E', 3)], 'B': [('C', 1), ('G', 9)], 'E':
[('D', 6)], 'D': [('G', 1)]}
aStarAlgo('A', 'G')
2(b). Memory Bounded A* (SMA*)
A memory-constrained version of A* that prunes nodes when memory limit is hit.
import heapq
def sma(start, goal, graph, limit):
frontier = [(0, start)]
cost = {start: 0}
while frontier:
curr = [Link](frontier)[1]
if curr == goal: return print('Goal reached')
for nbr, weight in graph[curr].items():
new_cost = cost[curr] + weight
if nbr not in cost or new_cost < cost[nbr]:
cost[nbr] = new_cost
[Link](frontier, (new_cost, nbr))
while len(frontier) > limit:
[Link](frontier)
graph = {(0,0): {(0,1):1, (1,0):1}, (0,1): {(1,1):1}, (1,0): {(1,1):1},
(1,1): {}}
sma((0,0), (1,1), graph, 3)
3. Naive Bayes Classification
Implements the Gaussian Naive Bayes model for categorical prediction.
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
df = pd.read_csv('[Link]')
X, y = [Link][:, :-1].values, [Link][:, -1].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
model = GaussianNB().fit(X_train, y_train)
print('Accuracy:', [Link](X_test, y_test))
4. Bayesian Networks (Spam Detection)
Uses CountVectorizer and MultinomialNB for text classification.
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
data = pd.read_csv('SMSSpamCollection', sep='\t', header=None,
names=['label', 'text'])
vec = CountVectorizer()
X = vec.fit_transform(data['text'])
y = data['label']
model = MultinomialNB().fit(X[:4000], y[:4000])
print('Accuracy:', [Link](X[4000:], y[4000:]))
5(a). Simple Linear Regression
Calculates slope and intercept to find a linear relationship between x and y.
import numpy as np
x, y = [Link]([0,1,2,3,4,5,6,7,8,9]), [Link]([1,3,2,5,7,8,8,9,10,12])
b1 = [Link](x, y)[0,1] / [Link](x)
b0 = [Link](y) - b1 * [Link](x)
print(f'Equation: y = {b1:.2f}x + {b0:.2f}')
5(b). Multiple Regression
Simulates 3D data point generation for multiple input variables.
import numpy as np
x1, x2 = [Link](100), [Link](100)
y = 2*x1 + 3*x2 + 5 + [Link](100)
print('Sample target values:', y[:5])
6(a). Decision Tree
Uses entropy criterion to build a classification tree.
import pandas as pd
from [Link] import DecisionTreeClassifier
from [Link] import LabelEncoder
df = pd.read_csv('[Link]')
for col in ['Sex', 'BP', 'Cholesterol']:
df[col] = LabelEncoder().fit_transform(df[col])
X = df[['Age', 'Sex', 'BP', 'Cholesterol', 'Na_to_K']]
y = df['Drug']
model = DecisionTreeClassifier(criterion='entropy').fit(X, y)
print('Score:', [Link](X, y))
6(b). Random Forest
An ensemble method that uses multiple trees for majority-vote prediction.
from [Link] import RandomForestClassifier
import pandas as pd
df = pd.read_csv('User_Data.csv')
X, y = [Link][:, [2, 3]].values, [Link][:, 4].values
model = RandomForestClassifier(n_estimators=10).fit(X, y)
print('Prediction:', [Link](X[:5]))
7. Support Vector Machine (SVM)
Demonstrates linear and RBF kernels for class separation.
from sklearn import svm, datasets
iris = datasets.load_iris()
X, y = [Link][:, :2], [Link]
model_lin = [Link](kernel='linear').fit(X, y)
model_rbf = [Link](kernel='rbf').fit(X, y)
print('Linear:', model_lin.score(X,y), 'RBF:', model_rbf.score(X,y))
8(a). Bagging
Reduces variance by training models on data subsets.
from [Link] import BaggingClassifier
from [Link] import load_wine
X, y = load_wine(return_X_y=True)
model = BaggingClassifier(n_estimators=12, oob_score=True).fit(X, y)
print('OOB Score:', model.oob_score_)
8(b). Boosting (AdaBoost)
Sequential learners where each corrects previous errors.
from [Link] import AdaBoostClassifier
import pandas as pd
from [Link] import LabelEncoder
df = pd.read_csv('[Link]').apply(LabelEncoder().fit_transform)
X, y = [Link]('class', axis=1), df['class']
model = AdaBoostClassifier(n_estimators=50).fit(X, y)
print('Accuracy:', [Link](X, y))
9(a). Hierarchical Clustering
Unsupervised grouping using the linkage method.
from [Link] import linkage
data = list(zip([4,5,10,4,3,11,14,6,10,12], [21,19,24,17,16,25,24,22,21,21]))
link = linkage(data, method='ward')
print('Linkage Matrix Shape:', [Link])
9(b). Density-Based Clustering (DBSCAN)
Clusters data based on spatial density and marks outliers.
from [Link] import DBSCAN
from [Link] import make_blobs
X, _ = make_blobs(n_samples=750, centers=[[1,1],[-1,-1]], cluster_std=0.4)
db = DBSCAN(eps=0.3, min_samples=10).fit(X)
print('Clusters:', len(set(db.labels_)) - (1 if -1 in db.labels_ else 0))
9(c). K-Means Clustering
Groups data into K pre-defined clusters using centroids.
from [Link] import KMeans
from [Link] import load_digits
data, _ = load_digits(return_X_y=True)
model = KMeans(n_clusters=10).fit(data)
print('Cluster labels for first 5:', model.labels_[:5])
10. EM for Bayesian Networks
Estimates parameters for models with latent/hidden variables.
import pandas as pd
import numpy as np
from [Link] import BayesianNetwork
from [Link] import ExpectationMaximization as EM
data = [Link]([Link](0, 2, size=(1000, 3)), columns=['A',
'C', 'D'])
data['B'] = [Link]
model = BayesianNetwork([('A', 'B'), ('C', 'B'), ('C', 'D')])
est = EM(model, data)
params = est.get_parameters(latent_card={'B': 3})
print('Learned Table:', params[0])
11. Simple Neural Network
A manual implementation of forward pass and backpropagation.
from numpy import exp, array, random, dot
class NN:
def __init__(self): self.w = 2 * [Link]((3, 1)) - 1
def train(self, i, o, it):
for _ in range(it):
p = 1 / (1 + exp(-dot(i, self.w)))
self.w += dot(i.T, (o - p) * p * (1 - p))
nn = NN()
X, y = array([[0,0,1], [1,1,1]]), array([[0,1]]).T
[Link](X, y, 10000)
print('Weight:', nn.w)
12. Deep Learning Model (Keras)
A production-ready neural network using the Keras library.
from [Link] import Sequential
from [Link] import Dense
import numpy as np
X, y = [Link](10, 8), [Link](0, 2, 10)
model = Sequential([
Dense(12, input_shape=(8,), activation='relu'),
Dense(1, activation='sigmoid')
])
[Link](loss='binary_crossentropy', optimizer='adam')
[Link](X, y, epochs=5, verbose=0)
print('Model Summary:', [Link]())