0% found this document useful (0 votes)
2 views

AI&ML

The document outlines various implementations of search algorithms and machine learning models in Python, including Breadth First Search (BFS), Depth First Search (DFS), A* Algorithm, Memory-Bounded A*, Naive Bayes, Bayesian Networks, Regression Models, and Decision Trees. Each section includes the aim, algorithm steps, program code, and results of execution. The document serves as a comprehensive guide for implementing these algorithms and models in Python.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

AI&ML

The document outlines various implementations of search algorithms and machine learning models in Python, including Breadth First Search (BFS), Depth First Search (DFS), A* Algorithm, Memory-Bounded A*, Naive Bayes, Bayesian Networks, Regression Models, and Decision Trees. Each section includes the aim, algorithm steps, program code, and results of execution. The document serves as a comprehensive guide for implementing these algorithms and models in Python.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Ex.

No:1 a Implementation of Uninformed Search


Ex.No:1 a Breadth First Search (BFS)
Date:

Aim:
To write a python program for implement Breadth First Search (BFS)
Algorithm:
Step 1: Create a function bfs
Step 2: In the function perform bfs
Step 3: Get the graph and node as a input from the user
Step 4: Assign visited =[]
Step 5: Assign queue=[]
Step 6: Call the function
Program:
graph = {
'A' : ['B','C'],
'B' : ['D', 'E'],
'C' : ['F'],
'D' : [],
'E' : ['F'],
'F' : []
}

visited = [] # List to keep track of visited nodes.


queue = [] #Initialize a queue

def bfs(visited, graph, node):


visited.append(node)
queue.append(node)

while queue:
s = queue.pop(0)
print (s, end = " ")

for neighbour in graph[s]:


if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)

# Driver Code
bfs(visited, graph, 'A')

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 1


Output:-

Result:
Thus the program for python Implementation BFS is executed and the output is
obtained.

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 2


Ex.No:1 b Implementation of Uninformed Search
Depth First Search (DFS)
Date:

Aim:
To write a python program for Implementation Depth First Search (DFS)
Algorithm:
Step 1: Create a function dfs
Step 2: In the function perform dfs
Step 3: Get the graph and node as a input from the user
Step 4: Assign dfs()
Step 5: Call the function
Program:
graph = {
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'8' : []
}
visited = set() # Set to keep track of visited nodes of graph.
def dfs(visited, graph, node): #function for dfs
if node not in visited:
print (node)
visited.add(node)
for neighbour in graph[node]:
dfs(visited, graph, neighbour)
print("Following is the Depth-First Search")
dfs(visited, graph, '5')

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 3


Output:

Result:
Thus the program for python Implementation DFS is executed and the output is
obtained.

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 4


Ex.No:2 a Implementation of Informed Search
A* Algorithm
Date:

Aim:
To write a python program to perform the A* algorithm.

Algorithm:

Step 1: start the program

Step 2: place the standing node in the open list

Step 2: check if the open node is empty or not if the list is empty return failure and
stop

Step 4: select the node from the user while it has the smallest value

Step 5: expand node A and generate all of its successor and put into closed list

Step 6: else if node n is already open and closed then H should be attached to the back

Step 7: next pointer which reflects the g(n) value

Step 8: return the step2

Step 9: stop the program.

Program:
import heapq
def aStarAlgo(start_node, stop_node):
open_heap = [(0, start_node)]
closed_set = set()
g = {}
parents = {}
g[start_node] = 0
parents[start_node] = start_node
while len(open_heap) > 0:
(f, n) = heapq.heappop(open_heap)
if n == stop_node or Graph_nodes[n] is None:
pass

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 5


else:
for (m, weight) in get_neighbors(n):
if m not in closed_set:
tentative_g = g[n] + weight
if m not in g or tentative_g < g[m]:
g[m] = tentative_g
parents[m] = n
f = tentative_g + heuristic(m)
heapq.heappush(open_heap, (f, m))
if n==stop_node:
path=[]
while parents[n] != n:
path.append(n)
n = parents[n]
path.append(start_node)
path.reverse()
print("path Found")
print("path=",end="")
for i in path:
print(i,end="")
if i!=path[len(path)-1]:
print(end='->')
return path
open_set.remove(n)
closed_set.add(n)
print('Path does not exist!')
return None

