Lab Programs
Lab Programs
[2]:
#A STAR
class Graph:
self.adjac_lis = adjac_lis
return self.adjac_lis[v]
# This is heuristic function which is having equal values for all nodes
H = {
'A': 1,
'B': 1,
'C': 1,
'D': 1
return H[n]
# In this open_lst is a lisy of nodes which have been visited, but who's
# neighbours haven't all been always inspected, It starts off with the start
#node
open_lst = set([start])
closed_lst = set([])
poo = {}
poo[start] = 0
par = {}
par[start] = start
n = None
for v in open_lst:
n = v;
if n == None:
return None
if n == stop:
reconst_path = []
while par[n] != n:
reconst_path.append(n)
n = par[n]
reconst_path.append(start)
reconst_path.reverse()
return reconst_path
open_lst.add(m)
par[m] = n
else:
par[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 = {
graph1 = Graph(adjac_lis)
graph1.a_star_algorithm('A', 'D')
In [9]:
#AO STAR
class Graph:
self.graph = graph
self.H=heuristicNodeList
self.start=startNode
self.parent={}
self.status={}
self.solutionGraph={}
self.aoStar(self.start, False)
return self.graph.get(v,'')
return self.status.get(v,0)
self.status[v]=val
def printSolution(self):
print("FOR GRAPH SOLUTION, TRAVERSE THE GRAPH FROM THE START NODE:",self.sta
print("------------------------------------------------------------")
print(self.solutionGraph)
print("------------------------------------------------------------")
costToChildNodeListDict={}
costToChildNodeListDict[minimumCost]=[]
flag=True
nodeList=[]
cost=cost+self.getHeuristicNodeValue(c)+weight
nodeList.append(c)
minimumCost=cost
def aoStar(self, v, backTracking): # AO* algorithm for a start node and back
print("---------------------------------------------------------------------
self.setHeuristicNodeValue(v, minimumCost)
self.setStatus(v,len(childNodeList))
self.parent[childNode]=v
if self.getStatus(childNode)!=-1:
h1 = {'A': 1, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 5, 'H': 7, 'I': 7, 'J':
graph1 = {
G1.applyAOStar()
G1.printSolution()
# h2 = {'A': 1, 'B': 6, 'C': 12, 'D': 10, 'E': 4, 'F': 4, 'G': 5, 'H': 7} # Heurist
# graph2 = { # Graph of Nodes and Edges
# 'A': [[('B', 1), ('C', 1)], [('D', 1)]], # Neighbors of Node 'A', B, C &
# 'B': [[('G', 1)], [('H', 1)]], # Neighbors are included in a li
# 'D': [[('E', 1), ('F', 1)]] # Each sublist indicate a "OR" n
# }
HEURISTIC VALUES : {'A': 1, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 5, 'H':
7, 'I': 7, 'J': 1, 'T': 3}
SOLUTION GRAPH : {}
PROCESSING NODE : A
------------------------------------------------------------------------------------
-----
HEURISTIC VALUES : {'A': 10, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 5, 'H':
7, 'I': 7, 'J': 1, 'T': 3}
SOLUTION GRAPH : {}
PROCESSING NODE : B
------------------------------------------------------------------------------------
-----
HEURISTIC VALUES : {'A': 10, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 5, 'H':
7, 'I': 7, 'J': 1, 'T': 3}
SOLUTION GRAPH : {}
PROCESSING NODE : A
------------------------------------------------------------------------------------
-----
HEURISTIC VALUES : {'A': 10, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 5, 'H':
7, 'I': 7, 'J': 1, 'T': 3}
SOLUTION GRAPH : {}
PROCESSING NODE : G
------------------------------------------------------------------------------------
-----
HEURISTIC VALUES : {'A': 10, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 8, 'H':
7, 'I': 7, 'J': 1, 'T': 3}
SOLUTION GRAPH : {}
PROCESSING NODE : B
------------------------------------------------------------------------------------
-----
HEURISTIC VALUES : {'A': 10, 'B': 8, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 8, 'H':
7, 'I': 7, 'J': 1, 'T': 3}
SOLUTION GRAPH : {}
PROCESSING NODE : A
------------------------------------------------------------------------------------
-----
HEURISTIC VALUES : {'A': 12, 'B': 8, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 8, 'H':
7, 'I': 7, 'J': 1, 'T': 3}
SOLUTION GRAPH : {}
PROCESSING NODE : I
------------------------------------------------------------------------------------
-----
HEURISTIC VALUES : {'A': 12, 'B': 8, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 8, 'H':
7, 'I': 0, 'J': 1, 'T': 3}
PROCESSING NODE : G
------------------------------------------------------------------------------------
-----
HEURISTIC VALUES : {'A': 12, 'B': 8, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H':
7, 'I': 0, 'J': 1, 'T': 3}
PROCESSING NODE : B
------------------------------------------------------------------------------------
-----
HEURISTIC VALUES : {'A': 12, 'B': 2, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H':
7, 'I': 0, 'J': 1, 'T': 3}
PROCESSING NODE : A
------------------------------------------------------------------------------------
-----
HEURISTIC VALUES : {'A': 6, 'B': 2, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H':
7, 'I': 0, 'J': 1, 'T': 3}
PROCESSING NODE : C
------------------------------------------------------------------------------------
-----
HEURISTIC VALUES : {'A': 6, 'B': 2, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H':
7, 'I': 0, 'J': 1, 'T': 3}
PROCESSING NODE : A
------------------------------------------------------------------------------------
-----
HEURISTIC VALUES : {'A': 6, 'B': 2, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H':
7, 'I': 0, 'J': 1, 'T': 3}
PROCESSING NODE : J
------------------------------------------------------------------------------------
-----
HEURISTIC VALUES : {'A': 6, 'B': 2, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H':
7, 'I': 0, 'J': 0, 'T': 3}
SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G'], 'J': []}
PROCESSING NODE : C
------------------------------------------------------------------------------------
-----
HEURISTIC VALUES : {'A': 6, 'B': 2, 'C': 1, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H':
7, 'I': 0, 'J': 0, 'T': 3}
SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G'], 'J': [], 'C': ['J']}
PROCESSING NODE : A
------------------------------------------------------------------------------------
-----
FOR GRAPH SOLUTION, TRAVERSE THE GRAPH FROM THE START NODE: A
------------------------------------------------------------
{'I': [], 'G': ['I'], 'B': ['G'], 'J': [], 'C': ['J'], 'A': ['B', 'C']}
------------------------------------------------------------
In [7]:
#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 [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 [3]:
#CANDIDATE ELIMINATION
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 [16]:
#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.08139583] [0.6589966] [0.6590127
1] [0.66893982]
In [13]:
#K-MEANS ALGORITM USING EM ALGORITHM
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.5263157894736842
0.5263157894736842
In [15]:
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))
[[14 0 0]
[ 0 13 0]
[ 0 3 15]]
Accuracy Matrics
accuracy 0.93 45
In [19]:
import matplotlib.pyplot as plt
import pandas as pd
m,n= np1.shape(xmat)
weights = np1.mat(np1.eye((m)))
for j in range(m):
weights[j,j] = np1.exp(diff*diff.T/(-2.0*k**2))
return weights
def localWeight(point,xmat,ymat,k):
wei = kernel(point,xmat,k)
W = (X.T*(wei*X)).I*(X.T*(wei*ymat.T))
return W
def localWeightRegression(xmat,ymat,k):
m,n = np1.shape(xmat)
ypred = np1.zeros(m)
for i in range(m):
ypred[i] = xmat[i]*localWeight(xmat[i],xmat,ymat,k)
return ypred
data = pd.read_csv('prg9tips.csv')
bill = np1.array(data.total_bill)
tip = np1.array(data.tip)
mbill = np1.mat(bill)
mtip = np1.mat(tip)
m= np1.shape(mbill)[1]
print("******",m)
one = np1.mat(np1.ones(m))
X= np1.hstack((one.T,mbill.T))
ypred = localWeightRegression(X,mtip,2)
SortIndex = X[:,1].argsort(0)
xsort = X[SortIndex][:,0]
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(bill,tip, color='blue')
plt.xlabel('Total bill')
plt.ylabel('Tip')
plt.show();
---------------------------------------------------------------------------
~\AppData\Local\Temp/ipykernel_3268/3362905836.py in <module>
21 ypred[i] = xmat[i]*localWeight(xmat[i],xmat,ymat,k)
22 return ypred
24 bill = np1.array(data.total_bill)
25 tip = np1.array(data.tip)
309 stacklevel=stacklevel,
310 )
312
~\Anaconda3\lib\site-packages\pandas\io\parsers\readers.py in read_csv(filepath_or_b
uffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_du
pe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, ski
prows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank
_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, ca
che_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quo
techar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialec
t, error_bad_lines, warn_bad_lines, on_bad_lines, delim_whitespace, low_memory, memo
ry_map, float_precision, storage_options)
584 kwds.update(kwds_defaults)
585
587
588
~\Anaconda3\lib\site-packages\pandas\io\parsers\readers.py in _read(filepath_or_buff
er, kwds)
480
483
810
812
1038 )
1041
~\Anaconda3\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py in __init__(sel
f, src, **kwds)
49
50 # open handles
53
~\Anaconda3\lib\site-packages\pandas\io\parsers\base_parser.py in _open_handles(sel
f, src, kwds)
220 Let the readers open IOHandles after they are done with their potent
ial raises.
221 """
223 src,
224 "r",
701 # Encoding
704 ioargs.mode,
In [ ]: