0% found this document useful (0 votes)
12 views41 pages

Aiml Record1

It is A record of AIML

Uploaded by

shanejo1506
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)
12 views41 pages

Aiml Record1

It is A record of AIML

Uploaded by

shanejo1506
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

EX 1(a): Implementation of Uninformed search algorithms (BFS)

Program:

from collections import deque

def bfs(graph, start):


visited = []
queue = deque([start])

while queue:
node = queue.popleft()
if node not in visited:
print(node, end=' ')
visited.append(node)
for neighbor in graph[node]:
if neighbor not in visited:
queue.append(neighbor)

graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': ['F'],
'F': []
}

print("Breadth First Search Travel:")


bfs(graph, 'A')

Output:

Breadth First Search Travel:


A B C D E F
EX 1(b): Implementation of Uninformed search algorithms (DFS)

Program:

def dfs(vc, graph, node):


if node not in vc:
print(node, end=' ')
vc.append(node)
for i in graph[node]:
if i not in vc:
dfs(vc, graph, i)

graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': ['F'],
'F': []
}

vc = []
print("Depth First Search Travel:")
dfs(vc, graph, 'A')

Output:
Depth First Search Travel:
A B D E F C
EX2 (a): Implementation of Informed search algorithms (A*, memory-bounded A*)

Program:

import heapq

def a_star(graph, start, goal, h):


open_list = []
closed_list = set()
g_cost = {start: 0}
f_cost = {start: h[start]}
came_from = {}

heapq.heappush(open_list, (f_cost[start], start))

while open_list:
_, current = heapq.heappop(open_list)

if current == goal:
path = []
while current in came_from:
path.append(current)
current = came_from[current]
path.append(start)
path.reverse()
return path

closed_list.add(current)

for neighbor, weight in graph[current].items():


if neighbor in closed_list:
continue

tentative_g_cost = g_cost[current] + weight


if neighbor not in g_cost or tentative_g_cost <
g_cost[neighbor]:
came_from[neighbor] = current
g_cost[neighbor] = tentative_g_cost
f_cost[neighbor] = tentative_g_cost + h[neighbor]
heapq.heappush(open_list, (f_cost[neighbor], neighbor))

return None

graph = {
'A': {'B': 1, 'C': 3},
'B': {'A': 1, 'D': 3, 'E': 1},
'C': {'A': 3, 'E': 1},
'D': {'B': 3},
'E': {'B': 1, 'C': 1}
}

h = {
'A': 4,
'B': 3,
'C': 2,
'D': 3,
'E': 1
}

start = 'A'
goal = 'D'

path = a_star(graph, start, goal, h)


print("Path from start to goal:", path)

Output:
Path from start to goal: ['A', 'B', 'D']
EX 2b) Implementation of Informed search algorithms (A*, memory-bounded A*)

PROGRAM:

import math

def ma_star(graph, heuristic, start, goal, max_memory):


stack = [(0, start, [start])]
min_fcost = {start: 0}
while stack:
(f, node, path) = stack.pop()
if node == goal:
return path
if math.isnan(f) or f > max_memory:
continue
for adjacent, cost in graph[node]:
g = cost + min_fcost[node]
if adjacent in min_fcost and g >= min_fcost[adjacent]:
continue
f = g + heuristic[adjacent]
stack.append((f, adjacent, path + [adjacent]))
min_fcost[adjacent] = g
return None

graph = {
'S': [('A', 1), ('B', 3)],
'A': [('B', 1), ('C', 3)],
'B': [('C', 1), ('D', 2)],
'C': [('D', 1), ('G', 2)],
'D': [('E', 3)],
'E': [('G', 2)],
'G': []
}
heuristic = {'S': 7, 'A': 6, 'B': 2, 'C': 4, 'D': 2, 'E': 1, 'G': 0}

start = 'S'
goal = 'G'
max_memory = 8

path = ma_star(graph, heuristic, start, goal, max_memory)


print('Shortest path:', path)

OUTPUT:

Shortest path: ['S', 'B', 'C', 'G']


EX 3): Implement Naïve Bayes models

