from numpy import *
import operator
def createDataSet():
group=array([
[1.0,1.1],
[1.0,1.0],
[0,0],
[0,0.1],
[1,0],
[1.1,0]
])
labels=['A','A','B','B','C','C']
return group,labels
def classify0(inX,dataSet,labels,k):
dataSetSize=dataSet.shape[0]
diffMat=tile(inX,(dataSetSize,1))-dataSet
print(diffMat)
sqDiffMat=diffMat**2;
print(sqDiffMat)
sqDistances=sqDiffMat.sum(axis=1)
print(sqDistances)
distances=sqDistances**0.5
print(distances)
sortedDistIndicies=distances.argsort()
print(sortedDistIndicies)
clasCount={}
for i in range(k):
print(clasCount)
voteIlabel=labels[sortedDistIndicies[i]]
print(voteIlabel)
print(clasCount.get(voteIlabel,0))
clasCount[voteIlabel]=clasCount.get(voteIlabel,0)+1
print(clasCount)
sortedClassCount=sorted(clasCount.iteritems(),
key=operator.itemgetter(1),reverse=True)
print(sortedClassCount)
return sortedClassCount[0][0]
group,labels=createDataSet()
print(group)
print(group.shape)
print(group.shape[0])
print(classify0([1,0],group,labels,3))