Skip to content

Instantly share code, notes, and snippets.

@tinylamb
Created September 11, 2013 06:59
Show Gist options
  • Select an option

  • Save tinylamb/6520155 to your computer and use it in GitHub Desktop.

Select an option

Save tinylamb/6520155 to your computer and use it in GitHub Desktop.
KNN algorithm
#coding=utf-8
#KNN algorithm
#describe:given a Classification Class={ci|i=1...n} and ci={sample(j)|j=1...n}
#input test_sample
#output which class the test_sample in/ie.test_sample ∈ C?
import random
def dis(p1,p2):
f=lambda (x,y):(x-y)**2
return sum(map(f,zip(p1,p2)))
def classify():
Class={}
x=[random.randint(0,100) for i in range(0,40)]
y=[random.randint(0,100) for i in range(0,40)]
sample=zip(x,y) #40 samples
for i in range(4):# 4 class
Class[i]=sample[10*i:10*(i+1)]
return Class
if __name__=='__main__':
#...Initial classification
Class={}
Class=classify()
sample=[]
for key in Class:
sample.extend(Class[key])
test_sample=(50,50)
#...compute knn k=10
k=10
distance={}
for s in sample:
distance[s]=dis(s,test_sample)
distance=sorted(distance.items(),key=lambda key:key[1])[:k]
#...vote
v=[0]*4
for d in distance:
if d[0] in Class[0]:
v[0]+=1
elif d[0] in Class[1]:
v[1]+=1
elif d[0] in Class[2]:
v[2]+=1
elif d[0] in Class[3]:
v[3]+=1
print 'test sample belongs to class %d'%v.index(max(v))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment