Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

1.) Implement A* Search algorithm.

def astarAlgo(start_node,stop_node):
open_set = set(start_node)
closed_set = set()
g = {}
parents = {}
g[start_node] = 0
parents[start_node] = start_node

while len(open_set) > 0:


n = None
for v in open_set:
if n == None or g[v] + heuristic(v) < g[n] +
heuristic(n):
n = v
if n == stop_node or Graphnodes[n] == None:
pass
else:
for(m,weight) in get_neighbors(n):
if m not in open_set and m not in closed_set:
open_set.add(m)
parents[m] = n
g[m] = g[n] + weight
else:
if g[m] > g[n] + weight:
g[m] = g[n] + weight
parents[m] = n
if m in closed_set:
closed_set.remove(m)
open_set.add(m)
if n == None:
print("Path does not exist")
return None
if n == stop_node:
path = []
while parents[n] != n:
path.append(n)
n = parents[n]
path.append(start_node)
path.reverse()
print("Path found {}".format(path))
return path
open_set.remove(n)
closed_set.add(n)
print("Path does not exist")
return None

def get_neighbors(v):
if v in Graphnodes:
return Graphnodes[v]
else:
return None
def heuristic(n):
H_dist = {
'A' : 10,
'B' : 8,
'C' : 5,
'D' : 7,
'E' : 3,
'F' : 6,
'G' : 5,
'H' : 3,
'I' : 1,
'J' : 0
}
return H_dist[n]
Graphnodes = {
'A' : [('B',6),('F',3)],
'B' : [('D',2),('C',3)],
'C' : [('D',1),('E',5)],
'D' : [('C',1),('E',8)],
'E' : [('I',5),('J',5)],
'F' : [('G',1),('H',7)],
'G' : [('I',3)],
'H' : [('I',2)],
'I' : [('E',5),('J',3)],
}
astarAlgo('A','J')

OUTPUT :

Path found [‘A’,’F’,’G’,’I’,’J’]


2.) Implement AO* Search algorithm.

class Graph:
def __init__(self, graph, heuristicNodeList, startNode):
self.graph = graph
self.H=heuristicNodeList
self.start=startNode
self.parent={}
self.status={}
self.solutionGraph={}

def applyAOStar(self):
self.aoStar(self.start, False)

def getNeighbors(self, v):


return self.graph.get(v,'')

def getStatus(self,v):
return self.status.get(v,0)

def setStatus(self,v, val):


self.status[v]=val

def getHeuristicNodeValue(self, n):


return self.H.get(n,0)

def setHeuristicNodeValue(self, n, value):


self.H[n]=value
def printSolution(self):
print("FOR GRAPH SOLUTION, TRAVERSE THE GRAPH FROM THE
STARTNODE:",self.start)
print("---------------------------------------------")
print(self.solutionGraph)
print("--------------------------------------------")

def computeMinimumCostChildNodes(self, v):


minimumCost=0
costToChildNodeListDict={}
costToChildNodeListDict[minimumCost]=[]
flag=True
for nodeInfoTupleList in self.getNeighbors(v):
cost=0
nodeList=[]
for c, weight in nodeInfoTupleList:
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
return minimumCost,
costToChildNodeListDict[minimumCost]

def aoStar(self, v, backTracking):

print("HEURISTIC VALUES :", self.H)


print("SOLUTION GRAPH :", self.solutionGraph)
print("PROCESSING NODE :", v)

print("---------------------------------------------")

if self.getStatus(v) >= 0:
minimumCost, childNodeList =
self.computeMinimumCostChildNodes(v)
self.setHeuristicNodeValue(v, minimumCost)
self.setStatus(v,len(childNodeList))

solved=True

for childNode in childNodeList:


self.parent[childNode]=v
if self.getStatus(childNode)!=-1:
solved=solved & False
if solved==True:
self.setStatus(v,-1)
self.solutionGraph[v]=childNodeList

if v!=self.start:
self.aoStar(self.parent[v], True)

if backTracking==False:
for childNode in childNodeList:
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, 'T': 3}
graph1 = {
'A': [[('B', 1), ('C', 1)], [('D', 1)]],
'B': [[('G', 1)], [('H', 1)]],
'C': [[('J', 1)]],
'D': [[('E', 1), ('F', 1)]],
'G': [[('I', 1)]]
}
G1= Graph(graph1, h1, 'A')
G1.applyAOStar()
G1.printSolution()