Program:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score, confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt

data = pd.read_csv('/content/drive/MyDrive/DATASET - AIML/IRIS.csv')

X = data.drop('species', axis=1)
y = data['species']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,


random_state=42)

model = GaussianNB()
model.fit(X_train, y_train)

y_pred = model.predict(X_test)

accuracy = accuracy_score(y_test, y_pred)


print("Accuracy:", accuracy)

# Confusion Matrix
cm = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(6, 4))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues',
xticklabels=model.classes_, yticklabels=model.classes_)
plt.title('Confusion Matrix')
plt.xlabel('Predicted')
plt.ylabel('True')
plt.show()
Output:
EX4): Implement Bayesian Networks

Program:
import numpy as np
import pandas as pd
from pgmpy.estimators import MaximumLikelihoodEstimator
from pgmpy.models import DiscreteBayesianNetwork
from pgmpy.inference import VariableElimination

heartDisease = pd.read_csv("/content/drive/MyDrive/DATASET -
AIML/heartdisease.csv")
heartDisease = heartDisease.replace('?', np.nan)

print('Sample instances from the dataset are given below')


print(heartDisease.head())

model = DiscreteBayesianNetwork([
('age', 'heartdisease'),
('exang', 'heartdisease'),
('cp', 'heartdisease'),
('heartdisease', 'restecg'),
('heartdisease', 'chol')
])

print('\nLearning CPD using Maximum likelihood estimators')


model.fit(heartDisease, estimator=MaximumLikelihoodEstimator)

print('\nInferencing with Bayesian Network:')


HeartDisease_test_infer = VariableElimination(model)

print('\n1. Probability of HeartDisease given evidence= restecg')


q1 = HeartDisease_test_infer.query(variables=['heartdisease'],
evidence={'restecg': 1})
print(q1)
OUTPUT:

Sample instances from the dataset are given below


age sex cp trestbps chol fbs restecg thalach exang oldpeak
slope \
0 52 1 0 125 212 0 1 168 0 1.0
2
1 53 1 0 140 203 1 0 155 1 3.1
0
2 70 1 0 145 174 0 1 125 1 2.6
0
3 61 1 0 148 203 0 1 161 0 0.0
2
4 62 0 0 138 294 1 1 106 0 1.9
1

ca thal heartdisease
0 2 3 0
1 0 3 0
2 0 3 0
3 1 3 0
4 3 2 0

Learning CPD using Maximum likelihood estimators

Inferencing with Bayesian Network:

1. Probability of HeartDisease given evidence= restecg


+-----------------+---------------------+
| heartdisease | phi(heartdisease) |
+=================+=====================+
| heartdisease(0) | 0.4157 |
+-----------------+---------------------+
| heartdisease(1) | 0.5843 |
+-----------------+---------------------+

Program:
print('\n2. Probability of HeartDisease given evidence= cp')
q2 = HeartDisease_test_infer.query(variables=['heartdisease'],
evidence={'cp': 2})
print(q2)
OUTPUT:

2. Probability of HeartDisease given evidence= cp


+-----------------+---------------------+
| heartdisease | phi(heartdisease) |
+=================+=====================+
| heartdisease(0) | 0.3420 |
+-----------------+---------------------+
| heartdisease(1) | 0.6580 |
+-----------------+---------------------+
EX 5a): Build Regression models - Linear Regression

PROGRAM:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

df = pd.read_csv('/content/drive/MyDrive/DATASET - AIML/IRIS.csv')
X = df[['petal_length']]
Y = df['sepal_length']

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3,


random_state=1)

model = LinearRegression()
model.fit(X_train, Y_train)

Y_pred = model.predict(X_test)

plt.figure(figsize=(10, 5))

plt.subplot(1, 2, 1)
plt.scatter(X_train, Y_train, color='blue')
plt.plot(X_train, model.predict(X_train), color='red')
plt.title('Training Set')
plt.xlabel('Petal Length')
plt.ylabel('Sepal Length')

