Cs-3491-Ai-Ml-Lab RECORD
Cs-3491-Ai-Ml-Lab RECORD
Algorithm:
BFS
Following is the Breadth-First Search
537248
DFS
Following is the Depth-First Search
5
3
2
4
8
7
Result:
Thus the program for BFS and DFS is executed successfully and output is verified.
IV. IMPLEMENTING INFORMED SEARCH ALGORITHMS
LIKE A* AND MEMORY-BOUNDED A*
AIM
V. The aim of a C program for implementing informed search algorithms like
A* and memory-bounded A* is to efficiently find the shortest path between two
points in a graph or network. The A* algorithm is a heuristic-based search algorithm that
finds the shortest path between two points by evaluating the cost function of each possible
path. The memory- bounded A* algorithm is a variant of the A* algorithm that uses a limited
amount of memory and is suitable for large search spaces.
Algorithm:
VI.
VII. Algorithm for A*
VIII.
1. Initialize the starting node with a cost of zero and add it to an open list.
2. While the open list is not empty:
a. Find the node with the lowest cost in the open list and remove it.
b. If this node is the goal node, return the path to this node.
Generate all successor nodes of the current node.
c. For each successor node, calculate its cost and add it to the open list.
3.If the open list is empty and the goal node has not been found, then there is no path from
the start node to the goal node
IX. Algorithm for memory-bounded A*
X.
1. Initialize the starting node with a cost of zero and add it to an open list and a closed list.
2. While the open list is not empty:
a. Find the node with the lowest cost in the open list and remove it.
b. If this node is the goal node, return the path to this node.
c. Generate all successor nodes of the current node.
d. For each successor node, calculate its cost and add it to the open list if it is not in
the closed list.
e. If the open list is too large, remove the node with the highest cost from the open
list and add it to the closed list.
f. Add the current node to the closed list.
3. If the open list is empty and the goal node has not been found, then there is no path from
the start node to the goal node.
Program:
from queue importPriorityQueue
v =14
graph =[[] fori inrange(v)]
forv, c ingraph[u]:
ifvisited[v] ==False:
visited[v] =True
pq.put((c, v))
print()
classPriorityQueue:
"""Priority queue implementation using heapq"""
def init (self):
self.elements = []
defis_empty(self):
returnlen(self.elements) == 0
class Node:
"""Node class for representing the search tree"""
def init (self, state, parent=None, action=None, path_cost=0): self.state
= state
self.parent = parent
self.action = action
self.path_cost = path_cost
defmemory_bounded_a_star_search(start_state, max_memory):
"""Memory-bounded A* search algorithm"""
frontier = PriorityQueue()
frontier.put(Node(start_state), 0)
explored = set()
memory = {start_state: 0}
while not frontier.is_empty():
node = frontier.get()
ifnode.state not in explored:
explored.add(node.state)
ifis_goal_state(node.state):
returnget_solution_path(node)
forchild_state, action, step_cost in get_successor_states(node.state):
child_node = Node(child_state, node, action, node.path_cost + step_cost)
child_node_f = child_node.path_cost + heuristic(child_state)
ifchild_state not in memory or child_node_f< memory[child_state]:
frontier.put(child_node, child_node_f)
memory[child_state] = child_node_f
whilememory_usage(memory) >max_memory:
state_to_remove = min(memory, key=memory.get)
del memory[state_to_remove]
return None
defget_successor_states(state):
"""Function for generating successor states"""
# Replace with actual successor state generation logic
return []
defis_goal_state(state):
"""Function for checking if a state is the goal state"""
# Replace with actual goal state checking logic
return False
defget_solution_path(node):
"""Function for retrieving the solution path"""
path = []
whilenode.parent is not None:
path.append((node.action, node.state))
node = node.parent
path.reverse()
return path
defmemory_usage(memory):
"""Function for estimating the memory usage of a dictionary"""
return sum(memory.values())
Output:
A*
013289
Memory Bounded A*
48
48
98*
SyntaxError: incomplete input
9*
SyntaxError: incomplete input
8
8
87/
SyntaxError: incomplete input
7
7
8
8
Result:
Thus the program for implementing informed search algorithms like A* and memory-
bounded A* has verified successfully and output is verified.
XI. XII.
1. can be done by computing The aim of the Naïve Bayes algorithm
various performance metrics such is to classify a given set of data points
as accuracy, precision, recall, and into different classes based on the
F1 score. probability of each data point
belonging to a particular class. This
can be done by computing various performance
algorithm is based on the Bayes
metrics such as accuracy, precision
theorem, which states that the
probability of an event occurring given
the prior knowledge of another event
can be calculated using conditional
probability.
Algorithm:
Collect the dataset: The first step in
using Naïve Bayes is to collect a
dataset that contains a set of data
points and their corresponding classes.
Prepare the data: The next step
is to preprocess the data and
prepare it for the Naïve Bayes
algorithm. This involves
removing any unnecessary
features or attributes and
normalizing the data.
Compute the prior probabilities: The
prior probabilities of each class can be
computed by calculating the number
of data points belonging to each class
and dividing it by the total number of
data points.
Compute the likelihoods: The
likelihoods of each feature for each
class can be computed by calculating
the conditional probability of the
feature given the class. This involves
counting the number of data points in
each class that have the feature and
dividing it by the total number of
data points in that class.
Compute the posterior probabilities:
The posterior probabilities of each
class can be computed by multiplying
the prior probability of the class with
the product of the likelihoods of each
feature for that class.
Make predictions: Once the posterior
probabilities have been computed for
each class, the Naïve Bayes algorithm
can be used to make predictions by
selecting the class with the highest
probability.
Evaluate the model: The final
step is to evaluate the
performance of the Naïve Bayes
model. This can
The aim of the Naïve Bayes algorithm
is to classify a given set of data points
into different classes based on the
probability of each data point
belonging to a particular class. This
algorithmbased on the Bayes theorem,
which states that the probability of an
event occurring given the prior
knowledge of another event can be
calculated using conditional
probability.
IMPLEMENT NAÏVE
Program:
# Importing library
import math
import random
importcsv
# driver code
# prepare model
info = MeanAndStdDevForClass(train_data)
# test model
predictions = getPredictions(info, test_data)
accuracy = accuracy_rate(test_data, predictions)
print("Accuracy of your model is: ", accuracy)
Output:
Algorithm:
Algorithm:
1. Collecting and cleaning the data:
The first step in building a regression
model is to gather the data needed for
analysis and ensure that it is clean and
consistent. This may involve removing
missing values, outliers, and other
errors.
2. Exploring the data: Once the data is
cleaned, it is important to explore it to
gain an understanding of the
relationships between the input and
outcome variables. This may involve
calculating summary statistics, creating
visualizations, and testing for
correlations.
3. Choosing the algorithm: Based on
the nature of the problem and the
characteristics of the data, an
appropriate regression algorithm is
chosen.
4. Preprocessing the data: Before
applying the regression algorithm, it
may be necessary to preprocess the data
to ensure that it is in a suitable format.
This may involve standardizing or
normalizing the data, encoding
categorical variables, or applying
feature engineering techniques.
5. Training the model: The regression
model is trained on a subset of the data,
using an optimization algorithm to find
the values of the model parameters that
minimize the difference between the
predicted and actual values.
6. Evaluating the model: Once the
model is trained, it is evaluated using a
separate test dataset to determine its
accuracy and generalization
performance. Metrics such as mean
squared error, R-squared, or root mean
squared error can be used to assess the
model's performance.
7. Improving the model: Based on the
evaluation results, the model can be
refined by adjusting the model
parameters or using different
algorithms.
8. Deploying the model: Finally, the
model can be deployed to make
predictions on new data.
Program:
import pandas as pd
importnumpy as np
importmatplotlib.pyplot as plt
fromsklearn.linear_model import LinearRegression
fromsklearn.model_selection import train_test_split
fromsklearn.metrics import mean_squared_error, r2_score
plt.xticks(())
plt.yticks(())
plt.show()
Out Put:
Algorithm:
Decision Trees.
1. Select the feature that best splits the data: The first step is to select the feature that
best separates the data into groups with different target values.
2. Recursively split the data: For each group created in step 1, repeat the process of
selecting the best feature to split the data until a stopping criterion is met. The stopping
criterion may be a maximum tree depth, a minimum number of samples in a leaf node, or
another condition.
3. Assign a prediction value to each leaf node: Once the tree is built, assign a prediction
value to each leaf node. This value may be the mean or median target value of the samples in
the leaf node.
4.
Random Forest
1. Randomly select a subset of features: Before building each decision tree, randomly
select a subset of features to consider for splitting the data.
2. Build multiple decision trees: Build multiple decision trees using the process
described above, each with a different subset of features.
3. Aggregate the predictions: When making predictions on new data, aggregate the
predictions from all decision trees to obtain a final prediction value. This can be done by
taking the average or majority vote of the predictions.
Program:
import pandas as pd
fromsklearn.tree import DecisionTreeRegressor
fromsklearn.ensemble import RandomForestRegressor
fromsklearn.model_selection import train_test_split
fromsklearn.metrics import mean_squared_error
# Load data
data = pd.read_csv('data.csv')
# Evaluate performance
mse = mean_squared_error(y_test, y_pred)
print(f"Decision Tree Mean Squared Error: {mse:.4f}")
# Evaluate performance
mse = mean_squared_error(y_test, y_pred)
print(f"Random Forest Mean Squared Error: {mse:.4f}")
Output:
Accuracy: 0.9777777777777777
Result:
Thus the program for Build SVM Model has been executed successfully and output is
verified.
XVII.IMPLEMENT
Aim: ENSEMBLING TECHNIQUES
Algorithm:
1. Load the dataset and split it into training and testing sets.
2. Choose the base models to be included in the ensemble.
3. Train each base model on the training set.
4. Combine the predictions of the base models using the chosen ensembling technique
(voting, bagging, boosting, etc.).
5. Evaluate the performance of the ensemble model on the testing set.
6. If the performance is satisfactory, deploy the ensemble model for making predictions
on new data.
Program:
# import required libraries
fromsklearn import datasets
fromsklearn.model_selection import train_test_split
fromsklearn.ensemble import RandomForestClassifier, VotingClassifier
fromsklearn.svm import SVC
fromsklearn.linear_model import LogisticRegression
Algorithm:
1. Data preparation: The first step is to prepare the data that we want to cluster. This
may involve data cleaning, normalization, and feature extraction, depending on the type and
quality of the data.
2. Choosing a distance metric: The next step is to choose a distance metric or similarity
measure that will be used to determine the similarity between data points. Common distance
metrics include Euclidean distance, Manhattan distance, and cosine similarity.
3. Choosing a clustering algorithm: There are many clustering algorithms available, each
with its own strengths and weaknesses. Some popular clustering algorithms include K-
Means, Hierarchical clustering, and DBSCAN.
4. Choosing the number of clusters: Depending on the clustering algorithm chosen, we
may need to specify the number of clusters we want to form. This can be done using
domain knowledge or by using techniques such as the elbow method or silhouette analysis.
5. Cluster assignment: Once the clusters have been formed, we need to assign each data
point to its nearest cluster based on the chosen distance metric.
6. Interpretation and evaluation: Finally, we need to interpret and evaluate the results of
the clustering algorithm to determine if the clustering has produced meaningful and useful
insights.
Program:
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans, AgglomerativeClustering
import matplotlib.pyplot as plt
Algorithm:
1. Initialize the parameters: Start by initializing the parameters of the Bayesian network,
such as the CPDs for each node. These can be initialized randomly or using some prior
knowledge.
2. E-step: In the E-step, we estimate the expected sufficient statistics for the unobserved
variables in the network, given the observed data and the current parameter estimates. This
involves computing the posterior probability distribution over the hidden variables, given the
observed data and the current parameter estimates.
3. M-step: In the M-step, we maximize the expected log-likelihood of the observed data
with respect to the parameters. This involves updating the parameter estimates using the
expected sufficient statistics computed in the E-step.
4. Repeat steps 2 and 3 until convergence: Iterate between the E-step and M-step until
the parameter estimates converge, or some other stopping criterion is met.
Program:
from pgmpy.models import BayesianModel
from pgmpy.estimators import MaximumLikelihoodEstimator
from pgmpy.inference import VariableElimination
from pgmpy.factors.discrete import TabularCPD
import numpy as np
# Create a Maximum Likelihood Estimator and fit the model to some data
data = np.random.randint(low=0, high=2, size=(5000, 2))
mle = MaximumLikelihoodEstimator(model, data)
model_fit = mle.fit()
Algorithm:
1. Data preparation: Preprocess the data to make it suitable for training the NN. This
may involve normalizing the input data, splitting the data into training and validation sets,
and encoding the output variables if necessary.
2. Define the architecture: Choose the number of layers and neurons in the NN, and
define the activation functions for each layer. The input layer should have one neuron per
input feature, and the output layer should have one neuron per output variable.
3. Initialize the weights: Initialize the weights of the NN randomly, using a small value
to avoid saturating the activation functions.
4. Forward propagation: Feed the input data forward through the NN, applying the
activation functions at each layer, and compute the output of the NN.
5. Compute the loss: Calculate the error between the predicted output and the true
output, using a suitable loss function such as mean squared error or cross-entropy.
6. Backward propagation: Compute the gradient of the loss with respect to the weights,
using the chain rule and backpropagate the error through the NN to adjust the weights.
7. Update the weights: Adjust the weights using an optimization algorithm such as
stochastic gradient descent or Adam, and repeat steps 4-7 for a fixed number of epochs or
until the performance on the validation set stops improving.
8. Evaluate the model: Test the performance of the model on a held-out test set and
report the accuracy or other performance metrics.
Program:
import tensorflow as tf
from tensorflow import keras
Epoch 1/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.2616 -
accuracy: 0.9250 - val_loss: 0.1422 - val_accuracy: 0.9571
Epoch 2/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.1159 -
accuracy: 0.9661 - val_loss: 0.1051 - val_accuracy: 0.9684
Epoch 3/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0791 -
accuracy: 0.9770 - val_loss: 0.0831 - val_accuracy: 0.9741
Epoch 4/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0590 -
accuracy: 0.9826 - val_loss: 0.0807 - val_accuracy: 0.9754
Epoch 5/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0462 -
accuracy: 0.9862 - val_loss: 0.0751 - val_accuracy: 0.9774
Epoch 6/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0366 -
accuracy: 0.9892 - val_loss: 0.0742 - val_accuracy: 0.9778
Epoch 7/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0288 -
accuracy: 0.9916 - val_loss: 0.0726 - val_accuracy: 0.9785
Epoch 8/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0240 -
accuracy: 0.9931 - val_loss: 0.0816 - val_accuracy: 0.9766
Epoch 9/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0192 -
accuracy: 0.9948 - val_loss: 0.0747 - val_accuracy: 0.9783
Epoch 10/10
1875/187
Result:
Thus the program is executed successfully and output is verified.
Aim:
XXI. BUILD DEEP LEARNING NN MODELS
The aim of building deep learning neural network (NN) models is to create a more complex
architecture that can learn hierarchical representations of data, allowing for more accurate
predictions and better generalization to new data. Deep learning models are typically
characterized by having many layers and a large number of parameters.
Algorithm:
1. Data preparation: Preprocess the data to make it suitable for training the NN. This
may involve normalizing the input data, splitting the data into training and validation sets,
and encoding the output variables if necessary.
2. Define the architecture: Choose the number of layers and neurons in the NN, and
define the activation functions for each layer. Deep learning models typically use activation
functions such as ReLU or variants thereof, and often incorporate dropout or other
regularization techniques to prevent overfitting.
3. Initialize the weights: Initialize the weights of the NN randomly, using a small value
to avoid saturating the activation functions.
4. Forward propagation: Feed the input data forward through the NN, applying the
activation functions at each layer, and compute the output of the NN.
5. Compute the loss: Calculate the error between the predicted output and the true
output, using a suitable loss function such as mean squared error or cross-entropy.
6. Backward propagation: Compute the gradient of the loss with respect to the weights,
using the chain rule and backpropagate the error through the NN to adjust the weights.
7. Update the weights: Adjust the weights using an optimization algorithm such as
stochastic gradient descent or Adam, and repeat steps 4-7 for a fixed number of epochs or
until the performance on the validation set stops improving.
8. Evaluate the model: Test the performance of the model on a held-out test set and
report the accuracy or other performance metrics.
9. Fine-tune the model: If necessary, fine-tune the model by adjusting the
hyperparameters or experimenting with different architectures.
Program:
import tensorflow as tf
from tensorflow import keras
Epoch 1/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.2921 -
accuracy: 0.9148 - val_loss: 0.1429 - val_accuracy: 0.9562
Epoch 2/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.1417 -
accuracy: 0.9577 - val_loss: 0.1037 - val_accuracy: 0.9695
Epoch 3/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.1066 -
accuracy: 0.9676 - val_loss: 0.0877 - val_accuracy: 0.9724
Epoch 4/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0855 -
accuracy: 0.9730 - val_loss: 0.0826 - val_accuracy: 0.9745
Epoch 5/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0732 -
accuracy: 0.9772 - val_loss: 0.0764 - val_accuracy: 0.9766
Epoch 6/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0635 -
accuracy: 0.9795 - val_loss: 0.0722 - val_accuracy: 0.9778
Epoch 7/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0551 -
accuracy: 0.9819 - val_loss: 0.0733 - val_accuracy: 0.9781
Epoch 8/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0504 -
accuracy: 0.9829 - val_loss: 0.0714 - val_accuracy: 0.9776
Epoch 9/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0460 -
accuracy: 0.9847 - val_loss: 0.0731 - val_accuracy:
Result:
Thus the program is executed successfully and output is verified.