Ai&ml Record (22-23)
Ai&ml Record (22-23)
(Accredited by NBA & with ‘A’ grade by NAAC and ISO 9001:2008 Certified Institution)
(Regd. Under Sec 2(F) & 12(B) of the UGC Act 1956)
(Regulation - 2021)
CAHECT
INDEX
S.NO DATE LIST OF EXERCISES PAGE SIGN
NUMBER
1 Implementation of Uninformed
Search Algorithms(BFS,DFS)
CAHECT
CAHECT
EX.NO:1A Implementation of Uninformed search algorithms
DATE: (BFS)
AIM :
BFS Algorithm:
1. Pick any node, visit the adjacent unvisited vertex, mark it as visited, display it,
and insert it in a queue.
2. If there are no remaining adjacent vertices left, remove the first vertex from the
queue.
3. Repeat step 1 and step 2 until the queue is empty or the desired node is found.
Implementation:
Source Code:
graph = {
'A':['B','C'],
'B':['D','E'],
'C':['F'],
'D':[],
'E':['F'],
'F':[],
CAHECT
}
visited = []
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)
bfs(visited,graph,'A')
Output:
Result:
CAHECT
EX.NO:1B Implementation of Uninformed search algorithms
DATE: (DFS)
AIM :
DFS Algorithm:
1. We will start by putting any one of the graph's vertex on top of the stack.
2. After that take the top item of the stack and add it to the visited list of the vertex.
3. 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.
4. Lastly, keep repeating steps 2 and 3 until the stack is empty.
Implementation:
Source code:
# Using a Python dictionary to act as an adjacency list
graph ={
'5':['3','7'],
'3':['2','4'],
'7':['8'],
'2':[],
CAHECT
'4':['8'],
'8':[],
}
# Driver code
print("following is the depth -first search")
dfs(visited,graph,'5')
Output:
Result:
CAHECT
EX.NO:2 Implementation of A*, Memory-bounded A*
DATE:
AIM:
Algorithm:
Step 2: Check if the OPEN list is empty or not, if the list is empty then return failure and
stops.
Step 3: Select the node from the OPEN list which has the smallest value of evaluation
function (g+h), if node n is goal node then return success and stop, otherwise
Step 4: Expand node n and generate all of its successors, and put n into the closed list.
For each successor n', check whether n' is already in the OPEN or CLOSED list, if not
then compute evaluation function for n' and place into Open list.
Step 5: Else if node n' is already in OPEN and CLOSED, then it should be attached to the
back pointer which reflects the lowest g(n') value.
class Graph:
def __init__(self, adjac_lis):
self.adjac_lis = adjac_lis
}
return H[n]
CAHECT
def a_star_algorithm(self, start, stop):
open_lst = set([start])
closed_lst = set([])
poo = {}
poo[start] = 0
par = {}
par[start] = start
if n == None:
print('Path does not exist!')
return None
if n == stop:
reconst_path = []
while par[n] != n:
reconst_path.append(n)
n = par[n]
reconst_path.append(start)
reconst_path.reverse()
print('Path found: {}'.format(reconst_path))
return reconst_path
for (m, weight) in self.get_neighbors(n):
if m not in open_lst and m not in closed_lst:
open_lst.add(m)
par[m] = n
poo[m] = poo[n] + weight
else:
if poo[m] > poo[n] + weight:
poo[m] = poo[n] + weight
par[m] = n
if m in closed_lst:
closed_lst.remove(m)
open_lst.add(m)
open_lst.remove(n)
closed_lst.add(n)
adjac_lis = {
CAHECT
'A': [('B', 1), ('C', 3), ('D', 7)],
'B': [('D', 5)],
'C': [('D', 12)]
}
graph1 = Graph(adjac_lis)
graph1.a_star_algorithm('A', 'D')
Output:
for v, c in graph[u]:
if visited[v] == False:
visited[v] = True
CAHECT
pq.put((c, v))
print()
# Function for adding edges to graph
def addedge(x, y, cost):
graph[x].append((y, cost))
graph[y].append((x, cost))
# 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)
Result:
CAHECT
EX.NO: 3 Implementation of Naive Bayes Models (BFS)
DATE:
AIM:
Algorithm:
Step1: Data Pre-processing step.
Step 2: Fitting Naive Bayes to the Training set.
Step 3: Predicting the test result.
Step 4 : Test accuracy of the result(Creation of Confusion matrix).
Step 5: Visualizing the test set result.
Source Code:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
dataset =pd.read_csv("C:/Users/Banu/Downloads/iris1.csv")
dataset.head(5)
#splitting the data
X = dataset.iloc[:,:4].values
Y = dataset['variety'].values
#to see the split
print(X)
print(Y)
#testing and training
from sklearn.model_selection import train_test_split
CAHECT
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.2)
#feature scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
#Naive Bayes
from sklearn.naive_bayes import GaussianNB
classifier = GaussianNB()
classifier.fit(X_train, y_train)
#prediction
y_pred = classifier.predict(X_test)
y_pred
#prediction
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
print(cm)
from sklearn.metrics import accuracy_score
print ("Accuracy : ", accuracy_score(y_test, y_pred))
df = pd.DataFrame({"Real Values" : y_test , "Predicted Values" : y_pred})
print(df.head(5))
Output:
CAHECT
Result:
CAHECT
EX.NO:4
DATE: Implement Bayesian Networks
AIM:
To write a program to implement the naïve Bayesian classifier for a data set.
Algorithm:
1. First, identify which are the main variable in the problem to solve. Each variable
corresponds to a node of the network. It is important to choose the number states for
each variable, for instance, there are usually two states (true or false).
2. Second, define structure of the network, that is, the causal relationships between all
the variables (nodes).
3. Third, define the probability rules governing the relationships between the variables.
Source code:
from pgmpy.models import BayesianNetwork
from pgmpy.factors.discrete import TabularCPD
import networkx as nx
import pylab as plt
CAHECT
infer = VariableElimination(model)
posterior_p = infer.query(['Host'], evidence={'Guest': 2, 'Price': 2})
print(posterior_p)
pos = nx.circular_layout(model)
nx.draw(model, node_color='#00b4d9', pos=pos, with_labels=True)
plt.savefig('model.png')
Output:
True
Result:
Thus the program executed successfully
CAHECT
EX.NO:5 Build Regression models
DATE:
AIM:
To write a program to Build Regression models using python.
Algorithm:
Source code:
import numpy as np
import matplotlib.pyplot as plt
CAHECT
b_1 = SS_xy / SS_xx
b_0 = m_y - b_1 * m_x
# putting labels
plt.xlabel('x')
plt.ylabel('y')
def main():
# observations / data
x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
y = np.array([1, 3, 2, 5, 7, 8, 8, 9, 10, 12])
# estimating coefficients
b = estimate_coef(x, y)
print("Estimated coefficients:\nb_0 = {} \
\nb_1 = {}".format(b[0], b[1]))
CAHECT
# plotting regression line
plot_regression_line(x, y, b)
if __name__ == "__main__":
main()
Output:
Result:
CAHECT
EX.NO:6A Build decision trees and random forests
DATE:
AIM:
To write a program to Build decision tree models using python.
ALGORITHM:
Step-1: Begin the tree with the root node, says S, which contains the complete dataset.
Step-2: Find the best attribute in the dataset using Attribute Selection Measure
(ASM).
Step-3: Divide the S into subsets that contains possible values for the best attributes.
Step-4: Generate the decision tree node, which contains the best attribute.
Step-5: Recursively make new decision trees using the subsets of the dataset created in
step -3. Continue this process until a stage is reached where you cannot further classify
the nodes and called the final node as a leaf node.
Source code:
# Here the array contains three values which are height,weight and shoe size
X = [[181, 80, 91], [182, 90, 92], [183, 100, 92], [184, 200, 93], [185, 300, 94],
[186, 400, 95],
[187, 500, 96], [189, 600, 97], [190, 700, 98], [191, 800, 99], [192, 900, 100],
[193, 1000, 101]]
Y = ['male', 'male', 'female', 'male', 'female', 'male', 'female', 'male', 'female',
'male', 'female',
'male']
clf = clf.fit(X, Y)
CAHECT
# Predicting on basis of given random values for each given feature
predictionf = clf.predict([[181, 80, 91]])
predictionm = clf.predict([[183, 100, 92]])
Output:
Result:
CAHECT
EX.NO:6B Build Random Forest Model
DATE:
AIM:
To write a program to random forest models using python
Algorithm:
Step-2: Build the decision trees associated with the selected data points (Subsets).
Step-3: Choose the number N for decision trees that you want to build.
Source code:
import numpy as np
dataset = pd.read_csv("Boston1.csv")
dataset.head()
x = pd.DataFrame(dataset.iloc[:,:-1])
x
y = pd.DataFrame(dataset.iloc[:,-1])
y
CAHECT
print('mean Absolute error:',metrics.mean_absolute_error(y_test,y_pred))
print('mean squared error:',metrics.mean_squared_error(y_test,y_pred))
print('Root mean Squared
error:',np.sqrt(metrics.mean_squared_error(y_test,y_pred)))
Output:
Result:
CAHECT
EX.NO:7
DATE: Build SVM Models
AIM:
Algorithm:
Source code:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.read_csv('UniversalBank (1).csv')
df.head(5)
df.drop('ZIP Code',inplace=True,axis=1)
df.head(5)
x = df.drop('Personal Loan',axis=1)
x.drop('ID',axis=1,inplace=True)
x.head(5)
y = df['Personal Loan']
CAHECT
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state =
0)
from sklearn.svm import LinearSVC
svc = LinearSVC()
svc.fit(x_train,y_train)
pred = svc.predict(x_test)
from sklearn.metrics import confusion_matrix, classification_report
import seaborn as sns
print(classification_report(y_test,pred))
sns.heatmap(confusion_matrix(y_test,pred),annot=True)
Output:
Result:
CAHECT
EX.NO:8
DATE: Implement Ensembling techniques
AIM:
Algorithm:
1. importing the necessary packages which are required to build our bagging model
2. Download the dataset from the GitHub link provided below. Then we store the link in a
variable called url
3. we set the seed value for all the random states, so that the generated random values shall
remain the same until the seed value is exhausted, We now declare the KFold model
wherein we set the n_splits parameter to 10, and the random_state parameter to the seed
value.
4. we train the model using the BaggingClassifier by declaring the parameters to the
values defined in the previous step.
5. check the performance of the model by using the parameters Bagging model, X, Y,
and the kfold model.
6. The output printed will be the accuracy of the model
Source code:
import pandas
from sklearn import model_selection
from sklearn.ensemble import BaggingClassifier
from sklearn.tree import DecisionTreeClassifier
url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-
diabetes.data.csv"
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
dataframe = pandas.read_csv(url, names=names)
array = dataframe.values
X = array[:,0:8]
Y = array[:,8]
CAHECT
seed = 7
kFold = model_selection.KFold(n_splits=10, random_state=None)
cart = DecisionTreeClassifier()
num_trees = 100
model = BaggingClassifier(estimator=cart, n_estimators=num_trees, random_state=7)
results = model_selection.cross_val_score(model, X, Y, cv=kFold)
print(results.mean())
Output;
Result:
CAHECT
EX.NO:9 Implement clustering algorithms
DATE:
AIM:
To write a program to Implement clustering algorithms using python.
Algorithm:
2. we randomly select the centroid for each cluster. Let’s say we want to have 2 clusters,
so k is equal to 2 here.
3. Once we have initialized the centroids, we assign each point to the closest cluster
centroid.
4. once we have assigned all of the points to either cluster, the next step is to compute the
centroids of newly formed clusters
5. Repeat step 3& 4 until we have optimal centroids and the assignments of data points to
correct cluster are not changing anymore.
Source Code:
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
data = {
'x': [25, 34, 22, 27, 33, 33, 31, 22, 35, 34, 67, 54, 57, 43, 50, 57, 59, 52, 65, 47, 49,
48, 35, 33, 44, 45, 38,
43, 51, 46],
'y': [79, 51, 53, 78, 59, 74, 73, 57, 69, 75, 51, 32, 40, 47, 53, 36, 35, 58, 59, 50, 25,
20, 14, 12, 20, 5, 29, 27,
8, 7]
}
df = pd.DataFrame(data)
CAHECT
kmeans = KMeans(n_clusters=4).fit(df)
centroids = kmeans.cluster_centers_
print(centroids)
Output:
Result:
CAHECT
.
EX.NO:10 Implement EM for Bayesian networks
DATE:
AIM:
ALGORITHM:
1. Given a set of incomplete data, start with a set of initialized parameters.
2. Expectation step (E – step): In this expectation step, by using the observed
available data of the dataset, we can try to estimate or guess the values of the
missing data. Finally, after this step, we get complete data having no missing
values.
3. Maximization step (M – step): Now, we have to use the complete data, which
is prepared in the expectation step, and update the parameters.
4. Repeat step 2 and step 3 until we converge to our solution.
Source code:
import pandas as pd
import numpy as np
import pyAgrum as gum
import pyAgrum.lib.notebook as gnb
data = pd.read_csv("C:/Users/Banu/Downloads/asia10K.csv")
data = pd.DataFrame(data,columns=["Smoker","LungCancer","X-ray"])
test_data = data[:2000]
new_data = data[2000:].copy()
new_data["Smoker"][:500]= "?"
learner = gum.BNLearner(test_data)
learner.useScoreBIC()
learner.useGreedyHillClimbing()
bn=learner.learnBN()
learner2=gum.BNLearner(new_data,bn)
learner2.setVerbosity(True)
learner2.useEM(1e-10)
CAHECT
learner2.fitParameters(bn)
print(f"Nbr iteration EM : {learner2.nbrIterations()}")
import matplotlib.pyplot as plt
plt.plot(np.arange(1,1+learner2.nbrIterations()),learner2.history())
plt.semilogy()
plt.title("error during EM iterations")
Output:
Result:
CAHECT
EX.NO:11
DATE:
Build simple NN models
AIM:
Source code:
import numpy as np
class NeuralNetwork():
def __init__(self):
# seeding for random number generation
np.random.seed(1)
CAHECT
def sigmoid_derivative(self, x):
#computing derivative to the Sigmoid function
return x * (1 - x)
#training the model to make accurate predictions while adjusting weights continually
for iteration in range(training_iterations):
#siphon the training data via the neuron
output = self.think(training_inputs)
self.synaptic_weights += adjustments
inputs = inputs.astype(float)
output = self.sigmoid(np.dot(inputs, self.synaptic_weights))
return output
if __name__ == "__main__":
CAHECT
print("Beginning Randomly Generated Weights: ")
print(neural_network.synaptic_weights)
training_outputs = np.array([[0,1,1,0]]).T
CAHECT
Output:
Result:
CAHECT
EX.NO:12
DATE: Build deep learning NN models
AIM:
To write a program to Build deep learning NN models using python in Jupyter notebook.
Algorithm:
1. use the NumPy library to load your dataset and two classes from the Keras
library to define your model.
2. Models in Keras are defined as a sequence of layers,We create a Sequential
model and add layers one at a time .
3. Compiling the model uses the efficient numerical libraries under the covers (the so-
called backend) such as Theano or TensorFlow.
4 . To execute the model on some data. We can train or fit your model on your
loaded data by calling the fit() function on the model.Training occurs over epochs,
and each epoch is split into batches.
5. After trained our neural network on the entire dataset, and we can evaluate the
performance of the network on the same dataset using the evaluate() function.
Source code:
from numpy import loadtxt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# load the dataset
dataset = loadtxt('C:/Users/Banu/Downloads/pima-indians-diabetes.data.csv',
delimiter=',')
# split into input (X) and output (y) variables
X = dataset[:,0:8]
y = dataset[:,8]
# define the keras model
CAHECT
model = Sequential()
model.add(Dense(12, input_shape=(8,), activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# compile the keras model
model.compile(loss='binary_crossentropy', optimizer='adam',
metrics=['accuracy'])
# fit the keras model on the dataset
model.fit(X, y, epochs=150, batch_size=10)
# evaluate the keras model
_, accuracy = model.evaluate(X, y)
print('Accuracy: %.2f' % (accuracy*100))
Output:
Result:
CAHECT