def get_neighbors(v):
if v in Graph_nodes:
return Graph_nodes[v]
else:
return None

def heuristic(n):
H_dist = {'A': 11, 'B': 6, 'C': 99, 'D': 1, 'E': 7, 'G': 0}
return H_dist[n]

Graph_nodes = {
'A': [('B', 2), ('E', 3)],
'B': [('A', 2), ('C', 1), ('G', 9)],
'C': [('B', 1)],

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 6


'D': [('E', 6), ('G', 1)],
'E': [('A', 3), ('D', 6)],
'G': [('B', 9), ('D', 1)]
}
aStarAlgo('A', 'C')

Output:-

Result:
Thus, the python program for implementation of A * algorithm was executed and its
output is verified successfully.

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 7


Ex.No:2 b Implementation of Informed Search
Memory-Bounded A*
Date:

Aim:
To write a python program to implement the memory-bounded A*.

Algorithm:
Step 1: Start the program
Step 2: Get the graph as user input.
Step 3: Create a function A with parameters (Start, goal, path, level, max D).
Step 4: Ger the depth limit as user input.
Step 5: Check for the goal node and return present or not present.
Step 6: Call the function with required parameters.
Step 7: Stop the program and Exit

Program:
graph = {
'A': ['B', 'C' ],
'B': ['D', 'E'],
'C': ['F','G',],
'D': ['H', 'I'],
'E': ['J','K'],
'F':['L','M'],
'G': ['N', 'O'],
'H':[ ],
'I':[ ],
'J':[ ],
'K':[ ],
'L':[ ],
'M':[ ],

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 8


'N':[ ],
'O':[ ],
}

def ma(start,goal,path,level,maxd):
path.append(start)
if start==goal:
return path
if level==maxd:
return False
for child in graph [start]:
if ma(child,goal,path,level+1,maxd):
return path
path.pop()

start='A'
goal='K'
maxd=3
print()
path=list()
res=ma(start,goal,path,0,maxd)
if res:
print("path Found")
print("path=",end="")
for i in path:
print(i,end="")
if i!=path[len(path)-1]:
print(end='->')

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 9


else:
print("Path not found")

Output:-

Result:
Thus, the python program for implementation of memory-bounded A* is executed
and its output is verified.

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 10


EX No:3
Date: NAIVE BAYES MODELS
Aim:
To write a python program to implement the naive Bayes models.

Algorithm:
Step1 : Start
Step 2 : Calculate the prior probability for given class labels
Step 3 : Find Likelihood probability with each attribute for each class
Step 4 : Put this value in Bayes Formula and calculate posterior
probability.
Step 5 : See which class has a higher probability, given the input belongs to
the higher probability class.
Step 6 : Stop the program

Program
import pandas as pd
from sklearn import tree
from sklearn.preprocessing import LabelEncoder
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

data = pd.read_csv('/content/tennisdata.csv')
print("The first 5 values of data is :\n",data.head())

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())

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 11


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 :\n",X.head())

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

X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.20)


classifier = GaussianNB()
classifier.fit(X_train,y_train)
print("Accuracy is:",accuracy_score(classifier.predict(X_test),y_test))

Commands:-
pip install pandas
pip install scikit-learn

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 12


Output:-

Result:
Thus, the python program for implementation of naive bayes models is executed
and its output is verified.

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 13


EX No:4
Date: BAYESIAN NETWORK
Aim:
To write a python program to implement the bayesian network.
Algorithm:
Step 1 : Start
Step 2 : Import the required modules
Step 3 : Read the csv files and replace the null values
Step 4 : Build the Bayesian model by passing the appropriate argument to the
bayesian network function
Step 5 : Fill the model by using maximum likelihood criterion and Influencing the
network is done by using variable elimination method and the probability is
completed given some of data from the dataset
Step 6 : Stop the program and Exit

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

