AI and ML LAB
AI and ML LAB
AI and ML LAB
[1]:
class Graph:
def __init__(self,adjac_lis):
self.adjac_lis = adjac_lis
def get_neighbours(self,v):
return self.adjac_lis[v]
def h(self,n):
H={'A':1,'B':1, 'C':1,'D':1}
return H[n]
def a_star_algorithm(self,start,stop):
open_lst = set([start])
closed_lst = set([])
dist ={}
dist[start] = 0
prenode ={}
prenode[start] =start
while len(open_lst)>0:
n = None
for v in open_lst:
if n==None or dist[v]+self.h(v)<dist[n]+self.h(n):
n=v;
if n==None:
return None
if n==stop:
reconst_path=[]
while prenode[n]!=n:
reconst_path.append(n)
n = prenode[n]
reconst_path.append(start)
reconst_path.reverse()
print("path found:{}".format(reconst_path))
return reconst_path
open_lst.add(m)
prenode[m] = n
dist[m] = dist[n]+weight
else:
if dist[m]>dist[n]+weight:
dist[m] = dist[n]+weight
prenode[m]=n
if m in closed_lst:
closed_lst.remove(m)
open_lst.add(m)
open_lst.remove(n)
closed_lst.add(n)
return None
adjac_lis ={'A':[('B',1),('C',3),('D',7)],'B':[('D',5)],'C':[('D',12)]}
graph1=Graph(adjac_lis)
graph1.a_star_algorithm('A', 'D')
In [2]:
def recAOStar(n):
global finalPath
print("Expanding Node:",n)
and_nodes = []
or_nodes =[]
if(n in allNodes):
if 'AND' in allNodes[n]:
and_nodes = allNodes[n]['AND']
if 'OR' in allNodes[n]:
or_nodes = allNodes[n]['OR']
if len(and_nodes)==0 and len(or_nodes)==0:
return
solvable = False
marked ={}
if len(marked)==len(and_nodes)+len(or_nodes):
min_cost_least,min_cost_group_least = least_cost_group(and_nodes,or_node
solvable = True
change_heuristic(n,min_cost_least)
optimal_child_group[n] = min_cost_group_least
continue
min_cost,min_cost_group = least_cost_group(and_nodes,or_nodes,marked)
is_expanded = False
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 is_expanded:
solvable = True
change_heuristic(n, min_cost_verify)
optimal_child_group[n] = min_cost_group
else:
solvable = True
change_heuristic(n, min_cost)
optimal_child_group[n] = min_cost_group
marked[min_cost_group]=1
return heuristic(n)
node_wise_cost = {}
cost = 0
cost = 0
node_wise_cost[node] = cost
min_cost = 999999
min_cost_group = None
min_cost = node_wise_cost[costKey]
min_cost_group = costKey
def heuristic(n):
return H_dist[n]
H_dist[n] = cost
return
def print_path(node):
print(optimal_child_group[node], end="")
node = optimal_child_group[node]
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)
H_dist = {
'A': -1,
'B': 4,
'C': 2,
'D': 3,
'E': 6,
'F': 8,
'G': 2,
'H': 0,
'I': 0,
'J': 0
allNodes = {
optimal_child_group = {}
optimal_cost = recAOStar('A')
print_path('A')
Expanding Node: A
Expanding Node: B
Expanding Node: C
Expanding Node: D
Optimal Cost is :: 5
In [4]:
import csv
with open("ws.csv") as f:
csv_file=csv.reader(f)
data=list(csv_file)
s=data[0][:-1]
for i in range(len(s)):
for j in range(len(s)):
g[i][j]='?'
for i in data:
if i[-1]=="Yes":
for j in range(len(s)):
if i[j]!=s[j]:
s[j]='?'
g[j][j]='?'
elif i[-1]=="No":
for j in range(len(s)):
if i[j]!=s[j]:
g[j][j]=s[j]
else:
g[j][j]="?"
print(s)
print(g)
gh=[]
for i in g:
for j in i:
if j!='?':
gh.append(i)
break
[['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?',
'?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'],
['?', '?', '?', '?', '?', '?']]
[['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?',
'?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'],
['?', '?', '?', '?', '?', '?']]
[['Sunny', '?', '?', '?', '?', '?'], ['?', 'Warm', '?', '?', '?', '?'], ['?', '?',
'?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'],
['?', '?', '?', '?', '?', 'Same']]
[['Sunny', '?', '?', '?', '?', '?'], ['?', 'Warm', '?', '?', '?', '?'], ['?', '?',
'?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'],
['?', '?', '?', '?', '?', '?']]
[['Sunny', '?', '?', '?', '?', '?'], ['?', 'Warm', '?', '?', '?', '?']]
In [5]:
#ID3 ALGORITHM
import pandas as pd
import math
import numpy as np
data = pd.read_csv("3-dataset.csv")
features.remove("answer")
class Node:
def __init__(self):
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])
#print ("\n",uniq)
gain = entropy(examples)
#print ("\n",gain)
for u in uniq:
subdata = examples[examples[attr] == u]
#print ("\n",subdata)
sub_e = entropy(subdata)
#print ("\n",gain)
return gain
root = Node()
max_gain = 0
max_feat = ""
#print ("\n",examples)
max_gain = gain
max_feat = feature
root.value = max_feat
uniq = np.unique(examples[max_feat])
#print ("\n",uniq)
for u in uniq:
#print ("\n",u)
subdata = examples[examples[max_feat] == u]
#print ("\n",subdata)
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)
outlook
rain
wind
sunny
humidity
In [7]:
#BACKPROPAGATION ALGORITHM
import numpy as np
def sigmoid_derivative(x):
return x * (1 - x)
inputs = np.array([[0,0],[0,1],[1,0],[1,1]])
expected_output = np.array([[0],[1],[1],[0]])
epochs = 10000
lr = 0.1
hidden_weights = np.random.uniform(size=(inputLayerNeurons,hiddenLayerNeurons))
hidden_bias =np.random.uniform(size=(1,hiddenLayerNeurons))
output_weights = np.random.uniform(size=(hiddenLayerNeurons,outputLayerNeurons))
output_bias = np.random.uniform(size=(1,outputLayerNeurons))
print(*hidden_weights)
print(*hidden_bias)
print(*output_weights)
print(*output_bias)
for _ in range(epochs):
hidden_layer_activation = np.dot(inputs,hidden_weights)
hidden_layer_activation += hidden_bias
hidden_layer_output = sigmoid(hidden_layer_activation)
output_layer_activation = np.dot(hidden_layer_output,output_weights)
output_layer_activation += output_bias
predicted_output = sigmoid(output_layer_activation)
error_hidden_layer = d_predicted_output.dot(output_weights.T)
output_weights += hidden_layer_output.T.dot(d_predicted_output) * lr
output_bias += np.sum(d_predicted_output,axis=0,keepdims=True) * lr
hidden_weights += inputs.T.dot(d_hidden_layer) * lr
hidden_bias += np.sum(d_hidden_layer,axis=0,keepdims=True) * lr
print(*hidden_weights)
print(*hidden_bias)
print(*output_weights)
print(*output_bias)
print(*predicted_output)
Output from neural network after 10,000 epochs: [0.14322429] [0.825314] [0.82306587]
[0.21951972]
In [8]:
#NAVIE BAYESIAN CLASSIFIER
import pandas as pd
DB = pd.read_csv('tennis1.csv')
print(DB.columns)
len(DB)
DB.head(3)
X = DB.values[:,0:4] #Features
Y = DB.values[:,4] #Target
X_train,X_test,Y_train,Y_test = train_test_split(X,Y,test_size=0.30,random_state=10)
#implement Gaussian Naive Bayes
clf = GaussianNB()
clf.fit(X_train,Y_train)
Y_pred = clf.predict(X_test)
accuracy_score(Y_test,Y_pred,normalize=True)
1.0
Out[8]:
In [9]:
from sklearn import datasets
iris = datasets.load_iris()
X_train,X_test,Y_train,Y_test = train_test_split(iris.data,iris.target)
model = KMeans(n_clusters=3)
model.fit(X_train,Y_train)
model.score
acc1=metrics.accuracy_score(Y_test,model.predict(X_test))
print(acc1)
model2 = GaussianMixture(n_components=3)
model2.fit(X_train,Y_train)
model2.score
metrics
acc2=metrics.accuracy_score(Y_test,model.predict(X_test))
print(acc2)
0.5526315789473685
0.5526315789473685
In [10]:
from sklearn.model_selection import train_test_split
iris=datasets.load_iris()
iris_data=iris.data
iris_labels=iris.target
x_train,x_test,y_train,y_test=train_test_split(iris_data,iris_labels,test_size=0.30)
classifier=KNeighborsClassifier(n_neighbors=5)
classifier.fit(x_train,y_train)
y_pred=classifier.predict(x_test)
print(confusion_matrix(y_test,y_pred))
print('Accuracy Matrics')
print(classification_report(y_test,y_pred))
[[16 0 0]
[ 0 14 0]
[ 0 0 15]]
Accuracy Matrics
accuracy 1.00 45
In [11]:
from math import ceil
import numpy as np
n = len(x)
r = int(ceil(f * n))
w = (1 - w ** 3) ** 3
yest = np.zeros(n)
delta = np.ones(n)
for i in range(n):
residuals = y - yest
s = np.median(np.abs(residuals))
delta = (1 - delta ** 2) ** 2
return yest
import math
n = 100
x = np.linspace(0, 2 * math.pi, n)
f =0.25
iterations=3
plt.plot(x,y,"r.")
plt.plot(x,yest,"b-")
[<matplotlib.lines.Line2D at 0x171f4daeaf0>]
Out[11]:
In [ ]: