Aiml Lab Manual Nceh
Aiml Lab Manual Nceh
ALGORITHM
PROGRAM
open_set = set(start_node)
closed_set = set()
g = {}
parents = {}
g[start_node] = 0
parents[start_node] = start_node
n = None
for v in open_set:
n=v
pass
else:
open_set.add(m)
parents[m] = n
else:
parents[m] = n
if m in closed_set:
closed_set.remove(m)
open_set.add(m)
if n == None:
return None
if n == stop_node:
path = []
while parents[n] != n:
path.append(n)
n = parents[n]
path.append(start_node)
path.reverse()
return path
open_set.remove(n)
closed_set.add(n)
return None
def get_neighbors(v):
if v in Graph_nodes:
return Graph_nodes[v]
else:
return None
def heuristic(n):
H_dist = {
'A': 11,
'B': 6,
'C': 5,
'D': 7,
'E': 3,
'F': 6,
'G': 5,
'H': 3,
'I': 1,
'J': 0
return H_dist[n]
Graph_nodes = {
aStarAlgo('A', 'J')
def heuristic(n):
H_dist = {
'A': 11,
'B': 6,
'C': 99,
'D': 1,
'E': 7,
'G': 0,
return H_dist[n]
Graph_nodes = {
aStarAlgo('A', 'G')
OUTPUT
Step-2: Transverse the graph following the current path, accumulating node that has
not yet been expanded or solved.
Step-3: Select any of these nodes and explore it. If it has no successors then call this
value- FUTILITY else calculate f'(n) for each of the successors.
Step-5: Change the value of f'(n) for the newly created node to reflect its successors by
backpropagation.
Step-6: Whenever possible use the most promising routes, If a node is marked as
SOLVED then mark the parent node as SOLVED.
Step-7: If the starting node is SOLVED or value is greater than FUTILITY then stop
else repeat from Step-2.
PROGRAM
class Graph:
self.graph = graph
self.H=heuristicNodeList
self.start=startNode
self.parent={}
self.status={}
self.solutionGraph={}
def applyAOStar(self):
self.aoStar(self.start, False)
return self.graph.get(v,'')
def getStatus(self,v):
return self.status.get(v,0)
self.status[v]=val
return self.H.get(n,0)
self.H[n]=value
def printSolution(self):
print(" ")
print(self.solutionGraph)
print(" ")
minimumCost=0
costToChildNodeListDict={}
costToChildNodeListDict[minimumCost]=[]
flag=True
cost=0
nodeList=[]
cost=cost+self.getHeuristicNodeValue(c)+weight
nodeList.append(c)
if flag==True:
minimumCost=cost
costToChildNodeListDict[minimumCost]=nodeList
flag=False
else:
if minimumCost>cost:
minimumCost=cost
costToChildNodeListDict[minimumCost]=nodeList
print(" ")
if self.getStatus(v) >= 0:
print(minimumCost, childNodeList)
self.setHeuristicNodeValue(v, minimumCost)
self.setStatus(v,len(childNodeList))
solved=True
self.parent[childNode]=v
if self.getStatus(childNode)!=-1:
if solved==True:
self.setStatus(v,-1)
self.solutionGraph[v]=childNodeList
if v!=self.start:
self.aoStar(self.parent[v], True)
if backTracking==False:
self.setStatus(childNode,0)
self.aoStar(childNode, False)
h1 = {'A': 1, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 5, 'H': 7, 'I': 7, 'J': 1}
graph1 = {
G1.applyAOStar()
G1.printSolution()
OUTPUT
Graph - 1
HEURISTIC VALUES : {'A': 1, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 5, 'H': 7, 'I': 7, 'J': 1}
SOLUTION GRAPH : {}
PROCESSING NODE : A
10 ['B', 'C']
HEURISTIC VALUES : {'A': 10, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 5, 'H': 7, 'I': 7, 'J':
1}
SOLUTION GRAPH : {}
PROCESSING NODE : B
6 ['G']
HEURISTIC VALUES : {'A': 10, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 5, 'H': 7, 'I': 7, 'J':
1}
SOLUTION GRAPH : {}
PROCESSING NODE : A
10 ['B', 'C']
HEURISTIC VALUES : {'A': 10, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 5, 'H': 7, 'I': 7, 'J':
1}
SOLUTION GRAPH : {}
PROCESSING NODE : G
8 ['I']
HEURISTIC VALUES : {'A': 10, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 8, 'H': 7, 'I': 7, 'J':
1}
SOLUTION GRAPH : {}
PROCESSING NODE : B
8 ['H']
HEURISTIC VALUES : {'A': 10, 'B': 8, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 8, 'H': 7, 'I': 7, 'J':
1}
SOLUTION GRAPH : {}
PROCESSING NODE : A
12 ['B', 'C']
HEURISTIC VALUES : {'A': 12, 'B': 8, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 8, 'H': 7, 'I': 7, 'J':
1}
SOLUTION GRAPH : {}
PROCESSING NODE : I
0 []
HEURISTIC VALUES : {'A': 12, 'B': 8, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 8, 'H': 7, 'I': 0, 'J':
1}
SOLUTION GRAPH : {'I': []}
PROCESSING NODE : G
1 ['I']
HEURISTIC VALUES : {'A': 12, 'B': 8, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H': 7, 'I': 0, 'J':
1}
SOLUTION GRAPH : {'I': [], 'G': ['I']}
PROCESSING NODE : B
2 ['G']
HEURISTIC VALUES : {'A': 12, 'B': 2, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H': 7, 'I': 0, 'J':
1}
SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G']}
PROCESSING NODE : A
6 ['B', 'C']
HEURISTIC VALUES : {'A': 6, 'B': 2, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H': 7, 'I': 0, 'J': 1}
SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G']}
PROCESSING NODE : C
2 ['J']
HEURISTIC VALUES : {'A': 6, 'B': 2, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H': 7, 'I': 0, 'J': 1}
SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G']}
PROCESSING NODE : A
6 ['B', 'C']
HEURISTIC VALUES : {'A': 6, 'B': 2, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H': 7, 'I': 0, 'J': 1}
SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G']}
PROCESSING NODE : J
0 []
HEURISTIC VALUES : {'A': 6, 'B': 2, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H': 7, 'I': 0, 'J': 0}
SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G'], 'J': []}
PROCESSING NODE : C
1 ['J']
HEURISTIC VALUES : {'A': 6, 'B': 2, 'C': 1, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H': 7, 'I': 0, 'J': 0}
SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G'], 'J': [], 'C': ['J']}
PROCESSING NODE : A
5 ['B', 'C']
FOR GRAPH SOLUTION, TRAVERSE THE GRAPH FROM THE START NODE: A
{'I': [], 'G': ['I'], 'B': ['G'], 'J': [], 'C': ['J'], 'A': ['B', 'C']}
3. For a given set of training data examples stored in a .CSV file, implement and
demonstrate the Candidate-Elimination algorithm in python to output a description of
the set of all hypotheses consistent with the training examples.
ALGORITHM
If d is positive example
Remove s from S
If d is negative example
Remove g from G
PROGRAM
import numpy as np
import pandas as pd
data = pd.read_csv(path+'/enjoysport.csv')
concepts = np.array(data.iloc[:,0:-1])
print("\nInstances are:\n",concepts)
target = np.array(data.iloc[:,-1])
specific_h = concepts[0].copy()
for i, h in enumerate(concepts):
if target[i] == "yes":
for x in range(len(specific_h)):
if h[x]!= specific_h[x]:
specific_h[x] ='?'
general_h[x][x] ='?'
if target[i] == "no":
for x in range(len(specific_h)):
if h[x]!= specific_h[x]:
general_h[x][x] = specific_h[x]
else:
general_h[x][x] = '?'
print("\n")
indices = [i for i, val in enumerate(general_h) if val == ['?', '?', '?', '?', '?', '?']]
for i in indices:
DATASET
OUTPUT
Instances are:
Generic Boundary: [['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'],
['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?']]
Department of CSE, NCE, Hassan Page 15
Artificial intelligence and Machine Learning [18CSL76]
Instance is Positive
Specific Bundary after 1 Instance is ['sunny' 'warm' 'normal' 'strong' 'warm' 'same']
Generic Boundary after 1 Instance is [['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?',
'?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?']]
Instance is Positive
Specific Bundary after 2 Instance is ['sunny' 'warm' '?' 'strong' 'warm' 'same']
Generic Boundary after 2 Instance is [['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?',
'?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?']]
Instance is Negative
Specific Bundary after 3 Instance is ['sunny' 'warm' '?' 'strong' 'warm' 'same']
Generic Boundary after 3 Instance is [['sunny', '?', '?', '?', '?', '?'], ['?', 'warm', '?', '?',
'?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?',
'?', 'same']]
Instance is Positive
Specific Bundary after 4 Instance is ['sunny' 'warm' '?' 'strong' '?' '?']
Generic Boundary after 4 Instance is [['sunny', '?', '?', '?', '?', '?'], ['?', 'warm', '?', '?',
'?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?',
'?', '?']]
Final Specific_h:
Final General_h:
[['sunny', '?', '?', '?', '?', '?'], ['?', 'warm', '?', '?', '?', '?']]
ALGORITHM
If all Examples are positive, Return the single-node tree Root, with label = +
If all Examples are negative, Return the single-node tree Root, with label = -
Otherwise Begin
Let Examples vi, be the subset of Examples that have value vi for A
If Examples vi , is empty
Else
End
Return Root
PROGRAM
import pandas as pd
import math
import numpy as np
data = pd.read_csv("3-dataset.csv")
features.remove("answer")
class Node:
self.children = []
self.value = ""
self.isLeaf = False
self.pred = ""
def entropy(examples):
pos = 0.0
neg = 0.0
if row["answer"] == "yes":
pos += 1
else:
neg += 1
return 0.0
else:
uniq = np.unique(examples[attr])
gain = entropy(examples)
for u in uniq:
subdata = examples[examples[attr] == u]
sub_e = entropy(subdata)
return gain
root = Node()
max_gain = 0
max_feat = ""
max_gain = gain
max_feat = feature
root.value = max_feat
uniq = np.unique(examples[max_feat])
for u in uniq:
subdata = examples[examples[max_feat] == u]
if entropy(subdata) == 0.0:
newNode = Node()
newNode.isLeaf = True
newNode.value = u
newNode.pred = np.unique(subdata["answer"])
root.children.append(newNode)
else:
dummyNode = Node()
dummyNode.value = u
new_attrs = attrs.copy()
new_attrs.remove(max_feat)
dummyNode.children.append(child)
root.children.append(dummyNode)
return root
for i in range(depth):
print("\t", end="")
print(root.value, end="")
if root.isLeaf:
print()
printTree(child, depth + 1)
printTree(root)
DATASET
OUTPUT
Outlook
Overcast -> ['yes']
Rainy
Wind
Strong -> ['No']
Weak -> ['yes']
Sunny
Humidity
High -> ['No']
Normal -> ['yes']
ALGORITHM
PROGRAM
import numpy as np
y = y/100
#Sigmoid Function
def derivatives_sigmoid(x):
return x * (1 - x)
#Variable initialization
wh=np.random.uniform(size=(inputlayer_neurons,hiddenlayer_neurons))
bh=np.random.uniform(size=(1,hiddenlayer_neurons))
wout=np.random.uniform(size=(hiddenlayer_neurons,output_neurons))
bout=np.random.uniform(size=(1,output_neurons))
for i in range(epoch):
#Forward Propogation
hinp1=np.dot(X,wh)
hinp=hinp1 + bh
hlayer_act = sigmoid(hinp)
outinp1=np.dot(hlayer_act,wout)
outinp= outinp1+bout
output = sigmoid(outinp)
#Backpropagation
EO = y-output
outgrad = derivatives_sigmoid(output)
d_output = EO * outgrad
EH = d_output.dot(wout.T)
hiddengrad = derivatives_sigmoid(hlayer_act)
d_hiddenlayer = EH * hiddengrad
wh += X.T.dot(d_hiddenlayer) *lr
OUTPUT
Epoch- 1 Starts
Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
Actual Output:
[[0.92]
[0.86]
[0.89]]
Predicted Output:
[[0.71669304]
[0.70551416]
[0.72402119]]
Epoch- 1 Ends
Epoch- 2 Starts
Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
Actual Output:
[[0.92]
[0.86]
[0.89]]
Predicted Output:
[[0.72021397]
[0.70884396]
[0.72759384]]
Epoch- 2 Ends
Epoch- 3 Starts
Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
Actual Output:
[[0.92]
[0.86]
[0.89]]
Predicted Output:
[[0.72361682]
[0.71206417]
[0.7310446 ]]
Epoch- 3 Ends
Epoch- 4 Starts
Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
Actual Output:
[[0.92]
[0.86]
[0.89]]
Predicted Output:
[[0.72690698]
[0.71517975]
[0.73437912]]
Epoch- 4 Ends
Epoch- 5 Starts
Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
Actual Output:
[[0.92]
[0.86]
[0.89]]
Predicted Output:
[[0.73008956]
[0.71819539]
[0.73760274]]
Epoch- 5 Ends
Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
Actual Output:
[[0.92]
[0.86]
[0.89]]
Predicted Output:
[[0.73008956]
[0.71819539]
[0.73760274]]
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.
PROGRAM
import numpy as np
import random
import csv
import pdb
def read_data(filename):
datareader =csv.reader(csvfile)
metadata=next(datareader)
traindata=[]
traindata.append(row)
return(metadata,traindata)
def splitdataset(dataset,splitratio):
trainsize=int(len(dataset)*splitratio)
trainset=[]
testset=list(dataset)
i=0
while len(trainset)<trainsize:
trainset.append(testset.pop(i))
return[trainset,testset]
def classifydata(data,test):
total_size=data.shape[0]
print("\n")
countyes=0
countno=0
probyes=0
probno=0
print("\n")
for x in range(data.shape[0]):
if data[x,data.shape[1]-1]=='yes':
countyes=countyes+1
if data[x,data.shape[1]-1]=='No':
countno=countno+1
probyes=countyes/total_size
probno=countno/total_size
print("yes","\t",countyes,"\t",probyes)
print("no","\t",countno,"\t",probno)
prob0=np.zeros((test.shape[1]-1))
prob1=np.zeros((test.shape[1]-1))
accuracy=0
print("\n")
for t in range(test.shape[0]):
for k in range(test.shape[1]-1):
count1=count0=0
for j in range(data.shape[0]):
count0=count0+1
count1=count1+1
prob0[k]=count0/countno
prob1[k]=count1/countyes
probNo=probno
probYes=probyes
for i in range(test.shape[1]-1):
probNo=probNo*prob0[i]
probYes=probYes*prob1[i]
if probNo>probYes:
predict='no'
else:
predict='yes'
print(t+1,"\t",predict,"\t",test[t,test.shape[1]-1])
if predict==test[t,test.shape[1]-1]:
accuracy+=1
final_accuracy=(accuracy/test.shape[0])*100
print("accuracy",final_accuracy,"%")
return
metadata,traindata=read_data("data3.csv")
splitratio=0.6
trainingset,testset=splitdataset(traindata,splitratio)
training=np.array(trainingset)
for x in trainingset:
print(x)
testing=np.array(testset)
for x in testing:
print(x)
classifydata(training, testing)
DATASET
OUTPUT
attribute names of the traning dta are: ['Outlook', 'Temperature', 'Humidity', 'Wind',
'Target']
yes 4 0.5
no 4 0.5
1 yes yes
2 yes yes
3 yes yes
4 yes yes
5 yes yes
6 yes no
accuracy 83.33333333333334 %
7. Apply EM algorithm to cluster a set of data stored in a .CSV file. Use the same data
set for clustering using the 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.
ALGORITHM
EM Algorithm:
Step 2: Expectation step (E – step): Using the observed available data of the dataset,
estimate (guess) the values of the missing data.
Step 3: Maximization step (M – step): Complete data generated after the expectation
(E) step is used in order to update the parameters.
K- Means Clustering:
Step-2: Select random K points or centroids. (It can be other from the input dataset).
Step-3: Assign each data point to their closest centroid, which will form the
predefined K clusters.
Step-4: Calculate the variance and place a new centroid of each cluster.
Step-5: Repeat the third steps, which mean reassign each data point to the new
closest centroid of each cluster.
PROGRAM
from sklearn.cluster import KMeans
import pandas as pd
import numpy as np
X = dataset.iloc[:, :-1]
plt.figure(figsize=(14,7))
colormap=np.array(['red','lime','black'])
# REAL PLOT
plt.subplot(1,3,1)
plt.title('Real')
plt.scatter(X.Petal_Length,X.Petal_Width,c=colormap[y])
# K-PLOT
model=KMeans(n_clusters=3, random_state=0).fit(X)
plt.subplot(1,3,2)
plt.title('KMeans')
plt.scatter(X.Petal_Length,X.Petal_Width,c=colormap[model.labels_])
# GMM PLOT
gmm=GaussianMixture(n_components=3, random_state=0).fit(X)
y_cluster_gmm=gmm.predict(X)
plt.subplot(1,3,3)
plt.title('GMM Classification')
plt.scatter(X.Petal_Length,X.Petal_Width,c=colormap[y_cluster_gmm])
DATASET (iris.csv total 150 rows in this the attribute names should be
ignored while using it as .csv file)
OUTPUT
The accuracy score of K-Mean: 0.24
[[ 0 50 0]
[48 0 2]
[14 0 36]]
[[50 0 0]
[ 0 5 45]
[ 0 50 0]]
ALGORITHM
K-Nearest Neighbor
Training algorithm:
For each training example (x, f (x)), add the example to the list training
examples
Classification algorithm:
Given a query instance xq to be classified,
Let x1 . . .xk denote the k instances from training examples that are
nearest to xq
Return
PROGRAM
import numpy as np
import pandas as pd
X = dataset.iloc[:, :-1]
y = dataset.iloc[:, -1]
print(X.head())
ypred = classifier.predict(Xtest)
i=0
if (label == ypred[i]):
else:
i=i+1
DATASET (iris.csv)
OUTPUT
sepal-length sepal-width petal-length petal-width
Confusion Matrix:
[[3 0 0]
[0 7 1]
[0 0 4]]
Classification Report:
accuracy 0.93 15
ALGORITHM
1. Read the Given data Sample to X and the curve (linear or non linear) to Y
6. Prediction = x0*β
PROGRAM
import pandas as pd
import numpy as np
m,n = np.shape(xmat)
weights = np.mat(np.eye((m)))
for j in range(m):
weights[j,j] = np.exp(diff*diff.T/(-2.0*k**2))
return weights
wei = kernel(point,xmat,k)
W = (X.T*(wei*X)).I*(X.T*(wei*ymat.T))
return W
m,n = np.shape(xmat)
ypred = np.zeros(m)
for i in range(m):
ypred[i] = xmat[i]*localWeight(xmat[i],xmat,ymat,k)
return ypred
data = pd.read_csv('10-dataset.csv')
bill = np.array(data.total_bill)
tip = np.array(data.tip)
mbill = np.mat(bill)
mtip = np.mat(tip)
m= np.shape(mbill)[1]
one = np.mat(np.ones(m))
X = np.hstack((one.T,mbill.T))
ypred = localWeightRegression(X,mtip,0.5)
SortIndex = X[:,1].argsort(0)
xsort = X[SortIndex][:,0]
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(bill,tip, color='green')
plt.xlabel('Total bill')
plt.ylabel('Tip')
plt.show();
OUTPUT