CS3491-Ai&Ml Lab manual-CSE
CS3491-Ai&Ml Lab manual-CSE
Regulation-2021
Department of Computer science and
Engineering
CS3491 ARTIFICIAL INTELLIGENCE & MACHINE LEARNING LAB
Register No.
Name :
Semester : IV
Subject Name : ARTIFICIAL INTELLIGENCE & MACHINE -
LEARNING LAB
Subject Code : CS3491
UDAYA SCHOOL OF ENGINEERING
University Reg. No . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
University Examination held on . . . . . . . . . . . . . . . . . . . .
Aim:
To implement uninformed search algorithms such as BFS and DFS.
Algorithm (BFS):
Step 1: SET STATUS = 1 (ready state) for each node in G
Step 2: Enqueue the starting node A and set its STATUS = 2
(waiting state) Step 3: Repeat Steps 4 and 5 until QUEUE is
empty
Step 4: Dequeue a node N. Process it and set its STATUS = 3
(processed state).
Step 5: Enqueue all the neighbours of N that are in the ready state
(whose STATUS = 1) and set
their STATUS = 2
(waiting state)
[END OF LOOP]
Step 6: EXIT
Program:
graph = {
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'8' : []
}
visited = [] # List for visited nodes.
queue = [] #Initialize a queue
def bfs(visited, graph, node): #function for BFS
visited.append(node)
queue.append(node)
while queue: # Creating loop to visit each node
m = queue.pop(0)
print (m, end = " ")
for neighbour in graph[m]:
if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)
# Driver Code
print("Following is the Breadth-First Search")
bfs(visited, graph, '5') # function calling
Output:
Following is the Breadth-First Search
537248
Algorithm(DFS):
Step 1: SET STATUS = 1 (ready state) for each node in G
Step 2: Push the starting node A on the stack and set its STATUS = 2
(waiting state)
Step 3: Repeat Steps 4 and 5 until STACK is empty
Step 4: Pop the top node N. Process it and set its STATUS = 3
(processed state)
Step 5: Push on the stack all the neighbors of N that are in the ready
state (whose STATUS = 1) and set their STATUS = 2 (waiting
state)
[END OF LOOP]
Step 6: EXIT
Program:
# Using a Python dictionary to act as an adjacency list
graph = {
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'8' : []
}
visited = set() # Set to keep track of visited nodes of graph.
defdfs(visited, graph, node): #function for dfs
if node notin visited:
print (node)
visited.add(node)
forneighbourin graph[node]:
dfs(visited, graph, neighbour)
# Driver Code
print("Following is the Depth-First Search")
dfs(visited, graph, '5')
Output:
Following is the Depth-First Search
532487
Result:
Thus the uninformed search algorithms such as BFS and DFS have been executed
successfully and the output got verified.
Ex No: 2 IMPLEMENTATION OF INFORMED SEARCH ALGORITHMS (A*)
Aim:
Algorithm(A*):
return H_dist[n]
aStarAlgo('A', 'J')
Output:
Result:
Thus the program to implement informed search algorithm have been executed
successfully and output got verified.
Ex No: 3 IMPLEMENT NAÏVE BAYES MODELS
Aim:
To diagnose heart patients and predict disease using heart disease dataset with
Naïve Bayes Classifier Algorithm.
Algorithm:
Output:
Model Accuracy:
Accuracy: 0.9074074074074074
Result:
Thus the program to diagnose heart patients and predict disease using heart disease
dataset with Naïve Bayes Classifier Algorithm has been executed successfully and output got
verified.
Ex No: 4 IMPLEMENT BAYESIAN NETWORKS
Aim:
To construct a Bayesian network, to demonstrate the diagnosis of heart patients
using standard Heart Disease Data Set.
Algorithm:
Program
from pgmpy.models import BayesianNetwork
from pgmpy.factors.discrete import TabularCPD
import networkx as nx
import pylab as plt
# Defining Bayesian Structure
model = BayesianNetwork([('Guest', 'Host'), ('Price', 'Host')])
# Defining the CPDs:
cpd_guest = TabularCPD('Guest', 3, [[0.33], [0.33], [0.33]])
cpd_price = TabularCPD('Price', 3, [[0.33], [0.33], [0.33]])
cpd_host = TabularCPD('Host', 3, [[0, 0, 0, 0, 0.5, 1, 0, 1, 0.5],
[0.5, 0, 1, 0, 0, 0, 1, 0, 0.5],
[0.5, 1, 0, 1, 0.5, 0, 0, 0, 0]],
evidence=['Guest', 'Price'], evidence_card=[3, 3])
# Associating the CPDs with the network structure.
model.add_cpds(cpd_guest, cpd_price, cpd_host)
model.check_model()
Output:
True
Program
# Infering the posterior probability
from pgmpy.inference import VariableElimination
infer = VariableElimination(model)
posterior_p = infer.query(['Host'], evidence={'Guest': 2, 'Price': 2})
print(posterior_p)
Output:
+---------+-------------+
| Host | phi(Host) |
+=====+==== ===+
| Host(0) | 0.5000 |
+---------+-------------+
| Host(1) | 0.5000 |
+---------+-------------+
| Host(2) | 0.0000 |
+---------+-------------+
Program
nx.draw(model, with_labels=True)
plt.savefig('model.png')
plt.close()
Output:
Result:
Thus the program to implement a bayesian networks in the given heart disease dataset
have been executed successfully and the output got verified
Ex No: 5. BUILD REGRESSION MODELS
Aim:
To build regression models such as locally weighted linear regression and plot the
necessary graphs.
Algorithm:
1. Read the Given data Sample to X and the curve (linear or non-linear) to Y
2. Set the value for Smoothening parameter or free parameter say τ
3. Set the bias /Point of interest set x0 which is a subset of X
4. Determine the weight matrix using :
(𝑥−𝑥0 )2
𝑤(𝑥, 𝑥0 ) = 𝑒 − 2𝜏
5. Determine the value of model term parameter β using :
𝛽̂ (𝑥0 ) = (𝑋 𝑇 𝑊𝑋)−1 𝑋 𝑇 Wy
6. Prediction = x0*β.
Program:
residuals = y - yest
s = np.median(np.abs(residuals))
delta = np.clip(residuals / (6.0 * s), -1, 1)
delta = (1 - delta ** 2) ** 2
return yest
import math
n = 100
x = np.linspace(0, 2 * math.pi, n)
y = np.sin(x) + 0.3 * np.random.randn(n)
f =0.25
iterations=3
yest = lowess(x, y, f, iterations)
Result
Thus the program to implement build regression models have been executed successfully and the
output got verified
Ex No: 7 BUILD LOGISTIC REGRESSION MODELS
Aim:
Algorithm:
Output:
Result
Thus the program to implement BUILD logistic regression models have been executed
successfully and the output got verified
Ex No: 7 BUILD DECISION TREES
Aim :
To build decision trees with suitable datasets
Algorithm :
1. It begins with the original set S as the root node
2. On each iteration of the algorithm, it iterates through the very unused attribute of the set
S and calculates Entropy (H) and Information gain (IG) of this attribute.
3. It then selects the attribute which has the smallest entropy or largest information gain
4. The set S is then split by the selected attribute to produce a subset of the data.
5. The algorithm continues to recur on each subset, considering only attributes never
selected before
Program:
import pandas
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier
df = pandas.read_csv("data.csv")
print("Input:")
print(df.head(5))
d = {'UK':0,'USA':1,'N':2}
df['Nationality'] = df['Nationality'].map(d)
d = {'YES':1, 'NO':0}
df['Go'] = df['Go'].map(d)
print("Transformed Data:")
print(df.head(5))
features = ['Age','Experience','Rank','Nationality']
X = df[features]
y = df['Go']
dtree = DecisionTreeClassifier()
dtree = dtree.fit(X,y)
print(dtree.predict([[40,10,6,1]]))
print("[1]means 'Go'")
print("[0]means 'NO'")
DATA SET : (data.csv)
Age Experience Rank Nationality Go
36 10 9 UK NO
42 12 4 USA NO
23 4 6 N NO
52 4 4 USA NO
43 21 8 USA YES
Output:
Result
Thus the program to implement build decision trees have been executed successfully and
the output got verified
Ex No: 8 BUILD RANDOM FORESTS
Aim:
To implement the concept of random forest with suitable dataset from real world problems.
Algorithms:
1. State the questions and determine required data.
2. Acquire the data in an accessible format.
3. Identify and correct missing data points/anomalies as required.
4. Prepare the data for the machine learning model.
5. Establish a baseline model that you aim to exceed.
6. Train the model on the training data.
7. Make prediction on the test data.
8. Compare predictions to the known test set targets and calculate performance metrics.
9. If performance is not satisfactory, adjust the model,acquire more data,or try a different
modeling technique.
10. Interpret model and report results visually and numerically.
Program:
import numpy as np
# Labels are the values we want to predict
labels = np.array(features['actual'])
# Remove the labels from the features
# axis 1 refers to the columns
features= features.drop('actual', axis = 1)
# Saving feature names for later use
feature_list = list(features.columns)
# Convert to numpy array
features = np.array(features)
Output:
RandomForestRegressor(max_depth=3, n_estimators=10)
Accuracy: 93.73 %.
Result
Thus the program to implement build random forests have been executed successfully and
the output got verified
Ex no:9 BUILD SVM MODELS
Aim:
To create a machine learning model which classifies the given dataset using support vector
machine algorithm.
Algorithm:
1. First and foremost we will load appropriate SKlearn modules and classes.
2. Next step is to perform feature scaling. The reason for doing feature scaling is to make
sure that datafor different features are in the same range. The standardscaler class of
sklearn.preprocessing is used.
3. Next step is to instantiate a SVC and fit the model. The SVC classof sklearn.svm
module is used.
4. Finally,it is time to measure the model performance. Here is the ciode for doing the
same.
Program:
import pandas
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import confusion_matrix
data = pandas.read_csv("vector.csv")
print("Input: ")
print(data.head(10))
x_train = training_set.iloc[:,0:2].values
y_train = training_set.iloc[:,2].values
x_test = test_set.iloc[:,0:2].values
y_test = test_set.iloc[:,2].values
cm = confusion_matrix(y_test, y_pred)
accuracy = float(cm.diagonal().sum()/len(y_test))
print("\nAccuracy of SVM for the given dataset: ", accuracy)
Dataset
Output:
Result
Thus the program to implement build svm models have been executed successfully and the
output got verified
Ex No: 10 IMPLEMENT ENSEMBLING TECHNIQUES
Aim:
To implement ensembling techniques for voting classifier for bagging, adboost using python
in Jupiter environment.
Algorithm:
1. Split the train dataset into n parts.
2. A base modelis fitted on n-1 parts and predictions are made for the nth part. This is done
for each one of the n part of the train set.
3. The base model is then fitted on the whole train dataset.
4. This model is used to predict the test dataset.
5. The steps 2 to 4 are repeated for another base model which results in another set of
predictions for the train and test dataset.
6. The predictions on train dataset are used as a feature to build the new model.
7. This final model is used to make the predictions on test dataset.
Program:
#Implement VotingClassifier
#Importing necessary libraries:
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_moons
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import VotingClassifier
from sklearn.metrics import accuracy_score
#Creating dataset:
X, y = make_moons(n_samples=500, noise=0.30)
X_train, X_test, y_train, y_test = train_test_split(X, y)
#Implement BaggingClassifier
from sklearn.ensemble import BaggingClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
bagging_clf = BaggingClassifier(
DecisionTreeClassifier(), n_estimators=250,
max_samples=100, bootstrap=True, random_state=101)
#Fitting training data:
bagging_clf.fit(X_train, y_train)
#prediction using test data
y_pred = bagging_clf.predict(X_test)
print(accuracy_score(y_test, y_pred))
#Implement AdaBoostClassifier
from sklearn.ensemble import AdaBoostClassifier
adaboost_clf = AdaBoostClassifier(
DecisionTreeClassifier(max_depth=1), n_estimators=200,
algorithm="SAMME.R", learning_rate=0.5, random_state=42)
Output:
#For VotingClassifier
LogisticRegression 0.848
RandomForestClassifier 0.88
SVC 0.896
VotingClassifier 0.896
#For BaggingClassifier
0.888
#For AdaBoostClassifier
0.864
Result
Thus the program to implement ensembling techniques have been executed successfully
and the output got verified
Ex No: 11 IMPLEMENT CLUSTERING ALGORITHMS
Aim:
To implement k-nearest Neighbour algorithm to classify the Iris dataset.
Algorithm:
1. Select the number K of the neighbors.
2. Calaulate the Euclidean distance os K number of neighbors.
3. Take the K nearest neighbors as per the calculated Euclidean distance.
4. Among these k neighbors,count the number of the data points in each category.
5. Assign the new data points to that catedory for which the number of the neighbor is
maximum.
6. Our model is ready.
Program:
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,4
6],
'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]
}
Output:
Result
Thus the program to implement clustering algorithms have been executed successfully and
the output got verified
Ex No: 12 IMPLEMENT GMM ALGORITHMS
Aim:
To implement GMM algorithms for clustering networks using the given dataset.
Algorithm:
Initialize randomly Repeat until convergence.
E-step:
Compute q(h)=p(H=h|E=e;) for each h
Create fully-observed weighted examplesh,e)with weight q(h)
M-step:
Maximum likehood on weighted examples to get teta
Program:
import matplotlib.pyplot as plt
from sklearn import datasets
import sklearn.metrics as sm
import pandas as pd
import numpy as np
%matplotlib inline
# import some data to play with
iris = datasets.load_iris()
# GMM
from sklearn import preprocessing
scaler = preprocessing.StandardScaler()
scaler.fit(X)
xsa = scaler.transform(X)
xs = pd.DataFrame(xsa, columns = X.columns)
xs.sample(5)
from sklearn.mixture import GaussianMixture
gmm = GaussianMixture(n_components=3)
gmm.fit(xs)
y_cluster_gmm = gmm.predict(xs)
y_cluster_gmm
plt.subplot(1, 2, 1)
plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[y_cluster_gmm], s=40)
plt.title('GMM Classification')
# Accuracy
sm.accuracy_score(y, y_cluster_gmm)
# Confusion Matrix
sm.confusion_matrix(y, y_cluster_gmm)
Output:
array([[50, 0, 0],
[ 0, 5, 45],
[ 0, 50, 0]], dtype=int64)
Result
Thus the program to implement GMM algorithms have been executed successfully and the
output got verified
Ex No: 13 BUILD A DEEP LEARNING NEURAL NETWORKS MODELS
Aim:
To implement and build a convolutional neural network model which predicts the age and
gender of a person using the given pre-trained models.
Algorithm:
1. Choose the dataset.
2. Prepare the dataset for training.
3. Create training data.
4. Shuffle the dataset.
5. Assigning Labels and features.
6. Normalizing X and converting labels to categorical data.
7. Split X and Y for use in CNN.
8. Define, compile and train the CNN model.
9. Accuracy and scope of the model.
Program:
import tensorflow as tf
from tensorflow import keras
fashiondata=tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test)=fashiondata.load_data()
x_test.shape
x_train.shape
x_train, x_test=x_train/255, x_test/255
model = tf.keras.models.Sequential([tf.keras.layers.Flatten(input_shape=(28,28)),
tf.keras.layers.Dense(128,activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10,activation='softmax')])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy',metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test)
Output:
(10000, 28, 28)
(60000, 28, 28)
Epoch 1/5
1875/1875 [==============================] - 7s 3ms/step - loss: 0.0672 - accuracy:
0.9793
Epoch 2/5
1875/1875 [==============================] - 6s 3ms/step - loss: 0.0578 - accuracy:
0.9811
Epoch 3/5
1875/1875 [==============================] - 6s 3ms/step - loss: 0.0528 - accuracy:
0.9825
Epoch 4/5
1875/1875 [==============================] - 6s 3ms/step - loss: 0.0500 - accuracy:
0.9833
Epoch 5/5
1875/1875 [==============================] - 6s 3ms/step - loss: 0.0453 - accuracy:
0.9844
313/313 [==============================] - 1s 3ms/step - loss: 0.0697 - accuracy: 0.9797
[0.06965507566928864, 0.9797000288963318]
Result
Thus the program to implement deep learning neural networks models have been executed
successfully and the output got verified