heartDisease = pd.read_csv('heart.csv')
heartDisease = heartDisease.replace('?', np.nan)

print('Few examples from the dataset are given below')


print(heartDisease.head())

model = BayesianNetwork([('age', 'trestbps'),


('age', 'fbs'),
('sex', 'trestbps'),
('exang', 'trestbps'),
('trestbps', 'heartdisease'),
('fbs', 'heartdisease'),
('heartdisease', 'restecg'),

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 14


('heartdisease', 'thalach'),
('heartdisease', 'chol')])
print('\nLearning CPD using Maximum likelihood estimators')
model.fit(heartDisease, estimator=MaximumLikelihoodEstimator)
print('\nInferencing with Bayesian Network:')
heartDisease_infer = VariableElimination(model)
q = heartDisease_infer.query(variables=['heartdisease'], evidence={'age': 67})
print('\n1. Probability of HeartDisease given Age=67 : ',q.values[1])
q = heartDisease_infer.query(variables=['heartdisease'], evidence={'chol': 229})
print('\n2. Probability of HeartDisease given cholesterol=229 : ',q.values[1])

Commands:-
pip install numpy
pip install pandas
pip install pgmpy

Output:-

Result:
Thus, the python program for implementation of bayesian network is executed and its output
is verified.

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 15


EX No:5
Date: REGRESSION MODEL
Aim:
To write a python program to implement the regression model.

Algorithm:
Step 1 : Start
Step 2 : Import the required modules
Step 3 : Provide data to work with and eventually do appropriate transformations
Step 4 : Create a regression model and fit it with existing data
Step 5 : Check the result of model fitting to know whether the model is satisfactor
Step 6 : Apply the model for prediction
Step 7 : Print the result
Step 8 : Stop the Program

Program:
from math import ceil
import numpy as np
from scipy import linalg
import matplotlib.pyplot as plt

def lowess(x, y, f, iterations):


n = len(x)
r = int(ceil(f * n))
h = [np.sort(np.abs(x - x[i]))[r] for i in range(n)]
w = np.clip(np.abs((x[:, None] - x[None, :]) / h), 0.0, 1.0)
w = (1 - w ** 3) ** 3
yest = np.zeros(n)
delta = np.ones(n)
for iteration in range(iterations):
for i in range(n):
weights = delta * w[:, i]
b = np.array([np.sum(weights * y), np.sum(weights * y * x)])
A = np.array([[np.sum(weights), np.sum(weights * x)],[np.sum(weights * x), np.sum(weights
* x * x)]])
beta = linalg.solve(A, b)

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 16


yest[i] = beta[0] + beta[1] * x[i]
residuals = y - yest
s = np.median(np.abs(residuals))
delta = np.clip(residuals / (6.0 * s), -1, 1)
delta = (1 - delta ** 2) ** 2
plt.plot(x, y, "r.")
plt.plot(x, yest, "b-")
plt.show()
n = 100
x = np.linspace(0, 2 * np.pi, n)
y = np.sin(x) + 0.3 * np.random.randn(n)
f = 0.25
iterations = 3
yest = lowess(x, y, f, iterations)
Command:-
pip install numpy scipy matplotlib

Output:-

Result:
Thus, the python program for implementation of regression model is executed and its output is
verified.

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 17


EX No:6a
Date: DECISION TREES
Aim:
To write a python program to implement the decision trees.

Algorithm:

Step 1 : Start
Step 2 : Import the necessary libraries
Step 3 : Load the Titanic dataset
Step 4 : Remove columns that are not necessary for the classification task
Step 5 : Separate inputs (features) and target (labels)
Step 6 : Convert the 'Sex' column from string to integer (male: 1, female: 2)
Step 7 : Fill missing values in the 'Age' column with the mean age
Step 8 : Split the data into training and testing sets
Step 9 : Create a decision tree classifier and train it on the training set
Step 10 : Evaluate the model on the testing set
Step 11 : Stop the program.