plt.subplot(1, 2, 2)
plt.scatter(X_test, Y_test, color='green')
plt.plot(X_test, Y_pred, color='red')
plt.title('Test Set')
plt.xlabel('Petal Length')
plt.ylabel('Sepal Length')

plt.tight_layout()
plt.show()
OUTPUT:

EX 5 b): Build Regression models - Multiple Linear Regression


PROGRAM:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

data = pd.read_csv('/content/drive/MyDrive/DATASET - AIML/IRIS.csv')

X = data[['petal_length', 'petal_width', 'sepal_width']]


Y = data['sepal_length']

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3,


random_state=1)

model = LinearRegression()
model.fit(X_train, Y_train)

Y_pred = model.predict(X_test)

plt.scatter(Y_test, Y_pred, color='purple')


plt.plot([Y_test.min(), Y_test.max()], [Y_test.min(), Y_test.max()],
color='red')
plt.title('Actual vs Predicted Sepal Length')
plt.xlabel('Actual Sepal Length')
plt.ylabel('Predicted Sepal Length')
plt.show()
OUTPUT:
EX 5 c):Build Regression models - Logistic Regression

PROGRAM:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix,
ConfusionMatrixDisplay

data = pd.read_csv('/content/drive/MyDrive/DATASET - AIML/IRIS.csv')

X = data[['petal_length', 'petal_width', 'sepal_width']]


Y = data['species']

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3,


random_state=1)

model = LogisticRegression(max_iter=200)
model.fit(X_train, Y_train)

Y_pred = model.predict(X_test)

accuracy = accuracy_score(Y_test, Y_pred)


print("Accuracy:", accuracy)

cm = confusion_matrix(Y_test, Y_pred, labels=model.classes_)


disp = ConfusionMatrixDisplay(confusion_matrix=cm,
display_labels=model.classes_)
disp.plot()
plt.title("Confusion Matrix")
plt.show()

OUTPUT:
EX 6 a):Build Decision Trees and Random Forests
PROGRAM:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.metrics import confusion_matrix

data = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)
df['species'] = data.target

X = df.drop('species', axis=1)
Y = df['species']

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3,


random_state=42)

model = DecisionTreeClassifier()
model.fit(X_train, Y_train)

plt.figure(figsize=(12, 8))
plot_tree(model, feature_names=X.columns, class_names=data.target_names,
filled=True)
plt.show()

Y_pred = model.predict(X_test)

OUTPUT:
cm = confusion_matrix(Y_test, Y_pred)
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues",
xticklabels=data.target_names, yticklabels=data.target_names)
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title('Confusion Matrix')
plt.show()
OUTPUT:

EX 6 b):Build Decision Trees and Random Forests


PROGRAM:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix
from sklearn.tree import plot_tree

data = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)
df['species'] = data.target

X = df.drop('species', axis=1)
Y = df['species']

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3,


random_state=42)

model = RandomForestClassifier(n_estimators=10, random_state=42)


model.fit(X_train, Y_train)

Y_pred = model.predict(X_test)

cm = confusion_matrix(Y_test, Y_pred)
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues",
xticklabels=data.target_names, yticklabels=data.target_names)
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title('Confusion Matrix')
plt.show()

for i, tree in enumerate(model.estimators_):


plt.figure(figsize=(12, 8))
plot_tree(tree, feature_names=X.columns,
class_names=data.target_names, filled=True)
plt.title(f'Decision Tree {i+1}')
plt.show()

OUTPUT:
EX 7):Build SVM Models
PROGRAM:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import confusion_matrix, accuracy_score

data = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)
df['species'] = data.target

X = df.drop('species', axis=1)
Y = df['species']

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3,


random_state=42)

model = SVC(kernel='linear', random_state=42)


model.fit(X_train, Y_train)

Y_pred = model.predict(X_test)

cm = confusion_matrix(Y_test, Y_pred)
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues",
xticklabels=data.target_names, yticklabels=data.target_names)
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title('Confusion Matrix')
plt.show()

train_accuracy = accuracy_score(Y_train, model.predict(X_train))


print(f'Training Accuracy: {train_accuracy:.4f}')