h2 = {'A': 1, 'B': 6, 'C': 12, 'D': 10, 'E': 4, 'F': 4, 'G':


5, 'H': 7}
graph2 = {
'A': [[('B', 1), ('C', 1)], [('D', 1)]],
'B': [[('G', 1)], [('H', 1)]],
'D': [[('E', 1), ('F', 1)]]
}

G2 = Graph(graph2, h2, 'A')


G2.applyAOStar()
G2.printSolution()

OUTPUT :

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}
Solution Graph : {'I': []}
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}
Solution Graph : {'I': [], 'G': ['I']}
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}
Solution Graph : {'I': [], 'G': ['I'], 'B': ['G']}
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}
Solution Graph : {'I': [], 'G': ['I'], 'B': ['G']}
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}
Solution Graph : {'I': [], 'G': ['I'], 'B': ['G']}
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}
Solution Graph : {'I': [], 'G': ['I'], 'B': ['G']}
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 Results Traverse the graph from start node A
-----------------------------------------------------
{'I': [], 'G': ['I'], 'B': ['G'], 'J': [], 'C': ['J'], 'A':
['B', 'C']}
-----------------------------------------------------
Heuristic values : {'A': 1, 'B': 6, 'C': 12, 'D': 10, 'E': 4,
'F': 4, 'G': 5, 'H': 7}
Solution Graph : {}
Processing Node : A
---------------------------------------------------------
Heuristic values : {'A': 11, 'B': 6, 'C': 12, 'D': 10, 'E':
4, 'F': 4, 'G': 5, 'H': 7}
Solution Graph : {}
Processing Node : D
---------------------------------------------------------
Heuristic values : {'A': 11, 'B': 6, 'C': 12, 'D': 10, 'E':
4, 'F': 4, 'G': 5, 'H': 7}
Solution Graph : {}
Processing Node : A
---------------------------------------------------------
Heuristic values : {'A': 11, 'B': 6, 'C': 12, 'D': 10, 'E':
4, 'F': 4, 'G': 5, 'H': 7}
Solution Graph : {}
Processing Node : E
---------------------------------------------------------
Heuristic values : {'A': 11, 'B': 6, 'C': 12, 'D': 10, 'E':
0, 'F': 4, 'G': 5, 'H': 7}
Solution Graph : {'E': []}
Processing Node : D
---------------------------------------------------------
Heuristic values : {'A': 11, 'B': 6, 'C': 12, 'D': 6, 'E': 0,
'F': 4, 'G': 5, 'H': 7}
Solution Graph : {'E': []}
Processing Node : A
---------------------------------------------------------
Heuristic values : {'A': 7, 'B': 6, 'C': 12, 'D': 6, 'E': 0,
'F': 4, 'G': 5, 'H': 7}
Solution Graph : {'E': []}
Processing Node : F
---------------------------------------------------------
Heuristic values : {'A': 7, 'B': 6, 'C': 12, 'D': 6, 'E': 0,
'F': 0, 'G': 5, 'H': 7}
Solution Graph : {'E': [], 'F': []}
Processing Node : D
---------------------------------------------------------
Heuristic values : {'A': 7, 'B': 6, 'C': 12, 'D': 2, 'E': 0,
'F': 0, 'G': 5, 'H': 7}
Solution Graph : {'E': [], 'F': [], 'D': ['E', 'F']}
Processing Node : A
---------------------------------------------------------
For Graph Results Traverse the graph from start node A
-----------------------------------------------------
{'E': [], 'F': [], 'D': ['E', 'F'], 'A': ['D']}
-----------------------------------------------------
3.) Candidate-Elimination algorithm
import numpy as np
import pandas as pd
data = pd.DataFrame(data =
pd.read_csv('C:/Users/Lannister/Desktop/Ai & Ml/prg3.csv'))
concepts = np.array(data.iloc[:,0:-1])
target = np.array(data.iloc[:,-1])
def learn(concepts,target):
specific_h = concepts[0].copy()
general_h = [['?' for i in range(len(specific_h))] for i
in range(len(specific_h))]
for i,h in enumerate(concepts):
if target[i] == 'Y':
for x in range(len(specific_h)):
if h[x] != specific_h[x]:
specific_h[x] = '?'
general_h[x][x] = '?'
if target[i] == 'N':
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] = '?'
indices = [i for i,val in enumerate(general_h) if val ==
['?','?','?','?','?','?']]
for i in indices:
general_h.remove(['?','?','?','?','?','?'])
return specific_h,general_h