Program:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier

# load the Titanic dataset


df = pd.read_csv("titanic.csv")

# remove columns that are not necessary for the classification task
df.drop(['PassengerId', 'Name', 'SibSp', 'Parch', 'Ticket', 'Cabin', 'Embarked'], axis=1, inplace=True)

# separate inputs (features) and target (labels)


inputs = df.drop('Survived', axis=1)
target = df['Survived']

# convert the 'Sex' column from string to integer (male: 1, female: 2)


inputs['Sex'] = inputs['Sex'].map({'male': 1, 'female': 2})

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 18


# fill missing values in the 'Age' column with the mean age
inputs['Age'] = inputs['Age'].fillna(inputs['Age'].mean())

# split the data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(inputs, target, test_size=0.2)

# create a decision tree classifier and train it on the training set


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

# evaluate the model on the testing set


accuracy = model.score(X_test, y_test)
print(f"Model accuracy: {accuracy}")

Commands:-
pip install pandas
pip install scikit-learn

Output:-

Result:
Thus, the python program for implementation of decision trees is executed and its output is
verified.

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 19


EX No:6b
Date: RANDOM FORESTS

Aim:
To write a python program to implement the random forests.
Algorithm:
Step 1 : Start
Step 2 : Import the necessary libraries: sklearn.datasets, sklearn.model_selection,
sklearn.ensemble, and pandas.
Step 3 : Load the iris dataset using the load_iris() function from the sklearn.datasets
module.
Step 4 : Create a pandas DataFrame from the iris data and feature names using the
pd.DataFrame() function.
Step 5 : Add a new column 'target' to the DataFrame containing the target labels.
Step 6 : Split the data into training and testing sets using the train_test_split() function
from the sklearn.model_selection module.
Step 7 : Create a RandomForestClassifier object with default hyperparameters using the
RandomForestClassifier() function from the sklearn.ensemble module.
Step 8 : Fit the model on the training data using the fit() method of the
RandomForestClassifier object.
Step 9 : Calculate the accuracy score of the model on the testing data using the score()
method of the RandomForestClassifier object.
Step 10 : Print the accuracy score.
Step 11 : Create another RandomForestClassifier object with 40 estimators by passing
n_estimators=40 as an argument to the RandomForestClassifier() function.
Step 12 : Fit the new model on the training data using the fit() method of the
RandomForestClassifier object.
Step 13 : Stop the program.

Program:
# import necessary libraries
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
import pandas as pd

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 20


# load iris dataset
iris = load_iris()

# create a pandas DataFrame from the iris data and feature names
df = pd.DataFrame(iris.data, columns=iris.feature_names)

# add a new column 'target' to df containing the target labels


df['target'] = iris.target

# split the data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(df.drop(['target'], axis='columns'), iris.target,
test_size=0.2)

# create a RandomForestClassifier object with default hyperparameters


model = RandomForestClassifier()

# fit the model on the training data


model.fit(X_train, y_train)

# calculate the accuracy score of the model on the testing data


score = model.score(X_test, y_test)
print("Accuracy score with default hyperparameters:", score)

# create a RandomForestClassifier object with 40 estimators


model = RandomForestClassifier(n_estimators=40)

# fit the model on the training data


model.fit(X_train, y_train)

# calculate the accuracy score of the model on the testing data


score = model.score(X_test, y_test)
print("Accuracy score with 40 estimators:", score)

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 21


Commands:-
pip install -U scikit-learn

Output:-

Result:
Thus, the python program for implementation of random forests is executed and its output is
verified.

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 22


EX No:7
Date: SVM MODELS

