Demonstration of Knn algorithm
import numpy as np
import matplotlib.pyplot as plt
s=np.random.uniform(100,200,150)
t=np.random.uniform(150,250,150)
s=list(s)
t=list(t)
plt.plot(s,'o',color='#ffff00')
plt.plot(t,'o',color='silver')
dic={}
dic['A']=s
dic['B']=t
distance=[]
td=int(input('Enter the test data:'))
plt.plot(td,'o',color='aqua')
for key in dic:
for i in range(120):
t=str(abs(dic[key][i]-td))+key
distance.append(t)
distance.sort()
k=int(input('Enter the key value:'))
s=t=0
for i in range(k):
if distance[i][len(distance[i])-1]=='A':
s+=1
else:
t+=1
if s>t:
print("Data belongs to class A")
else:
print("Data belongs to class B")

Comments
Post a Comment