forked from shivaylamba/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added implementation of KNN algorithm
Added implementation of KNN algorithm from Scratch.
- Loading branch information
1 parent
4766c02
commit 8dc9158
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
import math | ||
f=[[10,5],[40,7],[3,2],[5,3]] | ||
l=['good','good','bad','bad'] | ||
x=[] | ||
y=[] | ||
s=len(set(l)) #shows all categories present | ||
for i in range(len(f)): | ||
f1=f[i][0] | ||
x=np.append(x,f1) | ||
f2=f[i][1] | ||
y=np.append(y,f2) | ||
print(x," ",y) | ||
for i in range(len(f)): | ||
if (l[i]== 'good'): | ||
plt.plot(x[i],y[i],'r*') | ||
else: | ||
plt.plot(x[i],y[i],"y^") | ||
p = int(input("Enter saving%")) | ||
q = int(input("Enter no. of good habit")) | ||
k=int(input("Enter k")) | ||
plt.plot(p,q,'b*') | ||
disx=[] | ||
disy=[] | ||
dis=[] | ||
if (k>len(f)): | ||
k=len(f) | ||
for i in range(len(x)): | ||
dis=np.append(dis,math.sqrt(((p-x[i])**2)+((q-y[i])**2))) | ||
print(dis) | ||
dis.sort() | ||
min1=[] | ||
for i in range(k): | ||
min1=np.append(min1,dis[i]) | ||
print(min1) | ||
sum1=0 | ||
for i in range(len(min1)): | ||
sum1=sum1+min1[i] | ||
print(sum1/len(min1)) #avgmin = sum1/len(min1) | ||
plt.show() |