Aim:
To write a python program to implement the svm models.
Algorithm:
Step 1 : Start
Step 2 : Import the necessary libraries.
Step 3 : Create a Pandas dataframe from the digits data and target.
Step 4 : Split the data into training and testing sets
Step 5 : Create an SVM model with radial basis function (RBF) kernel
Step 6 : Fit the RBF SVM model to the training data
Step 7 : Evaluate and print the accuracy of the RBF SVM model on the test data .
Step 8 : Create an SVM model with linear kernel
Step 9 : Fit the linear SVM model to the training data
Step 10 : Evaluate and print the accuracy of the linear SVM model on the test data
Step 11 : Stop the program.

Program:
import pandas as pd
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC

# load the digits dataset


digits = load_digits()

# create a pandas dataframe from the digits data and target


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

# split the data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(df.drop('target', axis='columns'), df.target,
test_size=0.3)

# create an SVM model with radial basis function (RBF) kernel and fit it to the training data

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 23


rbf_model = SVC(kernel='rbf')
rbf_model.fit(X_train, y_train)

# evaluate the accuracy of the RBF SVM model on the test data
rbf_model_score = rbf_model.score(X_test, y_test)
print("Accuracy of RBF SVM model:", rbf_model_score)

# create an SVM model with linear kernel and fit it to the training data
linear_model = SVC(kernel='linear')
linear_model.fit(X_train, y_train)

# evaluate the accuracy of the linear SVM model on the test data
linear_model_score = linear_model.score(X_test, y_test)
print("Accuracy of linear SVM model:", linear_model_score)

Command:-
pip install scikit-learn

Output:-

Result:
Thus, the python program for implementation of svm models is executed and its output is
verified.

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 24


EX No:8
Date: ENSEMBLING TECHNIQUES

Aim:
To write a python program to implement the ensembling techniques.
Algorithm:
Step 1 : Start
Step 2 : Import the necessary libraries.
Step 3 : Read the diabetes dataset using pd.read_csv() method and assign it to a variable
df.
Step 4 : Check the first few rows of the dataframe using df.head() method.
Step 5 : Check for missing values using df.isnull().sum() method.
Step 6 : Get summary statistics of the dataframe using df.describe() method.
Step 7 : Count the number of instances of each class in the Outcome column using
df.Outcome.value_counts() method.
Step 8 : Separate the feature variables (X) from the target variable (y) using
df.drop("Outcome", axis="columns") and df.Outcome respectively.
Step 9 : Standardize the feature variables using StandardScaler() method and assign it to a
variable scaler.
Step 10 : Split the dataset into training and testing sets with stratification using
train_test_split() method.
Step 11 : Check the dimensions of the training set
Step 12 : Calculate the proportion of class instances in the testing set
Step 13 : Calculate and print the decision and random forest classifiers
Step 14 : Stop the program.

Program:
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier

# read the diabetes dataset


df = pd.read_csv("diabetes.csv")

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 25


# check the first few rows of the dataframe
df.head()

# check for missing values


df.isnull().sum()

# get summary statistics of the dataframe


df.describe()

# count the number of instances of each class in the Outcome column


df.Outcome.value_counts()

# separate the feature variables (X) from the target variable (y)
X = df.drop("Outcome", axis="columns")
y = df.Outcome

# standardize the feature variables


scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
X_scaled[:3]

# split the dataset into training and testing sets with stratification
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, stratify=y, random_state=10)

# check the dimensions of the training set


X_train.shape

# check the dimensions of the testing set


X_test.shape

# count the number of instances of each class in the training set


y_train.value_counts()

# calculate the proportion of class instances in the training set


y_train.value_counts() / len(y_train)

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 26


# count the number of instances of each class in the testing set
y_test.value_counts()

# calculate the proportion of class instances in the testing set


y_test.value_counts() / len(y_test)

# evaluate the performance of a decision tree classifier using cross-validation


dt_scores = cross_val_score(DecisionTreeClassifier(), X_scaled, y, cv=5)
print("Decision Tree Score:",dt_scores.mean())

# evaluate the performance of a random forest classifier using cross-validation


