0% found this document useful (0 votes)
21 views9 pages

Lab

Uploaded by

vamsidayyala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views9 pages

Lab

Uploaded by

vamsidayyala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1/8/24, 6:50 AM KNN.

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

#split the dataset into train and test data


from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split (X, y, test_size=0.20, random_state =23)
print(iris.data.shape)
print('Training Set', len(X_train))
print('Test Set', len(X_test))

#to train the model


from sklearn.neighbors import KNeighborsClassifier
knn=KNeighborsClassifier(n_neighbors=3)
# Try with n_neighbors=60, n_neighbors=5
# Check if it is overfitting or underfitting the model.
knn.fit(X_train,y_train)

#make prediction on test data, Classify


y_pred=knn.predict(X_test)
print(y_pred)

#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

0 1.00 1.00 1.00 12


1 0.89 1.00 0.94 8
2 1.00 0.90 0.95 10

accuracy 0.97 30
macro avg 0.96 0.97 0.96 30
weighted avg 0.97 0.97 0.97 30

ID Actual Predicted Correct?


1 virginica virginica yes
2 virginica virginica yes
3 versicolor versicolor yes
4 setosa setosa yes
5 virginica virginica yes
6 versicolor versicolor yes
7 setosa setosa yes
8 virginica virginica yes
9 setosa setosa yes

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")

#Initializing noisy non linear data


x = np.linspace(0,1,100)
noise = np.random.normal(loc = 0, scale = .25, size = 100)
y = np.sin(x * 1.5 * np.pi )
y_noise = y + noise

#Plotting the noisy data and the kernell at around x = 0.2


plt.figure(figsize=(10,6))
plt.plot(x,y,color = 'darkblue', label = 'f(x)')
plt.scatter(x,y_noise,facecolors='none', edgecolor = 'darkblue',label = 'f(x) + noise')
plt.legend()
plt.title('Noisy sine function and bell shaped kernell at x=.2')
plt.show()

from math import ceil


import numpy as np
from scipy import linalg

def kernel_function(xi,x0,tau= .005):


return np.exp( - (xi - x0)**2/(2*tau**2))

def lwr(x,y, xtest,tau = .005):

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

<ipython-input-2-a2578210b2f9>:5: MatplotlibDeprecationWarning: The seaborn styles s


plt.style.use("seaborn")

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

# import some data to play with


iris = datasets.load_iris()

#print("\n IRIS DATA :",iris.data);


print("\n IRIS FEATURES :\n",iris.feature_names)
print("\n IRIS TARGET :\n",iris.target)
print("\n IRIS TARGET NAMES:\n",iris.target_names)

# Store the input


# This is what KMeans thought model.labels_ as a Pandas Dataframe and set the column names
X = pd.DataFrame(iris.data)
X.columns = ['Sepal_Length','Sepal_Width','Petal_Length','Petal_Width']
y = pd.DataFrame(iris.target)
y.columns = ['Targets']

# Set the size of the plot


plt.figure(figsize=(14,7))

# 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')

# Build model K Means Cluster


model = KMeans(n_clusters=3, random_state=100)
model.fit(X)
y_pred = model.labels_
print(y_pred)

# Plot the Original Classifications


plt.subplot(1, 2, 1)
plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[y.Targets], s=40)
plt.title('Real Classification')

# Plot the Models Classifications


plt.subplot(1, 2, 2)
plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[y_pred], s=40)
plt.title('K Mean Classification')

from sklearn.mixture import GaussianMixture


gmm = GaussianMixture(n_components=3, covariance_type='full', random_state=100)
# covariance_type =spherical
gmm.fit(X)
y_cluster_gmm = gmm.predict(X)
y_cluster_gmm

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')

# Plot the Models Classifications


plt.subplot(1, 3, 2)
plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[y_pred], s=40)
plt.title('K Mean 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]]

Candidate Elimination Method

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] = "?"

elif i[-1] == "N":


for j in range(len(specific)):
if i[j] != specific[j]:
general[j][j] = specific[j]
else:
general[j][j] = "?"

print("\nStep " + str(data.index(i)+1) + " of Candidate Elimination Algorithm")


print(specific)
print(general)

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)

['Sunny', 'Warm', 'Normal', 'Strong', 'Warm', 'Same ']


[['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['

Step 1 of Candidate Elimination Algorithm


['Sunny', 'Warm', 'Normal', 'Strong', 'Warm', 'Same ']
[['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['

Step 2 of Candidate Elimination Algorithm


['Sunny', 'Warm', 'Normal', 'Strong', 'Warm', 'Same ']
[['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['

Step 3 of Candidate Elimination Algorithm


['Sunny', 'Warm', '?', 'Strong', 'Warm', 'Same ']
[['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['

Step 4 of Candidate Elimination Algorithm


['Sunny', 'Warm', '?', 'Strong', 'Warm', 'Same ']
[['Sunny', '?', '?', '?', '?', '?'], ['?', 'Warm', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '

Step 5 of Candidate Elimination Algorithm


['Sunny', 'Warm', '?', 'Strong', '?', '?']
[['Sunny', '?', '?', '?', '?', '?'], ['?', 'Warm', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '

Final Specific hypothesis:


['Sunny', 'Warm', '?', 'Strong', '?', '?']

Final General hypothesis:


[['Sunny', '?', '?', '?', '?', '?'], ['?', 'Warm', '?', '?', '?', '?']]

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

from collections import Counter


import math
import pandas as pd
df = pd.read_csv('Play.csv')
n=len(df) # number of training examples
df

#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

def inf_gain(df, a,target):

https://colab.research.google.com/drive/1skhcjCHuObahJ_ZwztzAws7ytFltLMQv#scrollTo=hqRNfR7CBd3K&printMode=true 9/9

You might also like