Fods Lab Manual
Fods Lab Manual
BONAFIDE CERTIFICATE
To be an eminent centre for Academia, Industry and Research by imparting knowledge, relevant
practices and inculcating human values to address global challenges through novelty and sustainability.
IM1: To create next generation leaders by effective teaching learning methodologies and instill scientific
spark in them to meet the global challenges.
IM2: To transform lives through deployment of emerging technology, novelty and sustainability.
IM3: To inculcate human values and ethical principles to cater to the societal needs.
IM4: To contribute towards the research ecosystem by providing a suitable, effective platform for
interaction between industry, academia and R & D establishments.
To Excel in the emerging areas of Electronics and Communication Engineering by imparting knowledge,
relevant practices and inculcating human values to transform the students as potential resources to cater the
industrial and societal development through sustainable technology growth.
DM1: To provide strong fundamentals and technical skills through effective teaching learning Methodologies.
DM2: To transform lives of the students by fostering ethical values, creativity and innovation to become
Entrepreneurs and establish Start-ups.
DM3: To habituate the students to focus on sustainable solutions to improve the quality of life and welfare of the
society.
DM4: To provide an ambience for research through collaborations with industry and academia.
DM5: To inculcate learning of emerging technologies for pursuing higher studies leading to lifelong learning.
PROGRAM OUTCOMES (POs):
PO8: Ethics
Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
PO9: Individual and team work
Function effectively as an individual, and as a member or leader in diverse teams, and
in multidisciplinary settings.
PO10: Communication
Communicate effectively on complex engineering activities with the engineering
community and with society at large, such as, being able to comprehend and write effective
reports and design documentation, make effective presentations, and give and receive clear
instructions.
PO11: Project management and finance
Demonstrate knowledge and understanding of the engineering and management
principles and apply these to one’s own work, as a member and leader in a team, to manage
projects and in multidisciplinary environments.
PEO1: Contribute to the industry as an Engineer through sound knowledge acquired in core engineering
to develop new processes and implement the solutions for industrial problems.
PEO3: Create a better future by pursuing higher education / research and develop the sustainable
products / solutions to meet the demand.
PSO1: To analyze, design and develop quality solutions in Fundamentals of Data Science and Machine
Learning by adapting the emerging technologies.
PSO2: To innovate ideas and solutions for real time problems in industrial and domestic automation
using Data Science and Machine Learning / IOT tools.
CS3411 - FUNDAMENTALS OF DATA SCIENCE AND MACHINE LEARNING
LIST OF EXPERIMENTS:
COURSE OUTCOMES:
CO1: Understand the benefits, uses, and different facets of Data Science.
CO2: Master the Data Science process, including goal definition, data analysis, and
presentation.
CO4: Differentiate between various learning algorithms and address issues of over fitting, under
CO5: Build, train, and optimize neural networks while addressing deep learning challenges
INDEX OF CONTENTS
2
Implement Bayesian Networks
3
Build Regression models
6
Implement ensembling techniques
7
Implement clustering algorithms
8
Implement EM for Bayesian networks
AIM:
To implement uninformed search algorithms of BFS(Breadth-First Search) and DFS(Depth-First
Search) using Python.
APPARATUS REQUIRED:
ALGORITHM:
Step 1 : Start.
Step 2 : Initialize an empty queue and mark all vertices as unvisited.
Step 3 : Add the starting vertex to the queue and mark it as visited.
Step 4 : While the queue is not empty:
Dequeue the next vertex from the queue.
For each adjacent vertex that is not visited, mark it as visited
and add it to the queue.
Step 5 : Repeat step 3 until the queue is empty.
Step 6 : Exit.
PROGRAM:
A) Breadth-First Search(BFS)
visited.append(node) queue.append(node)
m = queue.pop(0)
1
")
2
if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)
# Driver Code
OUTPUT:
3
B) Depth-First Search(DFS)
graph = { '5' : ['3','7'], '3' : ['2', '4'], '7' : ['8'], '2' : [], '4' : ['8'], '8' : [] }
print (node)
visited.add(node)
# Driver Code
OUTPUT:
RESULT:
Thus the implementation of uninformed search algorithms of BFS and DFS is written and executed
successfully.
4
EXP.NO: 12 DATE :
AIM:
To implement informed search algorithms of A* and memory-bounded A* using python.
APPARATUS REQUIRED:
ALGORITHM:
Step 1 : Start
Step 2 : Initialize the start and goal nodes.
Step 3 : Create an empty open list and a closed list.
Step 4 : Add the start node to the open list.
Step 5 : While the open list is not empty:
Find the node with the lowest f score in open list.
If this node is the goal, return the path.
Otherwise, move the node to the closed list and consider its neighbors.
For each neighbor, calculate its g score (the cost to move to this node
from the start) and h score (the heuristic estimate of the cost to
move from this node to the goal).
If the neighbor is not in the open list or its new g score is lower than its
old g score, update the neighbor's f, g, and h scores and set its parent
to the current node.
Add the neighbor to the open list if it is not already there.
Step 6 : If the goal node is not reached, there is no path.
Step 7 : Exit
PROGRAM:
a) A*
to_add.append(m)
def a_star_algo(cost, heuristic, start, goals):
path = [] #optimal path
pathSet = []
## closed list
Closed_list = [] # ex: S, A, ...
## open list
open_list = [start]
path_len = {}
path_len[start] = 0
#for back-tracking:
parent_node = {}
parent_node[start]=start
while len(open_list) > 0:
#get node with least f
node = None
for n in open_list:
if node == None or f(path_len, heuristic, n) < f(path_len, heuristic, node):
node = n
if node == None: #path does not exist
break
if node in goals: #[6, 7, 10]
f_n = f(path_len, heuristic, node)
reconstruct = []
aux = node
while parent_node[aux] != aux: # [(S, 9, S), (A, 6, S)]
reconstruct.append(aux) #[ A, S]
aux = parent_node[aux]
reconstruct.append(start)
reconstruct.reverse()
pathSet.append((reconstruct, f_n))
update(open_list, closed_list, node)
continue
#explore the current node
6
path_cost = cost[node] #[0, 0, 5, 9, -1, 6, -1, -1, -1, -1, -1]
for adj_node in range(0, len(path_cost)):
weight = path_cost[adj_node]
if weight > 0:
if adj_node not in open_list and adj_node not in closed_list:
open_list.append(adj_node)
parent_node[adj_node] = node
path_len[adj_node] = path_len[node] + weight
else:
if path_len[adj_node] > path_len[node] + weight:
if len(pathSet) > 0:
pathSet = sorted(pathSet, key=lambda x: x[1]) #[([1,5,7], 8), ([1,2,3], 10)]
path = pathSet[0][0]
return path
#driver
code #Input
give_cost = [[0,1,2.1],[1,0,1],[3.1,1,0]]
start=0
give_goals=[2,3]
heuristic = [1,2.1,0]
getPath = a_star_algo(give_cost, heuristic, start, give_goals)
print(getPath)
OUTPUT:
7
b) Memory-Bounded A*
import copy
from heapq import heappush, heappop
row = [ 1, 0, -1, 0 ]
col = [ 0, -1, 0, 1 ]
class priorityQueue:
class node:
def init (self, parent, mat, empty_tile_pos, cost, level):
self.mat = mat
# Total Misplaced
tiles self.cost = cost
count = 0
for i in range(n):
for j in range(n):
if ((mat[i][j]) and (mat[i][j] != final[i]
[j])):
count +=
1 return count
new_mat =
copy.deepcopy(mat) x1 =
empty_tile_pos[0]
y1 = empty_tile_pos[1]
x2 = new_empty_tile_pos[0]
y2 = new_empty_tile_pos[1]
9
new_mat[x1][y1], new_mat[x2][y2] = new_mat[x2][y2], new_mat[x1][y1]
10
# Set number of misplaced tiles
cost = calculateCost(new_mat, final)
new_node = node(parent, new_mat, new_empty_tile_pos, cost,
level)
return new_node
def
printPath(root):
if root ==
None:
return
printPath(root.parent)
printMatrix(root.mat)
print()
def solve(initial, empty_tile_pos,
final): pq = priorityQueue()
pq.push(root)
11
while not pq.empty():
12
minimum = pq.pop()
minimum.empty_tile_pos[0] + row[i],
minimum.empty_tile_pos[1] + col[i], ]
if is Safe(new_tile_pos[0], new_tile_pos[1]):
minimum.empty_tile_pos,
new_tile_pos,
minimum.level + 1,
minimum, final,)
# Driver Code
# 0 represents the blank space
# Initial state
initial = [ [ 2, 8, 3 ],
[ 1, 6, 4 ],
[ 7, 0, 5 ] ]
13
# Final State
final = [ [ 1, 2, 3
],
[ 8, 0, 4 ],
[ 7, 6, 5 ] ]
# Function call
solve(initial, empty_tile_pos, final)
OUTPUT:
RESULT:
Thus the implementation of A* and memory-bounded A* using python is written and executed
14
successfully.
15
EXP.NO: 01 DATE :
AIM:
To implement a naïve Bayes model using Python.
APPARATUS REQUIRED:
1. GOOGLE COLLAB
2. PC
ALGORITHM:
Step 1: Start
Step 2: import the dataset and necessary
dependencies Step 3: Calculate Prior Probability of
Classes P(y) Step 4: Calculate the Likelihood Table
for all features
Step 5: Calculate Posterior Probability for each class using
the Naive Bayesian equation
Step 6: End
PROGRAM:
OUTPUT:
RESULT:
Thus the implementation of the naïve Bayes model using python is written and executed successfully.
16
EXP.NO: 02 DATE :
AIM:
To implement Bayesian Networks using Python.
APPARATUS REQUIRED:
1. GOOGLE COLLAB
2. PC
ALGORITHM:
Step 1: start
Step 2: Construct the Bayesian network by specifying the nodes and their
conditional probability distributions.
Step 3: Identify the set of observed variables (i.e., the evidence).
Step 4: For each unobserved variable, compute its posterior probability
given the evidence using the Bayes' rule and the conditional
probability distributions of the variable and its parents.
Step 5: Return the posterior probabilities of interest.
Step 6: stop
.
PROGRAM:
import pandas as pd # for data
manipulation import networkx as nx # for
drawing graphs
import matplotlib.pyplot as plt # for drawing
graphs # for creating Bayesian Belief Networks
(BBN) from pybbn.graph.dag import Bbn
from pybbn.graph.edge import Edge, EdgeType
from pybbn.graph.jointree import EvidenceBuilder
from pybbn.graph.node import BbnNode
from pybbn.graph.variable import Variable
from pybbn.pptc.inferencecontroller import InferenceController
# For other columns with missing values, fill them in with column mean
df=df.fillna(df.mean())
OUTPUT:
in a future version this will raise TypeError. Select only valid columns
RESULT:
Thus the implementation of the Bayesian Networks using python is written and executed successfully.
18
EXP.NO: 03 DATE :
AIM:
To build Regression Models using Python.
APPARATUS REQUIRED:
1. GOOGLE COLLAB
2. PC
ALGORITHM:
PROGRAM:
import numpy as np
from sklearn.linear_model import LinearRegression
x = [[0, 1], [5, 1], [15, 2], [25, 5], [35, 11], [45, 15], [55, 34], [60, 35]]
y = [4, 5, 20, 14, 32, 22, 38, 43]
x, y = np.array(x), np.array(y)
model = LinearRegression().fit(x, y) r_sq = model.score(x, y)
print(f"coefficient of determination: {r_sq}") print(f"intercept: {model.intercept_}")
print(f"coefficients: {model.coef_}")
y_pred = model.predict(x)
print(f"predicted response:\n{y_pred}")
OUTPUT:
RESULT:
Thus the building of regression model using python is written and executed successfully.
19
EXP.NO: 04 DATE :
AIM:
1. GOOGLE COLLAB
2. PC
ALGORITHM:
PROGRAM:
%matplotlib inline
import numpy as
np
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
from sklearn.datasets import make_blobs
x, y = make_blobs(n_samples=300,
centers=4, random_state=0,
cluster_std=1.0)
plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='rainbow');
from sklearn.tree import DecisionTreeClassifier
tree = DecisionTreeClassifier().fit(X, y)
def visualize_classifier(model, X, y, ax=None, cmap='rainbow'):
ax = ax or plt.gca()
20
# Plot the training points
ax.scatter(X[:, 0], X[:, 1], c=y, s=30, cmap=cmap,
21
clim=(y.min(), y.max()), zorder=3)
ax.axis('tight')
ax.axis('off')
xlim =
ax.get_xlim() ylim
= ax.get_ylim()
ax.set(xlim=xlim, ylim=ylim)
visualize_classifier(DecisionTreeClassifier(), X,
y)
OUTPUT:
RESULT:
Thus the building of decision trees and random forests using python is written and executed
22
successfully.
.
23
EXP.NO: 05 DATE :
AIM:
To build SVM models using Python.
APPARATUS REQUIRED:
1. GOOGLE COLLAB
2. PC
ALGORITHM:
Step 1: Start
Step 2: Load the dataset you want to use for training the model.
Step 3: Preprocess the data to make it ready for training the model.
Step 4: Split the data into two sets - one for training the model and the other for
testing the model.
Step 5: splitting it into training and testing sets.
Step 6: Once the SVM model is defined, you can train it using the training set.
Step 7: Use cross-validation techniques like grid search to find the optimal values
for the hyperparameters.
Step 8: Stop.
PROGRAM:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dataset = pd.read_csv('Social_Network_Ads.csv')
X = dataset.iloc[:, [2, 3]].values
y = dataset.iloc[:, 4].values
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)
X_test = sc.transform(X_test)
from sklearn.svm import SVC
classifier = SVC(kernel = 'rbf', random_state = 0)
24
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)
from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test,
y_pred) print(cm)
accuracy_score(y_test,y_pred)
from matplotlib.colors import ListedColormap
X_set, y_set = X_test, y_test
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step
= 0.01),
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
OUTPUT:
RESULT:
Thus the building of SVM models using python is written and executed successfully.
25
EXP.NO: 06 DATE :
AIM:
To implement ensembling techniques using Python.
APPARATUS REQUIRED:
1. GOOGLE COLLAB
2. PC
ALGORITHM:
PROGRAM:
# importing utility modules
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
# printing the mean squared error between real value and predicted value
print(mean_squared_error(y_test, pred_final))
OUTPUT:
RESULT:
Thus the implementation of ensembling techniques using python is written and executed
successfully.
27
EXP.NO: 07 DATE :
AIM:
To implement clustering algorithms using Python.
APPARATUS REQUIRED:
1. GOOGLE COLLAB
2. PC
ALGORITHMS
PROGRAM:
# affinity propagation clustering
from numpy import unique
from numpy import where
from sklearn.datasets import
make_classification from sklearn.cluster import
AffinityPropagation from matplotlib import
pyplot
# define dataset
X, _ = make_classification(n_samples=1000, n_features=2, n_informative=2,
n_redundant=0, n_clusters_per_class=1, random_state=4)
# define the model
model = AffinityPropagation(damping=0.9) #
fit the model
model.fit(X)
# assign a cluster to each example
yhat = model.predict(X)
# retrieve unique
28
clusters clusters =
unique(yhat)
29
# create scatter plot for samples from each cluster for
cluster in clusters:
# get row indexes for samples with this cluster
row_ix = where(yhat == cluster)
# create scatter of these samples
pyplot.scatter(X[row_ix, 0], X[row_ix, 1])
# show the plot
pyplot.show()
OUTPUT:
RESULT:
Thus the implementation of clustering algorithms using python is written and executed
successfully.
30
EXP.NO: 08 DATE :
AIM:
To implement EM for Bayesian Networks using Python.
APPARATUS REQUIRED:
1. GOOGLE COLLAB
2. PC
ALGORITHM:
PROGRAM:
import numpy as np
import pandas as pd
import csv
heartDisease = pd.read_csv('heart.csv')
heartDisease = heartDisease.replace('?',np.nan)
print(heartDisease.head())
31
print('\n Attributes and datatypes')
print(heartDisease.dtypes)
model= BayesianModel([('age','heartdisease'),('sex','heartdisease'),('exang','heartdisease'),
('cp','heartdisease'),('heartdisease','restecg'),('heartdisease','chol')])
model.fit(heartDisease,estimator=MaximumLikelihoodEstimator)
HeartDiseasetest_infer = VariableElimination(model)
q1=HeartDiseasetest_infer.query(variables=['heartdisease'],evidence={'restecg':1})
print(q1)
q2=HeartDiseasetest_infer.query(variables=['heartdisease'],evidence={'cp':2})
print(q2)
32
OUTPUT:
RESULT:
Thus the implementation of EM for Bayesian Networks using python is written and executed
successfully.
33
EXP.NO: 09 DATE :
AIM:
To build simple NN models using Python.
APPARATUS REQUIRED:
1. GOOGLE COLLAB
2. PC
3. DATA SET
ALGORITHM:
Step 1 : Start
Step 2 : Create a sigmoid function
Step 3 : Initialize the requires parameters weight and bias.
Step 4 : Create forword propagation function which takes x ,the initialized parameters input
and returns a2 , cache .
Step 5 : Create a calculate_cost function which takes a2 , y as input parameters and return the
cost.
Step 6 : Now create a backword_propagation function which takes x , y , cache and
parameters as input parameters and returns grads.
Step 7 : Create a update_parameters function which takes parameters , grads , learning_rate
as input parameters and returns the updated values.
Step 8 : Now create model which takes the input parameters x , y , n_x , n_h , n_y ,
num_of_iters , learning_rate and returns the parameters as output.
Step 9 : Create a predict function which takes x and parameters as input parameters and
returns the prediction as output.
Step 10 : Stop
PROGRAM:
# Import python libraries required in this example:
import numpy as np
from scipy.special import expit as activation_function
f rom scipy.stats import truncnorm
34
return truncnorm(
(low - mean) / sd, (upp - mean) / sd, loc=mean, scale=sd)
self.no_of_in_nodes = no_of_in_nodes
self.no_of_out_nodes = no_of_out_nodes
self.no_of_hidden_nodes = no_of_hidden_nodes
self.learning_rate = learning_rate
self.create_weight_matrices()
def create_weight_matrices(self):
""" A method to initialize the weight matrices of the neural
network""" rad = 1 / np.sqrt(self.no_of_in_nodes)
X = truncated_normal(mean=0, sd=1, low=-rad, upp=rad)
self.weights_in_hidden = X.rvs((self.no_of_hidden_nodes,
self.no_of_in_nodes))
rad = 1 / np.sqrt(self.no_of_hidden_nodes)
X = truncated_normal(mean=0, sd=1, low=-rad, upp=rad)
self.weights_hidden_out = X.rvs((self.no_of_out_nodes,
self.no_of_hidden_nodes))
36
'input_vector' can be tuple, list or ndarray
"""
# Turn the input vector into a column vector:
input_vector = np.array(input_vector, ndmin=2).T
# activation_function() implements the expit function,
# which is an implementation of the sigmoid function:
input_hidden = activation_function(self.weights_in_hidden @ input_vector)
output_vector = activation_function(self.weights_hidden_out @ input_hidden)
return output_vector
OUTPUT:
RESULT:
Thus the building of simple NN models using python is written and executed successfully.
37
EXP.NO: 10 DATE :
AIM:
To build deep learning NN models using Python.
APPARATUS REQUIRED:
1. GOOGLE COLLAB
2. PC
3. DATA SET
ALGORITHM:
Step 1: Start
Step 2: Import the necessary libraries
Step 3: Set the random seed for reproducibility
Step 4: Define the model architecture
Step 5: Initialize the weights and biases for the input and hidden layers
Step 6: Initialize the weights and biases for the hidden and output
layers Step 7: Define the input data, labels and hyperparameters
Step 8: Define the Tensorflow variables for the weights and biases
Step 9: Define the forward propagation and backpropagation algorithms
Step 10: Train the model and make some predictions with the trained
model
Step 11: End
PROGRAM:
from sklearn.neural_network import MLPClassifier
OUTPUT:
RESULT:
Thus the building of deep learning NN models using python is written and executed successfully.
38
39