rf_scores = cross_val_score(RandomForestClassifier(n_estimators=50), X_scaled, y, cv=5)
print("Random Forest Score:",rf_scores.mean())

Command:-
pip install scikit-learn

Output:-

Result:
Thus, the python program for implementation of ensembling techniques is executed and its
output is verified.

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 27


EX No:9
Date: CLUSTERING ALGORITHM

Aim:
To write a python program to implement the clustering algorithm.
Algorithm:
Step 1 : Start
Step 2 : Import the necessary libraries.
Step 3 : Load the dataset using pandas read_csv method.
Step 4 : Plot the dataset using matplotlib scatter plot to visualize the relationship between
'Age' and 'Income($)'.
Step 5 : Create a KMeans model with the desired number of clusters (n_clusters) and
number of times the algorithm will be run with different centroid seeds (n_init).
Step 6 : Predict the cluster for each data point in the dataset using the KMeans model's
fit_predict method.
Step 7 : Assign the predicted clusters to the dataset by adding a new column called
'cluster'.
Step 8 : Plot the dataset again, but this time color-coded based on their predicted clusters,
and mark the centroid of each cluster using a different marker and color.
Step 9 : To improve the accuracy of the clustering model, scale the 'Age' and 'Income($)'
features using MinMaxScaler. Then, repeat steps 4 to 7.
Step 10 : Plot the elbow curve to determine the optimal number of clusters by computing
the sum of squared distances between data points and their assigned cluster
centers for different values of k.
Step 11 : Stop the program.

Program:
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.preprocessing import MinMaxScaler

# Load data
df = pd.read_csv("/content/income (1).csv")

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 28


# Scale data
scaler = MinMaxScaler()
df[['Income($)', 'Age']] = scaler.fit_transform(df[['Income($)', 'Age']])

# Fit K-Means model and make predictions


kmeans = KMeans(n_clusters=3, n_init=10)
y_predicted = kmeans.fit_predict(df[['Age', 'Income($)']])

# Add cluster labels to the dataframe


df['cluster'] = y_predicted

# Plot clusters
colors = ['green', 'red', 'black']
centroids = kmeans.cluster_centers_
for i in range(3):
plt.scatter(df[df['cluster'] == i]['Age'], df[df['cluster'] == i]['Income($)'], color=colors[i])
plt.scatter(centroids[:, 0], centroids[:, 1], color='purple', marker='*', label='centroid')
plt.xlabel('Age')
plt.ylabel('Income ($)')
plt.legend()
plt.show()

# Elbow curve to select optimal number of clusters


sse = []
k_rng = range(1, 10)
for k in k_rng:
kmeans = KMeans(n_clusters=k, n_init=10)
kmeans.fit(df[['Age', 'Income($)']])
sse.append(kmeans.inertia_)
plt.plot(k_rng, sse)
plt.xlabel('K')
plt.ylabel('Sum of squared error')
plt.show()

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 29


Command:-
pip install scikit-learn pandas
pip install matplotlib

Output:-

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 30


Result:
Thus, the python program for implementation of clustering algorithm is executed and its output
is verified

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 31


EX No:10
EM FOR BAYESIAN NETWORKS
Date:

Aim:
To write a python program to implement the em for Bayesian networks.
Algorithm:
Step 1 : Start

Step 2 : Define the Bayesian network structure (DAG) and the conditional probability
tables (CPTs) for each variable.

Step 3 : Initialize the parameters using the defined CPTs.

Step 4 : Define the observed data as a NumPy array.

Step 5 : Define the E-step function that takes the observed data and current parameters
as inputs and returns the posterior probabilities for each data point.

Step 6 : Define the M-step function that takes the observed data and posterior
probabilities as inputs and updates the parameters using the observed data and
posterior probabilities.

Step 7 : Run the EM algorithm for ten iterations.

Step 8 : In each iteration, call the E-step function to compute the posterior probabilities,
followed by the M-step function to update the parameters.

Step 9 : After the specified number of iterations, the final CPTs will be the estimated
probabilities for each variable given the observed data.

