AIML (2)
AIML (2)
bounded A*)
3. Implement naive Bayes models
Date:
Aim:
To write a Python program to implement Breadth First Search (BFS).
Algorithm:
Step 1. Start
Step 2. Put any one of the graph’s vertices at the back of the queue.
Step 3. Take the front item of the queue and add it to the visited list.
Step 4. Create a list of that vertex's adjacent nodes. Add those which are not within the
visited list to the rear of the queue.
Step 5. Continue steps 3 and 4 till the queue is empty.
Step 6. Stop
Program:
graph = {
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'8' : []
}
# Driver Code
print("Following is the Breadth-First Search")
bfs(visited, graph, '5') # function calling
OUTPUT:
Result:
Thus the Python program to implement Breadth First Search (BFS) was developed
successfully.
Ex. No.1.B UNINFORMED SEARCH ALGORITHM - DFS
Date:
Aim:
To write a Python program to implement Depth First Search (DFS).
Algorithm:
Step 1.Start
Step 2.Put any one of the graph's vertex on top of the stack.
Step 3.After that take the top item of the stack and add it to the visited list of the vertex.
Step 4.Next, create a list of that adjacent node of the vertex. Add the ones which aren't in the
visited list of vertexes to the top of the stack.
Step 5.Repeat steps 3 and 4 until the stack is empty.
Step 6.Stop
Program:
graph = {
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'8' : []
}
# Driver Code
print("Following is the Depth-First Search")
dfs(visited, graph, '5')
OUTPUT:
Result:
Thus the Python program to implement Depth First Search (DFS) was developed
successfully.
Ex. No.2. A INFORMED SEARCH ALGORITHM
Date: A* SEARCH
Aim:
To write a Python program to implement A* search algorithm.
Algorithm:
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.
c) Generate all successor nodes of the current node.
d) 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.
Program:
from queue import PriorityQueue
v = 14
graph = [[] for i in range(v)]
for v, c in graph[u]:
if not visited[v]:
visited[v] = True
pq.put((heuristic(v, target), v))
print()
# The nodes shown in above example(by alphabets) are # implemented using integers addedge(x,y,cost);
addedge(0, 1, 3)
addedge(0, 2, 6)
addedge(0, 3, 5)
addedge(1, 4, 9)
addedge(1, 5, 8)
addedge(2, 6, 12)
addedge(2, 7, 14)
addedge(3, 8, 7)
addedge(8, 9, 5)
addedge(8, 10, 6)
addedge(9, 11, 1)
addedge(9, 12, 10)
addedge(9, 13, 2)
source = 0
target = 9
best_first_search(source, target, v)
OUTPUT:
0389
Result:
Thus the python program for A* Search was developed and the output was verifiedsuccessfully.
Ex. No.2.B INFORMED SEARCH ALGORITHM
Date: MEMORY-BOUNDED A*
Aim:
To write a Python program to implement memory- bounded A* search algorithm.
Algorithm:
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:
import numpy as np
import heapq
class Graph:
def init (self, adjacency_matrix):
self.adjacency_matrix = adjacency_matrix
self.num_nodes = len(adjacency_matrix)
while priority_queue:
memory_usage = max(memory_usage, len(visited))
if memory_usage > memory_limit:
print("Memory limit exceeded!")
return None
if current_node == goal:
print("Goal found!")
return cost
graph = Graph(adjacency_matrix)
start_node = 0
goal_node = 6
memory_limit = 10 # Set the memory limit
OUTPUT:
Goal found!
Shortest path cost: 3
Result:
Thus the python program for memory-bounded A* search was developed and the
output was verified successfully.
Ex. No.3 NAIVE BAYES MODEL
Date:
Aim:
To write a python program to implement Naïve Bayes model.
Algorithm:
Step 1. Load the libraries: import the required libraries such as pandas, numpy, and
sklearn.
Step 2. Load the data into a pandas dataframe.
Step 3. Clean and preprocess the data as necessary. For example, you can handle missing
values, convert categorical variables into numerical variables, and normalize the
data.
Step 4. Split the data into training and test sets using the train_test_split function from
scikit-learn.
Step 5. Train the Gaussian Naive Bayes model using the training data.
Step 6. Evaluate the performance of the model using the test data and the accuracy_score
function from scikit-learn.
Step 7. Finally, you can use the trained model to make predictions on new data.
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
OUTPUT:
Accuracy: 0.9777777777777777
Result:
Thus the Python program for implementing Naïve Bayes model was developed and
the output was verified successfully.
Ex. No.4 BAYESIAN NETWORKS
Date:
Aim:
To write a python program to implement a Bayesian network for the Monty Hall
problem.
Algorithm:
Program:
import pandas as pd
from pgmpy.estimators import MaximumLikelihoodEstimator
from pgmpy.models import BayesianNetwork
from pgmpy.inference import VariableElimination
data = pd.read_csv("E:/dataset/heart.csv.csv")
heart_disease = pd.DataFrame(data)
print(heart_disease)
model = BayesianNetwork([
('age', 'Lifestyle'),
('Gender', 'Lifestyle'),
('Family', 'heartdisease'),
('diet', 'cholestrol'),
('Lifestyle', 'diet'),
('cholestrol', 'heartdisease'),
('diet', 'cholestrol')
])
model.fit(heart_disease, estimator=MaximumLikelihoodEstimator)
HeartDisease_infer = VariableElimination(model)
q = HeartDisease_infer.query(variables=['heartdisease'], evidence={
'age': int(input('Enter Age: ')),
'Gender': int(input('Enter Gender: ')),
'Family': int(input('Enter Family History: ')),
'diet': int(input('Enter Diet: ')),
'Lifestyle': int(input('Enter Lifestyle: ')),
'cholestrol': int(input('Enter Cholestrol: '))
})
print(q)
Output:
Result:
Thus, the Python program for implementing Bayesian Networks was successfully
developed and the output was verified.
Ex. No. 5
REGRESSION MODEL
Date:
Aim:
To write a Python program to build Regression models
Algorithm:
Program:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
OUTPUT:
Result:
Thus the Python program to build a simple linear Regression model was developed
successfully.
Ex. No. 6 DECISION TREE AND RANDOM FOREST
Date:
Aim:
To write a Python program to build decision tree and random forest.
Algorithm:
Program:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.ensemble import RandomForestClassifier
# calculate and print the accuracy of decision tree and random forest
print("Accuracy of decision tree: {:.2f}".format(tree.score(X_test, y_test)))
print("Accuracy of random forest: {:.2f}".format(rf.score(X_test, y_test)))
Sample flowers.csv
Sepal_length,Sepal_width,Petal_length,Petal_width,Flower
4.6,3.2,1.4,0.2,Rose
5.3,3.7,1.5,0.2,Rose
5,3.3,1.4,0.2,Rose
7,3.2,4.7,1.4,Jasmin
6.4,3.2,4.5,1.5,Jasmin
7.1,3,5.9,2.1,Lotus
6.3,2.9,5.6,1.8,Lotus
Output:
Result:
Thus the Python program to build decision tree and random forest was developed
successfully.
Ex. No.7 SVM MODELS
Date:
Aim:
To write a Python program to build SVM model.
Algorithm:
Step 1.Import the necessary libraries (matplotlib.pyplot, numpy, and svm from sklearn).
Step 2.Define the features (X) and labels (y) for the fruit dataset.
Step 3.Create an SVM classifier with a linear kernel using svm.SVC(kernel='linear').
Step 4.Train the classifier on the fruit data using clf.fit(X, y).
Step 5.Plot the fruits and decision boundary using plt.scatter(X[:, 0], X[:, 1], c=colors),
where colors is a list of colors assigned to each fruit based on its label.
Step 6.Create a meshgrid to evaluate the decision function using
np.meshgrid(np.linspace(xlim[0], xlim[1], 100), np.linspace(ylim[0], ylim[1], 100)).
Step 7.Use the decision function to create a contour plot of the decision boundary and
margins using ax.contour(xx, yy, Z, colors='k', levels=[-1, 0, 1], alpha=0.5,
linestyles=['--', '-', '--']).
Step 8.Show the plot using plt.show().
Program:
Output:
Result:
Thus, the Python program to build an SVM model was developed, and the output was
successfully verified.
IMPLEMENT ENSEMBLING TECHNIQUES
Ex. No.8
Date:
Aim:
The aim of ensembling is to combine the predictions of multiple individual
models, known as base models, in order to produce a final prediction that is more
accurate and reliable than any individual model. (Voting,Bagging,Boosting)
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:
#printensemble modelaccuracy
print("EnsembleAccuracy:",ensemble.score(X_test,y_test))
EnsembleAccuracy: 0.9333333333333333
Result:
Date:
Aim:
The aim of clustering is to find patterns and structure in data that may not be
immediately apparent, and to discover relationships and associations between data points
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
KMeans, 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:
Output:
Result:
Date:
Aim:
The aim of implementing EM for Bayesian networks is to learn the parameters of
the network from incomplete or noisy data. This involves estimating the conditional
probability distributions (CPDs) for each node in the network given the observed data.
The EM algorithm is particularly useful when some of the variables are hidden or
unobserved, as it can estimate the likelihood of the hidden variables based on the
observed data.
Algorithm:
1. Initialization: Initialize the parameters of the Bayesian network. This could involves randomly
initializing the CPDs or using some other initialization strategy.
2. Expectation(E): Given the current parameter estimates, compute the expected values of the hidden
variables using the observed data.
3. Maximization(M): Update the parameter estimates based on the observed data and the estimated
values of the hidden variables.\
4. Convergence Check: Repeat the E and M steps until the parameters converge or until a maximum
number of iterations is reached.
Program:
import numpy as np
# True parameters
true_prob_A = 0.7
true_prob_B_A = np.array([[0.9, 0.1],
[0.2, 0.8]])
# EM Algorithm
def expectation_step(prob_A, prob_B_A, observed_A):
# Compute posterior probabilities of B given A
posterior_B_A = prob_B_A[:, observed_A]
return posterior_B_A
def maximization_step(observed_A, posterior_B_A):
# Update parameter estimates
prob_A = np.mean(observed_A)
prob_B_A = np.dot(posterior_B_A, observed_A.T) / np.sum(posterior_B_A, axis=1, keepdims=True)
return prob_A, prob_B_A
# Initialize parameters
prob_A = 0.5
prob_B_A = np.array([[0.5, 0.5],
[0.5, 0.5]])
# EM iterations
max_iterations = 100
tolerance = 1e-6
for i in range(max_iterations):
# E-step
posterior_B_A = expectation_step(prob_A, prob_B_A, observed_A)
# M-step
new_prob_A, new_prob_B_A = maximization_step(observed_A, posterior_B_A)
# Check for convergence
if np.abs(new_prob_A - prob_A) < tolerance and np.all(np.abs(new_prob_B_A - prob_B_A) < tolerance):
print(f"Converged after {i+1} iterations.")
break
# Update parameters
prob_A = new_prob_A
prob_B_A = new_prob_B_A
print("Estimated parameters:")
print("P(A):", prob_A)
print("P(B|A):", prob_B_A)
Output:
Result:
Date:
Aim:
The aim of building simple neural network (NN) models is to create a basic architecture that can
learn patterns from data and make predictions based on the input. This can involve defining the
structure of the NN, selecting appropriate activation functions, and tuning the hyperparameters to
optimize the performance of the model.
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
Output:
Epoch 1/10
1875/1875 [==============================] - 3s 1ms/step - loss: 0.2949 - accuracy: 0.91
36 - val_loss: 0.1438 - val_accuracy: 0.9571
Epoch 2/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.1455 - accuracy: 0.95
73 - val_loss: 0.0998 - val_accuracy: 0.9701
Epoch 3/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.1089 - accuracy: 0.96
75 - val_loss: 0.0860 - val_accuracy: 0.9728
Epoch 4/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0883 - accuracy: 0.97
25 - val_loss: 0.0785 - val_accuracy: 0.9760
Epoch 5/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0751 - accuracy: 0.97
67 - val_loss: 0.0729 - val_accuracy: 0.9785
Epoch 6/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0654 - accuracy: 0.97
97 - val_loss: 0.0675 - val_accuracy: 0.9788
Epoch 7/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0577 - accuracy: 0.98
16 - val_loss: 0.0676 - val_accuracy: 0.9792
Epoch 8/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0548 - accuracy: 0.98
16 - val_loss: 0.0640 - val_accuracy: 0.9813
Epoch 9/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0475 - accuracy: 0.98
44 - val_loss: 0.0679 - val_accuracy: 0.9794
Epoch 10/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0449 - accuracy: 0.98
51 - val_loss: 0.0714 - val_accuracy: 0.9803
313/313 - 0s - loss: 0.0714 - accuracy: 0.9803 - 250ms/epoch - 799us/step
Test accuracy: 0.9803000092506409
Result:
Thus the program is executed successfully and output is verified.
Ex.No. 12 BUILD DEEP LEARNING NN MODELS
Date:
Aim:
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
# Load the MNIST dataset
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
Result:
Thus the program is executed successfully and output is verified.