Lab
Lab
ipynb - Colaboratory
KNN
#import libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
#load iris dataset
from sklearn.datasets import load_iris
iris=load_iris()
X=iris.data # features
y=iris.target # target
#compare the accuracy between the predicted and the actual output y_test
from sklearn import metrics
from sklearn.metrics import classification_report, confusion_matrix
print("accuracy=",metrics.accuracy_score(y_pred,y_test))
print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))
targets ={0:'setosa',1:'versicolor',2:'virginica'}
print(f'ID \t {"Actual":10s} \t\t {"Predicted":10s} \
\t Correct?')
i=1
for yt,yp in zip (y_test, y_pred):
correct='yes' if yt==yp else 'no**'
print(f'{i} \t{targets.get(yt):10s} \t \
{targets.get(yp):10s} \t {correct}')
i+=1
(150, 4)
Training Set 120
Test Set 30
[2 2 1 0 2 1 0 2 0 1 1 0 2 0 0 1 1 1 2 0 2 0 0 0 2 0 0 2 1 1]
accuracy= 0.9666666666666667
[[12 0 0]
[ 0 8 0]
[ 0 1 9]]
precision recall f1-score support
accuracy 0.97 30
macro avg 0.96 0.97 0.96 30
weighted avg 0.97 0.97 0.97 30
https://colab.research.google.com/drive/1skhcjCHuObahJ_ZwztzAws7ytFltLMQv#scrollTo=hqRNfR7CBd3K&printMode=true 1/9
1/8/24, 6:50 AM KNN.ipynb - Colaboratory
10 versicolor versicolor yes
11 versicolor versicolor yes
12 setosa setosa yes
13 virginica virginica yes
14 setosa setosa yes
15 setosa setosa yes
16 virginica versicolor no**
17 versicolor versicolor yes
18 versicolor versicolor yes
19 virginica virginica yes
20 setosa setosa yes
21 virginica virginica yes
22 setosa setosa yes
23 setosa setosa yes
24 setosa setosa yes
25 virginica virginica yes
26 setosa setosa yes
27 setosa setosa yes
28 virginica virginica yes
29 versicolor versicolor yes
30 versicolor versicolor yes
LWR
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
plt.style.use("seaborn")
yest = np.zeros(len(xtest))
for i in range(len(xtest)):
weights= np.array([np.exp(- (x - xtest[i])**2/(2*tau**2))])
b = np.array([np.sum(weights * y), np.sum(weights * y * x)])
A = np.array([[np.sum(weights), np.sum(weights * x)],[np.sum(weights * x), np.sum(weights * x * x)]])
theta = linalg.solve(A, b)
yest[i] = theta[0] + theta[1] * xtest[i]
return yest
tau=0.05
#xtest = np.array([0.2,0.3, 0.5,0.7,0.8])
xtest = np.linspace(0,1,50)
yest_bell = lwr(x,y, xtest, tau)
print(len(xtest), len(yest_bell))
plt.figure(figsize=(10,6))
#plt.plot(x,y,color = 'darkblue', label = 'sin()')
plt.scatter(x,y_noise, facecolors = 'none', edgecolor = 'darkblue', label = 'sin() + noise')
plt.fill(x,kernel_function(x,0.2,tau), color = 'lime', alpha = .5, label = 'Kernel')
plt.scatter(xtest,yest_bell,color = 'red', label = 'Loess: bell shape kernel')
plt.legend()
plt.title('Sine with noise: Loess regression and bell shaped kernel')
plt.show()
https://colab.research.google.com/drive/1skhcjCHuObahJ_ZwztzAws7ytFltLMQv#scrollTo=hqRNfR7CBd3K&printMode=true 2/9
1/8/24, 6:50 AM KNN.ipynb - Colaboratory
50 50
EM Algorithm
https://colab.research.google.com/drive/1skhcjCHuObahJ_ZwztzAws7ytFltLMQv#scrollTo=hqRNfR7CBd3K&printMode=true 3/9
1/8/24, 6:50 AM KNN.ipynb - Colaboratory
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.cluster import KMeans
import sklearn.metrics as sm
import pandas as pd
import numpy as np
#%matplotlib inline
# Create a colormap
colormap = np.array(['red', 'lime', 'black'])
# Plot Sepal
plt.subplot(1, 2, 1)
plt.scatter(X.Sepal_Length,X.Sepal_Width, c=colormap[y.Targets], s=40)
plt.title('Sepal')
plt.subplot(1, 2, 2)
plt.scatter(X.Petal_Length,X.Petal_Width, c=colormap[y.Targets], s=40)
plt.title('Petal')
plt.figure(figsize=(16,7))
# Plot the Original Classifications
plt.subplot(1, 3, 1)
plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[y.Targets], s=40)
plt.title('Real Classification')
plt.subplot(1, 3, 3)
plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[y_cluster_gmm], s=40)
plt.title('GMM Classification')
# K Means Accuracy
print('K Means Accuracy')
print('Accuracy', sm.accuracy_score(y, y_pred) )
print('Confusion Matrix', sm.confusion_matrix(y, y_pred) )
https://colab.research.google.com/drive/1skhcjCHuObahJ_ZwztzAws7ytFltLMQv#scrollTo=hqRNfR7CBd3K&printMode=true 4/9
1/8/24, 6:50 AM KNN.ipynb - Colaboratory
# GMM Accuracy
print('GMM Accuracy')
print('Accuracy', sm.accuracy_score(y, y_cluster_gmm) )
print('Confusion Matrix', sm.confusion_matrix(y, y_cluster_gmm))
# actual Accuracy
print('actual accuracy')
print('accuracy', sm.accuracy_score(y,y.Targets))
print('confusion matrix', sm.confusion_matrix(y,y.Targets))
https://colab.research.google.com/drive/1skhcjCHuObahJ_ZwztzAws7ytFltLMQv#scrollTo=hqRNfR7CBd3K&printMode=true 5/9
1/8/24, 6:50 AM KNN.ipynb - Colaboratory
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 2 2 2 1 2 2 2 2
2 2 1 1 2 2 2 2 1 2 1 2 1 2 2 1 1 2 2 2 2 2 1 2 2 2 2 1 2 2 2 1 2 2 2 1 2
2 1]
K Means Accuracy
Accuracy 0.8933333333333333
Confusion Matrix [[50 0 0]
[ 0 48 2]
[ 0 14 36]]
GMM Accuracy
Accuracy 0.9666666666666667
Confusion Matrix [[50 0 0]
[ 0 45 5]
[ 0 0 50]]
actual accuracy
accuracy 1.0
confusion matrix [[50 0 0]
[ 0 50 0]
[ 0 0 50]]
https://colab.research.google.com/drive/1skhcjCHuObahJ_ZwztzAws7ytFltLMQv#scrollTo=hqRNfR7CBd3K&printMode=true 6/9
1/8/24, 6:50 AM KNN.ipynb - Colaboratory
ANN 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 der_sigmoid(x):
return x*(1-x)
epoch=50000
lr=0.1
inputneu=2
hiddeneu=3
outputneu=1
wh=np.random.uniform(size=(inputneu,hiddeneu))
bh=np.random.uniform(size=(1,hiddeneu))
wout=np.random.uniform(size=(hiddeneu,outputneu))
bout=np.random.uniform(size=(1,outputneu))
for i in range(epoch):
hinp=np.dot(x,wh)+bh
hlayer=sigmoid(hinp)
outinp=np.dot(hlayer,wout)+bout
output=sigmoid(outinp)
outgrad=der_sigmoid(output)
hiddengrad=der_sigmoid(hlayer)
eo=y-output
doutput=eo*outgrad
eh=doutput.dot(wout.T)
dhidden=eh*hiddengrad
wout+=hlayer.T.dot(doutput)*lr
wh+=x.T.dot(dhidden)*lr
print("input:\n"+str(x))
print("acuat output"+str(y))
print("predicted output ",output)
input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
acuat output[[0.92]
[0.86]
[0.89]]
predicted output [[0.89939908]
[0.87413928]
[0.89547036]]
import csv
with open("enjoySport.csv") as f:
https://colab.research.google.com/drive/1skhcjCHuObahJ_ZwztzAws7ytFltLMQv#scrollTo=hqRNfR7CBd3K&printMode=true 7/9
1/8/24, 6:50 AM KNN.ipynb - Colaboratory
csv_file = csv.reader(f)
data = list(csv_file)
specific = data[1][:-1]
general=[['?' for i in range(len(specific))]for j in range(len(specific))]
print(specific)
print(general)
for i in data:
if i[-1] == "Y":
for j in range(len(specific)):
if i[j] != specific[j]:
specific[j] = "?"
general[j][j] = "?"
gh = [] # gh = general Hypothesis
for i in general:
for j in i:
if j != '?':
gh.append(i)
break
print("\nFinal Specific hypothesis:\n", specific)
print("\nFinal General hypothesis:\n", gh)
A* Algorithm
def astar(start,end):
open=set(start)
close=set()
dis={}
par={}
dis[start]=0
par[start]=start
while len(open)>0:
n=None
for v in open:
if n==None or dis[v]+heu(v)<dis[n]+heu(n):
n=v
if n==end or graph_nodes[n]==None:
pass
else:
for (m,w) in getneigh(n):
if m not in open and m not in close:
https://colab.research.google.com/drive/1skhcjCHuObahJ_ZwztzAws7ytFltLMQv#scrollTo=hqRNfR7CBd3K&printMode=true 8/9
1/8/24, 6:50 AM KNN.ipynb - Colaboratory
p
open.add(m)
par[m]=n
dis[m]=dis[n]+w
else:
if dis[m]>dis[n]+w:
dis[m]=dis[n]+w
par[m]=n
if m in close:
close.remove(m)
open.add(m)
if n==None:
print('path does not exit')
return None
if n==end:
path=[]
while par[n]!=n:
path.append(n)
n=par[n]
path.append(start)
path.reverse()
print('path found '.format(path))
return path
open.remove(n)
close.add(n)
def getneigh(v):
if v in graph_nodes:
return graph_nodes[v]
else:
return None
def heu(n):
H={
'A':10,
'B':8,
'C':5,
'D':8,
'E':3,
'F':6,
'G':5,
'H':3,
'I':1,
'J':0,
}
return H[n]
graph_nodes={
'A':[('B',6),('F',3)],
'B':[('C',3),('D',2)],
'C':[('E',5),('D',1)],
'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)],
}
astar('A','J')
path found
['A', 'F', 'G', 'I', 'J']
ID3 Algorithm
#calculate entropy
def entropy(ls):
counts = Counter(x for x in ls)
total = len(ls)
probs= [x/total for x in counts.values()]
E=sum(-p*math.log(p,2) for p in probs)
return E
https://colab.research.google.com/drive/1skhcjCHuObahJ_ZwztzAws7ytFltLMQv#scrollTo=hqRNfR7CBd3K&printMode=true 9/9