Step 10 : Stop the program.

Program:
import numpy as np
dag = [[], [0]]

cpt0 = np.array([0.3, 0.7])


cpt1 = np.array([[0.5, 0.5], [0.2, 0.8]])

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 32


params = {'cpt0': cpt0, 'cpt1': cpt1}

data = np.array([[0, 1], [1, 1], [1, 0], [0, 0]])

def e_step(data, params):


cpt0 = params['cpt0']
cpt1 = params['cpt1']
posterior = np.zeros((len(data), 2))
for i, obs in enumerate(data):
prob = cpt0[obs[0]] * cpt1[obs[1], obs[0]]
posterior[i, 0] = prob
posterior[i, 1] = 1 - prob
posterior[i, :] /= posterior[i, :].sum()
return posterior

def m_step(data, posterior):


cpt0 = np.zeros(2)
cpt1 = np.zeros((2, 2))
for i, obs in enumerate(data):
cpt0[obs[0]] += posterior[i, 0]
cpt1[obs[1], obs[0]] += posterior[i, 0]
cpt0 /= cpt0.sum()
cpt1 /= cpt1.sum(axis=0)
return {'cpt0': cpt0, 'cpt1': cpt1}

for i in range(10):
posterior = e_step(data, params)
params = m_step(data, posterior)

print(params['cpt0'])
print(params['cpt1'])

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 33


Command:-
pip install numpy
Output:-

Result:
Thus, the python program for implementation of em for Bayesian networks is executed and its
output is verified

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 34


EX No:11
SIMPLE NN MODELS
Date:

Aim:
To write a python program to implement the simple nn models.
Algorithm:
Step 1 : Start

Step 2 : Import the necessary libraries.

Step 3 : Define the input and output data

Step 4 : Define the neural network model architecture

Step 5 : Compile the model by specifying the loss function, optimizer, and evaluation
metrics

Step 6 : Train the model using the training data

Step 7 : Evaluate the model's performance on the training data

Step 8 : Stop the program.

Program:
import numpy as np
from keras.models import Sequential
from keras.layers import Dense

X = np.array([[0,0], [0,1], [1,0], [1,1]])


y = np.array([[0], [1], [1], [0]])
model = Sequential()
model.add(Dense(4, input_dim=2, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, y, epochs=5, batch_size=4)
scores = model.evaluate(X, y)

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 35


print("\n%s: %.2f%%" % (model.metrics_names[1], 4))

Commands:-
pip install numpy
pip install keras
pip install tensorflow or pip install tensorflow-gpu

Output:-

Result:
Thus, the python program for implementation of simple nn models is executed and its output
is verified.

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 36


EX No:12
DEEP LEARNING NN MODELS
Date:

Aim:
To write a python program to implement the deep learning nn models.
Algorithm:
Step 1 : Start

Step 2 : Import the necessary libraries.

Step 3 : Define the input and output data

Step 4 : Define the neural network model architecture

Step 5 : Compile the model by specifying the loss function, optimizer, and evaluation
metrics

Step 6 : Train the model using the training data

Step 7 : Evaluate the model's performance on the training data

Step 8 : Stop the program.

Program:
import tensorflow as tf
import numpy as np

X = np.array([[0,0], [0,1], [1,0], [1,1]])


y = np.array([[0], [1], [1], [0]])

model = tf.keras.Sequential([
tf.keras.layers.Dense(4, activation='relu', input_shape=(2,)),
tf.keras.layers.Dense(4, activation='relu'),
tf.keras.layers.Dense(4, activation='relu'),
tf.keras.layers.Dense(4, activation='relu'),
tf.keras.layers.Dense(4, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 37


])
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, y, epochs=5)
scores = model.evaluate(X, y)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))

Commands:-
pip install numpy
pip install keras
pip install tensorflow or pip install tensorflow-gpu

Output:-

Result:
Thus, the python program for implementation of deep learning nn models is executed and its
output is verified.

CS3491-ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING 38

You might also like