OUTPUT:
EX 8): Implement Ensembling Techniques
PROGRAM:
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import BaggingClassifier, AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt

iris = load_iris()
X = iris.data
y = iris.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,


random_state=42)

base_classifier = DecisionTreeClassifier(max_depth=2)

bagging_clf = BaggingClassifier(estimator=base_classifier,
n_estimators=10, random_state=42)
bagging_clf.fit(X_train, y_train)
bagging_pred = bagging_clf.predict(X_test)
bagging_accuracy = accuracy_score(y_test, bagging_pred)
print("Bagging Accuracy:", bagging_accuracy)
OUTPUT:
Bagging Accuracy: 1.0

adaboost_clf = AdaBoostClassifier(n_estimators=10, random_state=42)


adaboost_clf.fit(X_train, y_train)
adaboost_pred = adaboost_clf.predict(X_test)
adaboost_accuracy = accuracy_score(y_test, adaboost_pred)
print("AdaBoost Accuracy:", adaboost_accuracy)
OUTPUT:
AdaBoost Accuracy: 1.0

accuracy_scores = [bagging_accuracy, adaboost_accuracy]


classifiers = ['Bagging', 'AdaBoost']

plt.figure(figsize=(5, 4))
plt.bar(classifiers, accuracy_scores, color=['blue', 'green'])
plt.xlabel('Classifiers')
plt.ylabel('Accuracy')
plt.title('Accuracy of Bagging vs AdaBoost')
plt.ylim(0.8, 1.0)
plt.show()

OUTPUT:

EX 9 a):Implement Clustering Algorithm - Kmeans Clustering


PROGRAM:
import pandas as pd
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt

df = pd.read_csv('/content/drive/MyDrive/DATASET - AIML/IRIS.csv',
encoding='ascii')
print(df.head())

print(df.columns)

X = df.drop(['species'], axis=1)

kmeans = KMeans(n_clusters=3, random_state=0).fit(X)


df['Cluster'] = kmeans.labels_

plt.figure(figsize=(10, 6), facecolor='white')


plt.scatter(df['sepal_length'], df['sepal_width'], c=df['Cluster'],
cmap='viridis', marker='o')
plt.title('KMeans Clustering of Iris Dataset (Sepal Measurements)')
plt.xlabel('Sepal Length (cm)')
plt.ylabel('Sepal Width (cm)')
plt.colorbar(label='Cluster Label')
plt.show()

plt.figure(figsize=(10, 6), facecolor='white')


plt.scatter(df['petal_length'], df['petal_width'], c=df['Cluster'],
cmap='viridis', marker='o')
plt.title('KMeans Clustering of Iris Dataset (Petal Measurements)')
plt.xlabel('Petal Length (cm)')
plt.ylabel('Petal Width (cm)')
plt.colorbar(label='Cluster Label')
plt.show()

print('Clusters plotted based on Petal measurements.')

OUTPUT:

sepal_length sepal_width petal_length petal_width species


0 5.1 3.5 1.4 0.2 Iris-setosa
1 4.9 3.0 1.4 0.2 Iris-setosa
2 4.7 3.2 1.3 0.2 Iris-setosa
3 4.6 3.1 1.5 0.2 Iris-setosa
4 5.0 3.6 1.4 0.2 Iris-setosa
Index(['sepal_length', 'sepal_width', 'petal_length', 'petal_width',
'species'],
dtype='object')

EX 9 b): Implement Clustering Algorithm - KNN Clustering


PROGRAM:
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns

data = pd.read_csv('/content/drive/MyDrive/DATASET - AIML/IRIS.csv')

print("Column names:", data.columns)

data.columns = data.columns.str.strip()

print(data.head())

X = data.drop('species', axis=1)
y = data['species']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,


random_state=42)

scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

k = 5
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X_train, y_train)

y_pred = knn.predict(X_test)
print(f'Accuracy: {accuracy_score(y_test, y_pred)}')

error_rate = []
for i in range(1, 40):
knn = KNeighborsClassifier(n_neighbors=i)
knn.fit(X_train, y_train)
pred_i = knn.predict(X_test)
error_rate.append(np.mean(pred_i != y_test))