s_final,g_final = learn(concepts,target)
print("Final specific_h :",s_final,sep = "\n")
print("Final general_h :",g_final,sep = "\n")

OUTPUT :

Final specific_h :
['Sunny' 'Warm' '?' 'Strong' '?' '?']
Final general_h :
[['Sunny', '?', '?', '?', '?', '?'], ['?', 'Warm', '?', '?',
'?', '?']]
4.) ID3 algorithm.

import pandas as pd

df_tennis = pd.read_csv("prgm4.csv")
print("\n Given play tennis data set:\n\n",df_tennis)

def entropy(probs):
import math
return sum([-prob*math.log(prob,2) for prob in probs])

def entropy_of_list(a_list):
from collections import Counter
cnt = Counter(x for x in a_list)
num_instances = len(a_list)
probs = [x/num_instances for x in cnt.values()]
return entropy(probs)
total_entropy = entropy_of_list(df_tennis['PlayTennis'])
print("Total entropy of play tennis dataset:", total_entropy)

def information_gain(df,split_attribute_name,
target_attribute_name, trace=0):
df_split = df.groupby(split_attribute_name)
for name, group in df_split:
nobs = len(df.index)
df_agg_cnt =
df_split.agg({target_attribute_name:[entropy_of_list,lambda
x:len(x)/nobs]})[target_attribute_name]
df_agg_cnt.columns = ['Entropy','Propobservations']
if trace:
print(df_agg_cnt)
new_entropy =
sum(df_agg_cnt['Entropy']*df_agg_cnt['Propobservations'])
old_entropy = entropy_of_list(df[target_attribute_name])
return old_entropy-new_entropy

print('info-gain for Outlook is:


'+str(information_gain(df_tennis,
'Outlook','PlayTennis')),"\n")
print('info-gain for Humidity is:
'+str(information_gain(df_tennis,'Humidity','PlayTennis')),"\n
")
print('info-gain for Wind is:
'+str(information_gain(df_tennis,'Wind','PlayTennis')),"\n")
print('info-gain for Temperature is:
'+str(information_gain(df_tennis,'Temperature','PlayTennis')),
"\n")

def id3(df,target_attribute_name, attribute_names,


default_class=None):
from collections import Counter
cnt = Counter(x for x in df[target_attribute_name])
if len(cnt)==1:
return next(iter(cnt))
elif df.empty or (not attribute_names):
return default_class
else:
default_class = max(cnt.keys())
gainz = [information_gain(df,attr,target_attribute_name)
for attr in attribute_names]
index_of_max = gainz.index(max(gainz))
best_attr = attribute_names[index_of_max]
tree = {best_attr:{}}
remaining_attribute_names=[i for i in attribute_names if
i!=best_attr]
for attr_val, data_subset in df.groupby(best_attr):
subtree = id3(data_subset,
target_attribute_name,remaining_attribute_names,default_class)
tree[best_attr][attr_val] = subtree
return tree
attribute_names = list(df_tennis.columns)
attribute_names.remove('PlayTennis')
from pprint import pprint
tree = id3(df_tennis,'PlayTennis',attribute_names)
print("\n\nThe resultant Decision Tree is:\n")
pprint(tree)

def predict(query,tree,default=1):
for key in list(query.keys()):
if key in list(tree.keys()):
try:
result = tree[key][query[key]]
except:
return default
result = tree[key][query[key]]
if isinstance(result,dict):
return predict(query,result)
else:
return result

query =
{'Outlook':'sunny','Temperature':'hot','Humidity':'high','wind
':'weak'}
answer = predict(query,tree)
print('\n Can tennis be played for the given sample: ' +answer
OUTPUT :

Given play tennis data set:

Outlook Temperature Humidity Wind PlayTennis


0 sunny hot high weak no
1 sunny hot high strong no
2 overcast hot high weak yes
3 rain mild high weak yes
4 rain cool normal weak yes
5 rain cool normal strong no
6 overcast cool normal strong yes
7 sunny mild high weak no
8 sunny cool normal weak yes
9 rain mild normal weak yes
10 sunny mild normal strong yes
11 overcast mild high strong yes
12 overcast hot normal weak yes
13 rain mild high strong no
Total entropy of play tennis dataset: 0.9402859586706309
info-gain for Outlook is: 0.2467498197744391
info-gain for Humidity is: 0.15183550136234136
info-gain for Wind is: 0.04812703040826927
info-gain for Temperature is: 0.029222565658954647
The resultant Decision Tree is:
{'Outlook': {'overcast': 'yes', 'rain': {'Wind': {'strong':
'no', 'weak': 'yes'}},
'sunny': {'Humidity': {'high': 'no', 'normal': 'yes'}}}}
5.) Backpropagation algorithm

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)
y = y/100

def sigmoid(x):
return 1/(1+np.exp(-x))
def derivatives_sigmoid(x):
return x*(1-x)
epoch = 5000
lr = 0.1
inputlayer_neurons = 2
hiddenlayer_neurons = 3
output_neurons = 1

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):
hinp1 = np.dot(x,wh)
hinp = hinp1 + bh
hlayer_act = sigmoid(hinp)
outinp1 = np.dot(hlayer_act,wout)
outinp = outinp1 + bout
output = sigmoid(outinp)

EO = y - output
outgrad = derivatives_sigmoid(output)
d_output = EO * outgrad
EH = d_output.dot(wout.T)
hiddengrad = derivatives_sigmoid(hlayer_act)
d_hidden = EH * hiddengrad
wout += hlayer_act.T.dot(d_output) * lr
wh += x.T.dot(d_hidden) * lr

print("Input :\n" + str(x))


print("Actual output :\n" + str(y))
print("Predicted output :\n",output)
OUTPUT :
Input :
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
Actual output :
[[0.92]
[0.86]
[0.89]]
Predicted output :
[[0.89611258]
[0.87930992]
[0.89433858]]
6.) naïve Bayesian classifier
from sklearn.naive_bayes import GaussianNB
from sklearn.datasets import load_iris
from sklearn import metrics
from sklearn.model_selection import train_test_split as split

