Ai & ML Lab Manual (18CSL76)
Ai & ML Lab Manual (18CSL76)
Prepared By:
Shanta H Biradar
Assistant Professor
Dept of ISE
Verified By:
Dr G C Bhanuprakash
Professor and H.O.D.,
Dept of ISE
Vision
• To be a centre of excellence in technical and management education concurrently
focusing on disciplined and integrated development of personality through quality
education, sports, cultural and co-curricular activities.
• To promote transformation of students into better human beings, responsible citizens
and competent professionals to serve as a valuable resource for industry, work
environment and society.
Mission
• To impart quality technical education, provide state-of-art facilities, achieve high quality
in teaching-learning & research and encourage extra & co-curricular activities.
• To stimulate in students a spirit of inquiry and desire to gain knowledge and skills to
meet the changing needs that can enrich their lives.
• To provide opportunity and resources for developing skills for employability and
entrepreneurship, nurturing leadership qualities, imbibing professional ethics and
societal commitment.
• To create an ambiance and nurture conducive environment for dedicated and quality
staff to upgrade their knowledge & skills and disseminate the same to students on a
sustainable long term basis.
• To facilitate effective interaction with the industries, alumni and research institutions .
Vision
• To empower students with knowledge and skills to develop the competency in the emerging
areas of Information Technology.
Mission
• To train the students to have Professional career in IT industry and Higher studies through
Quality Education.
• To provide outstanding Teaching and Research environment by implementing innovative
Teaching and Research Methodologies for Quality Education and Research.
Program Educational Objectives
1. Graduates will have Prospective careers in the field of Information Technology.
2. Graduates will have good Leadership Qualities, Self Learning abilities and zeal for
higher studies and Research.
3. Graduates will follow Ethical Practices and exhibit high level of Professionalism by
participating and addressing Technical, Business and environmental challenges.
COURSE OUTCOMES
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2 PSO3
CO1 3
3
CO2 3
1
CO3 3
3
CO4 3
1
PO,PSO AND CO mapping Justification
Program Outcomes
Course Learning Objectives: This course (18CSL76) will enable students to:
• Implement and evaluate AI and ML algorithms in and Python programming language.
Descriptions (if any):
Installation procedure of the required software must be demonstrated, carried out in groups
and documented in the journal.
Programs List:
1. Implement A* Search algorithm.
2. Implement AO* Search algorithm.
3. For a given set of training data examples stored in a .CSV file, implement and demonstrate the
Candidate-Elimination algorithm to output a description of the set of all hypotheses consistent
with the training examples.
4. Write a program to demonstrate the working of the decision tree based ID3 algorithm. Use an
Appropriate data set for building the decision tree and apply this knowledge to classify a new
sample.
5. Build an Artificial Neural Network by implementing the Back propagation algorithm and test the
Same using appropriate data sets.
6. Write a program to implement the naïve Bayesian classifier for a sample training data set stored
as a .CSV file. Compute the accuracy of the classifier, considering few test data sets.
7. Apply EM algorithm to cluster a set of data stored in a .CSV file. Use the same data set for
clustering using k-Means algorithm. Compare the results of these two algorithms and comment
on the quality of clustering. You can add Java/Python ML library classes/API in the program.
8. Write a program to implement k-Nearest Neighbor algorithm to classify the iris data set. Print
Both correct and wrong predictions. Java/Python ML library classes can be used for this problem.
9. Implement the non-parametric Locally Weighted Regression algorithm in order to fit data points.
Select appropriate data set for your experiment and draw graphs
1. Go to the Anaconda Website and choose a Python 3.x graphical installer (A) or a Python 2.x
graphical installer (B). If you aren't sure which Python version you want to install, choose
Python 3. Do not choose both.
2. Locate your download and double click it.
4. Click on Next.
5. Note your installation location and then click Next.
6. This is an important part of the installation process. The recommended approach is to not
check the box to add Anaconda to your path. This means you will have to use Anaconda
Navigator or the Anaconda Command Prompt (located in the Start Menu under "Anaconda")
when you wish to use Anaconda (you can always add Anaconda to your PATH later if you don't
check the box). If you want to be able to use Anaconda in your command prompt (or git
bash, cmder, powershell etc), please use the alternative approach and check the box.
7. Click on Next.
8. You can install Microsoft VSCode if you wish, but it is optional.
9. Click on Finish.
2. Check if you already have Anaconda added to your path. Enter the commands below into
your Command Prompt. This is checking if you already have Anaconda added to your path. If
you get a command not recognized error like in the left side of the image below, proceed to step
3. If you get an output similar to the right side of the image below, you have already added
Anaconda to your path.
conda --version
python --version
3. If you don't know where your conda and/or python is, open an Anaconda Prompt and type in
the following commands. This is telling you where conda and python are located on your
computer.
where conda
where python
4. Add conda and python to your PATH. You can do this by going to your Environment
Variables and adding the output of step 3 (enclosed in the red rectangle) to your path. If you are
having issues, here is a short video on adding conda and python to your PATH.
5. Open a new Command Prompt. Try typing conda --version and python --version into
the Command Prompt to check to see if everything went well.
Artificial Intelligence & Machine Learning Lab Manual-18CSL76(2018 Scheme) 2023
PROGRAM.NO.1
Implement A* Search algorithm.
open_set = set(start_node)
closed_set = set()
g = {} #store distance from starting node
parents = {} # parents contains an adjacency map of all nodes
#for each node m,compare its distance from start i.e g(m) to the
#from start through n node
else:
if g[m] > g[n] + weight: # if better cost found, then update the existing cost g(m)
g[m] = g[n] + weight
#change parent of m to n
parents[m] = n
if n == None:
print('Path does not exist!')
return None
while parents[n] != n:
path.append(n)
n = parents[n]
path.append(start_node)
path.reverse()
def get_neighbors(v):
if v in Graph_nodes:
return Graph_nodes[v]
else:
return None
#for simplicity we ll consider heuristic distances given
#and this function returns heuristic distance for all nodes
def heuristic(n):
H_dist = {
'S': 8,
'A': 8,
'B': 4,
'C': 3,
'D': 1000,
'E': 1000,
'G': 0,
return H_dist[n]
A_star('S', 'G')
OUTPUT:- (Note:-better to Run in Jupyter Note book of Anaconda for correct output)
Optimal Path :
['S', 'B', 'G']
PROGRAM.Mo.2(AO* Algorithm)
solvable = False
marked = {}
is_expanded = False
# If the child nodes have sub trees then recursively visit them to recalculate the heuristic of
the child node
if len(min_cost_group) > 1:
if (min_cost_group[0] in allNodes):
is_expanded = True
recAOStar(min_cost_group[0])
if (min_cost_group[1] in allNodes):
is_expanded = True
recAOStar(min_cost_group[1])
else:
if (min_cost_group in allNodes):
is_expanded = True
recAOStar(min_cost_group)
# If the child node had any subtree and expanded, verify if the new heuristic value is still the
least among all nodes
if is_expanded:
min_cost_verify, min_cost_group_verify = least_cost_group(and_nodes, or_nodes, {})
if min_cost_group == min_cost_group_verify:
solvable = True
change_heuristic(n, min_cost_verify)
optimal_child_group[n] = min_cost_group
# If the child node does not have any subtrees then no change in heuristic, so update the
min cost of the current node
else:
solvable = True
change_heuristic(n, min_cost)
optimal_child_group[n] = min_cost_group
# Function to calculate the min cost among all the child nodes
def least_cost_group(and_nodes, or_nodes, marked):
node_wise_cost = {}
min_cost = 999999
min_cost_group = None
if len(node) > 1:
if node[0] in optimal_child_group:
print("->", end="")
print_path(node[0])
if node[1] in optimal_child_group:
print("->", end="")
print_path(node[1])
else:
if node in optimal_child_group:
print("->", end="")
print_path(node)
optimal_child_group = {}
optimal_cost = recAOStar('A')
OUTPUT:-
Expanding Node : A
Expanding Node : B
Expanding Node : C
Expanding Node : D
Nodes which gives optimal cost are
CD->HI->J
Optimal Cost is :: 5
dataarr=[]
with open('data/enjoysport.csv') as f:
for line in f:
dataarr.append(line.strip().split(','))
rows = len(dataarr)
cols = len(dataarr[0])
shypo = ['0']*(cols-1)
ghypo = [['?']*(cols-1)]
print("Initial Specific Hypothesis is = ", shypo)
print("Initial General Hypothesis is = ", ghypo)
if lst[cols-1] == "1":
for i in range(0, cols-1):
if shypo[i] == lst[i]:
continue
shypo[i] = '?' if shypo[i] != '0' else lst[i]
for g in ghypo:
if g[i] != '?' and shypo[i] == '?':
ghypo.remove(g)
PROGRAM.NO.4(Decision trees)
Write a program to demonstrate the working of the decision tree based ID3 algorithm. Use an
appropriate data set for building the decision tree and apply this knowledge toclassify a new
sample.
import math
def calc_entropy(data):
# Calculate the length of the data-set
entries = len(data)
labels = {}
# Read the class labels from the data-set file into the dict object "labels"
for rec in data:
label = rec[-1]
if label not in labels.keys():
labels[label] = 0
labels[label] += 1
entropy = 0.0
# For every class label (x) calculate the probability p(x)
for key in labels:
prob = float(labels[key])/entries
# Entropy formula calculation
entropy -= prob * math.log(prob, 2)
# Return the entropy of the data-set
return entropy
def attribute_selection(data):
features = len(data[0]) - 1
baseEntropy = calc_entropy(data)
max_InfoGain = 0.0
bestAttr = -1
for i in range(features):
# store the values of the features in a variable
AttrList = [rec[i] for rec in data]
# probability calculation
prob = len(newData)/float(len(data))
if classList.count(classList[0]) == len(classList):
return classList[0]
maxGainNode = attribute_selection(data)
treeLabel = labels[maxGainNode]
return theTree
Outlook
overcast
d = yes
rain
Wind
weak
d = yes
strong
d = no
sunny
Humidity
high
d = no
normal
d = yes
{'Outlook': {'overcast': 'yes', 'rain': {'Wind': {'weak': 'yes', 'strong': 'no'}}, 'sunny': {'Humidity': {'high':
'no', 'normal': 'yes'}}}}
import numpy as np
X = np.array(([2, 9], [1, 5], [3, 6]), dtype=float)
y = np.array(([.92], [.86], [.89]), dtype=float)
X = X/np.amax(X, axis=0)
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def der_sigmoid(x):
return x * (1 - x)
epoch = 5000
lr = 0.01
neurons_i = 2
neurons_h = 3
neurons_o = 1
for i in range(epoch):
inp_h = np.dot(X, weight_h) + bias_h
out_h = sigmoid(inp_h)
err_o = y - out_o
grad_o = der_sigmoid(out_o)
delta_o = err_o * grad_o
err_h = delta_o.dot(weight_o.T)
grad_h = der_sigmoid(out_h)
delta_h = err_h * grad_h
weight_o += out_h.T.dot(delta_o) * lr
weight_h += X.T.dot(delta_h) * lr
print('Input: ', X)
print('Actual: ', y)
print('Predicted: ', out_o)
Dept of ISE, SIR M VISVESVARAYA INSTITUTE OF Page 14
TECHNOLOGY,CHICKBALLAPUR
Artificial Intelligence & Machine Learning Lab Manual-18CSL76(2018 Scheme) 2023
OUTPUT:-
Input: [[ 0.66666667 1. ]
[ 0.33333333 0.55555556]
[ 1. 0.66666667]]
Actual: [[ 0.92]
[ 0.86]
[ 0.89]]
Predicted: [[ 0.89371021]
[ 0.87852765]
[ 0.89052431]]
import pandas as pd
import numpy as np
mush = pd.read_csv("data/mushrooms.csv")
mush = mush.replace('?', np.nan)
mush.dropna(axis=1, inplace=True)
target = 'class'
features = mush.columns[mush.columns != target]
target_classes = mush[target].unique()
test = mush.sample(frac=.3)
mush = mush.drop(test.index)
cond_probs = {}
target_class_prob = {}
for t in target_classes:
mush_t = mush[mush[target] == t][features]
target_class_prob[t] = float(len(mush_t) / len(mush))
class_prob = {}
return max_class
b = []
for i in mush.index:
b.append(classify(mush.loc[i, features]) == mush.loc[i, target])
print(sum(b), "correct of", len(mush))
print("Accuracy:", sum(b)/len(mush))
# Test data
b = []
for i in test.index:
b.append(classify(test.loc[i, features]) == test.loc[i, target])
print(sum(b), "correct of", len(test))
print("Accuracy:", sum(b)/len(test))
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from sklearn.mixture import GaussianMixture
from sklearn.cluster import KMeans
data = pd.read_csv('data/ex.csv')
f1 = data['V1'].values
f2 = data['V2'].values
X = np.array(list(zip(f1, f2)))
print("x: ", X)
kmeans = KMeans(2)
labels = kmeans.fit(X).predict(X)
print("labels for kmeans:", labels)
centroids = kmeans.cluster_centers_
print("centroids:", centroids)
plt.scatter(centroids[:, 0], centroids[:, 1], marker='*', c='red')
plt.show()
gmm = GaussianMixture(2)
labels = gmm.fit(X).predict(X)
print("Labels for GMM: ", labels)
PROGRAM.NO.8(KNN)
Write a program to implement k-Nearest Neighbor algorithm to classify the iris data set. Print
both correct and wrong predictions. Java/Python ML library classes can be used for this
problem.
iris_dataset = load_iris()
targets = iris_dataset.target_names
print("Class : number")
for i in range(len(targets)):
print(targets[i], ':', i)
for i in range(len(X_test)):
x_new = np.array([X_test[i]])
prediction = kn.predict(x_new)
print("Actual:[{0}] [{1}],Predicted:{2} {3}".format(y_test[i], targets[y_test[i]], prediction,
targets[prediction]))
OUTPUT:-
Class : number
setosa : 0
versicolor : 1
virginica : 2
Actual:[2] [virginica],Predicted:[2] ['virginica']
Actual:[2] [virginica],Predicted:[2] ['virginica']
Actual:[2] [virginica],Predicted:[2] ['virginica']
Actual:[1] [versicolor],Predicted:[1] ['versicolor']
Actual:[1] [versicolor],Predicted:[1] ['versicolor']
Actual:[2] [virginica],Predicted:[2] ['virginica']
Actual:[1] [versicolor],Predicted:[1] ['versicolor']
Actual:[2] [virginica],Predicted:[2] ['virginica']
Actual:[1] [versicolor],Predicted:[1] ['versicolor']
Actual:[0] [setosa],Predicted:[0] ['setosa']
Actual:[1] [versicolor],Predicted:[1] ['versicolor']
Accuracy: 0.973684210526
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
f = 0.25
yest = lowess(x, y, f, 3)
import pylab as pl
pl.clf()
pl.plot(x, y, label='y noisy')
pl.plot(x, yest, label='y predicted')
pl.legend()
pl.show()
OUTPUT:-