plt.figure(figsize=(10, 6))
plt.plot(range(1, 40), error_rate, color='blue', linestyle='dashed',
marker='o',
markerfacecolor='red', markersize=10)
plt.title('Error Rate vs. K Value')
plt.xlabel('K')
plt.ylabel('Error Rate')
plt.show()

conf_matrix = confusion_matrix(y_test, y_pred)


plt.figure(figsize=(10, 7))
sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues',
xticklabels=knn.classes_,
yticklabels=knn.classes_)
plt.xlabel('Predicted')
plt.ylabel('True')
plt.title('Confusion Matrix')
plt.show()

OUTPUT:
Column names: Index(['sepal_length', 'sepal_width', 'petal_length',
'petal_width',
'species'],
dtype='object')
sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 Iris-setosa
1 4.9 3.0 1.4 0.2 Iris-setosa
2 4.7 3.2 1.3 0.2 Iris-setosa
3 4.6 3.1 1.5 0.2 Iris-setosa
4 5.0 3.6 1.4 0.2 Iris-setosa
Accuracy: 1.0
EX 10): Implement EM for Bayesian networks
PROGRAM:

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
import seaborn as sns

mu1, sigma1 = 2, 1
mu2, sigma2 = -1, 0.8
X1 = np.random.normal(mu1, sigma1, size=200)
X2 = np.random.normal(mu2, sigma2, size=600)
X = np.concatenate([X1, X2])

sns.kdeplot(X)
plt.xlabel('X')
plt.ylabel('Density')
plt.title('Density Estimation of X')
plt.show()

OUTPUT:

mu1_hat, sigma1_hat, pi1_hat = 1, 1, 0.5


mu2_hat, sigma2_hat, pi2_hat = -2, 1, 0.5
epochs = 100
log_likelihoods = []

for epoch in range(epochs):


gamma1 = pi1_hat * norm.pdf(X, mu1_hat, sigma1_hat) / (
pi1_hat * norm.pdf(X, mu1_hat, sigma1_hat) + pi2_hat * norm.pdf(X,
mu2_hat, sigma2_hat))
gamma2 = 1 - gamma1

mu1_hat = np.sum(gamma1 * X) / np.sum(gamma1)


sigma1_hat = np.sqrt(np.sum(gamma1 * (X - mu1_hat)**2) /
np.sum(gamma1))
pi1_hat = np.mean(gamma1)

mu2_hat = np.sum(gamma2 * X) / np.sum(gamma2)


sigma2_hat = np.sqrt(np.sum(gamma2 * (X - mu2_hat)**2) /
np.sum(gamma2))
pi2_hat = np.mean(gamma2)

log_likelihood = np.sum(np.log(pi1_hat * norm.pdf(X, mu1_hat,


sigma1_hat) +
pi2_hat * norm.pdf(X, mu2_hat,
sigma2_hat)))
log_likelihoods.append(log_likelihood)

plt.plot(log_likelihoods)
plt.xlabel('Epochs')
plt.ylabel('Log-Likelihood')
plt.title('Log-Likelihood over Epochs')
plt.show()
OUTPUT:

sns.kdeplot(X, label='True Density', color='blue')


x_vals = np.linspace(np.min(X), np.max(X), 1000)
mixture_density = pi1_hat * norm.pdf(x_vals, mu1_hat, sigma1_hat) +
pi2_hat * norm.pdf(x_vals, mu2_hat, sigma2_hat)
plt.plot(x_vals, mixture_density, label='Mixture Density', color='red')
plt.xlabel('X')
plt.ylabel('Density')
plt.title('Final Estimated Density')
plt.legend()
plt.show()
OUTPUT:

EX 11):Build Simple NN Models


PROGRAM:
import tensorflow as tf

model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu', input_shape=(784,)),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

model.summary()

OUTPUT:

EX 12):Build Deep NN Models


PROGRAM:
import tensorflow as tf

model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

model.summary()

OUTPUT:

You might also like