机器学习实战
from numpy import *
import operator
def createDatSet():
group=array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])
labels=['A','A','B','B']
return group,labels
def classfy0(inX,dataSet,labels,k):
dataSetSize=dataSet.shape[0] #dataset.shape[0]返回的是dataset这个array的行数。
diffMat=tile(inX,(dataSetSize,1))-dataSet #tile(A,B),把A重复B次
sqDiffMat=diffMat**2
sqDistance=sqDiffMat.sum(axis=1)
distances=sqDistance**0.5
sortedDistIndicies=sqDistance.argsort()
classCount={}
for i in range(k):
voteIlabel=labels[sortedDistIndicies[i]]
classCount[voteIlabel]=classCount.get(voteIlabel,0)+1
sortedClassCount=sorted(classCount.items(),key=operator.itemgetter(1),reverse=True)
return sortedClassCount[0][0]
def main():
group,labels=createDatSet()
sortedClassCount=classfy0([0,0],group,labels,3)
print(sortedClassCount)
main()
运行结果为:B