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