dataset = load_iris()
x = dataset.data
y = dataset.target
x_train,x_test,y_train,y_test = split(x,y,test_size = 0.2,
random_state = 1)
gnb = GaussianNB()
classifier = gnb.fit(x_train,y_train)
y_pred = classifier.predict(x_test)

print("Accuracy Metrices
:",metrics.classification_report(y_test,y_pred))
print("The Acccuracy of Metrices is
:",metrics.accuracy_score(y_test,y_pred))
print("Confusion Matrix")
print(metrics.confusion_matrix(y_test,y_pred))

OUTPUT :

Accuracy Metrices : precision recall f1-score support

0 1.00 1.00 1.00 11


1 1.00 0.92 0.96 13
2 0.86 1.00 0.92 6

accuracy 0.97 30
macro avg 0.95 0.97 0.96 30
weighted avg 0.97 0.97 0.97 30
The Acccuracy of Metrices is : 0.9666666666666667
Confusion Matrix
[[11 0 0]
[ 0 12 1]
[ 0 0 6]]
7.)Apply EM algorithm and k-Means algorithm.
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.cluster import KMeans
import numpy as np
import pandas as pd

iris = datasets.load_iris()
x = pd.DataFrame(iris.data)
x.columns =
['sepal_length','sepal_width','petal_length','petal_width']
y = pd.DataFrame(iris.target)
y.columns = ['Targets']

model = KMeans(n_clusters = 3)
model.fit(x)

plt.figure(figsize=(14,14))
colormap = np.array(['red','lime','black'])

plt.subplot(2,2,1)
plt.scatter(x.petal_length,x.petal_width,c =
colormap[y.Targets],s=40)
plt.title('Real Clusters')
plt.xlabel('Petal Length')
plt.ylabel('Petal Width')
plt.show()

plt.subplot(2,2,2)
plt.scatter(x.petal_length,x.petal_width,c =
colormap[model.labels_],s=40)
plt.title('KMeans Clusters')
plt.xlabel('Petal Length')
plt.ylabel('Petal Width')
plt.show()

from sklearn import preprocessing


scaler = preprocessing.StandardScaler()
scaler.fit(x)
xsa = scaler.transform(x)
xs = pd.DataFrame(xsa,columns = x.columns)

from sklearn.mixture import GaussianMixture


gmm = GaussianMixture()
gmm.fit(xs)
gmm_y = gmm.predict(xs)

plt.subplot(2,2,1)
plt.scatter(x.petal_length,x.petal_width,c =
colormap[gmm_y],s=40)
plt.title('GMM Clusters')
plt.xlabel('Petal Length')
plt.ylabel('Petal Width')
plt.show()
print("The GMM using EM algorithm based clustering matched the
true labels closely than KMeans clusters")

OUTPUT :

The GMM using EM algorithm based clustering matched the true


labels closely than KMeans clusters.
8.) k-Nearest Neighbour algorithm
from sklearn.neighbors import KNeighborsClassifier
from sklearn import datasets
from sklearn.model_selection import train_test_split

iris = datasets.load_iris()
print("Iris dataset is loaded...")

x_train,x_test,y_train,y_test =
train_test_split(iris.data,iris.target,test_size = 0.1)
print("Dataset is split into training and testing..")
print("Size of training data and it's
label",x_train.shape,y_train.shape)
print("Size of test data and it's
label",x_test.shape,y_test.shape)

for i in range(len(iris.target_names)):
print("Label",i,"-",str(iris.target_names[i]))

classifier = KNeighborsClassifier(n_neighbors = 1)
classifier.fit(x_train,y_train)
y_pred = classifier.predict(x_test)

print("Results of classification usin knn with k=1")


for r in range(0,len(x_test)):
print("Sample :",str(x_test[r]),"Actual -
Label",str(y_test[r]),"Predicted - Label",str(y_pred[r]))
print("Classifictaion Accuracy
:",classifier.score(x_test,y_test))

from sklearn.metrics import


classification_report,confusion_matrix
print("Accuracy Metrices :")
print(classification_report(y_test,y_pred))
print("Confusion Matrix :")
print(confusion_matrix(y_test,y_pred))

OUTPUT :

Iris dataset is loaded...


Dataset is split into training and testing..
Size of training data and it's label (135, 4) (135,)
Size of test data and it's label (15, 4) (15,)
Label 0 - setosa
Label 1 - versicolor
Label 2 - virginica
Results of classification usin knn with k=1
Sample : [5.4 3.4 1.7 0.2] Actual - Label 0 Predicted - Label
0
Sample : [6.9 3.1 4.9 1.5] Actual - Label 1 Predicted - Label
1
Sample : [5. 3.5 1.3 0.3] Actual - Label 0 Predicted - Label
0
Sample : [6.1 2.6 5.6 1.4] Actual - Label 2 Predicted - Label
2
Sample : [6.7 2.5 5.8 1.8] Actual - Label 2 Predicted - Label
2
Sample : [6.3 2.3 4.4 1.3] Actual - Label 1 Predicted - Label
1
Sample : [4.9 3.1 1.5 0.1] Actual - Label 0 Predicted - Label
0
Sample : [5.2 2.7 3.9 1.4] Actual - Label 1 Predicted - Label
1
Sample : [5.1 2.5 3. 1.1] Actual - Label 1 Predicted - Label
1
Sample : [5.6 2.8 4.9 2. ] Actual - Label 2 Predicted - Label
2
Sample : [5.8 2.7 4.1 1. ] Actual - Label 1 Predicted - Label
1
Sample : [4.8 3. 1.4 0.1] Actual - Label 0 Predicted - Label
0
Sample : [5.4 3.7 1.5 0.2] Actual - Label 0 Predicted - Label
0
Sample : [5.4 3.9 1.7 0.4] Actual - Label 0 Predicted - Label
0
Sample : [6. 2.2 4. 1. ] Actual - Label 1 Predicted - Label
1
Classifictaion Accuracy : 1.0
Accuracy Metrices :
precision recall f1-score support

0 1.00 1.00 1.00 6


1 1.00 1.00 1.00 6
2 1.00 1.00 1.00 3

accuracy 1.00 15
macro avg 1.00 1.00 1.00 15
weighted avg 1.00 1.00 1.00 15

Confusion Matrix :
[[6 0 0]
[0 6 0]
[0 0 3]]
9.) Locally Weighted Regression algorithm

import matplotlib.pyplot as plt


import numpy as np
import pandas as pd

def kernel(point,xmat,k):
m,n = np.shape(xmat)
weights = np.mat(np.eye(m))
for j in range(m):
diff = point - X[j]
weights[j,j] = np.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 = np.shape(xmat)
ypred = np.zeros(m)
for i in range(m):
ypred[i] = xmat[i] * localweight(xmat[i],xmat,ymat,k)
return ypred

def GraphPlot(X,ypred):
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')
ax.plot(xsort[:,1],ypred[sortindex],color =
'red',linewidth = 5)
plt.xlabel('Total Bill')
plt.ylabel('Tip')
plt.show()

data = pd.read_csv('C:/Users/Lannister/Desktop/Ai &


Ml/prog10.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,3)
GraphPlot(X,ypred)
OUTPUT :

You might also like