1 __author__ = 'worfzyq'
2 from numpy import *
3 import operator
4
5 def creatDataset() :
6 group=array([[1.0,1.1],[1.0,1.0],[0.0,0.0],[0,0.1]])
7 labels=['A','A','B','B']
8 return group,labels
9
10 def classify0(inX,dataSet,labels,k) :
11 dataSetSize=dataSet.shape[0] #返回数组得到维数
12 # print(dataSetSize)
13 diffMat=tile(inX,(dataSetSize,1))-dataSet #把inX扩展datasetSize-1维然后做减法
14 #print(diffMat)
15 sqDiffMat=diffMat**2 #乘方
16 #print(sqDiffMat)
17 sqDistances=sqDiffMat.sum(axis=1) #把每一列加起来
18 #print(sqDistances)
19 distances=sqDistances**0.5 # 得到距离
20 sortedDistIndicies=distances.argsort() #得到排好序得到下标
21 classCount={}
22 for i in range(k) :
23 voteIlabel=labels[sortedDistIndicies[i]]
24 classCount[voteIlabel]=classCount.get(voteIlabel,0)+1
25 sortedClassCount=sorted(classCount.iteritems(),key=operator.itemgetter(1),reverse=True)
26 #print(sortedClassCount)
27 return sortedClassCount[0][0]
28
29 group,labels=creatDataset()
30 # print(group)
31 print(classify0([0,0],group,labels,3))
2 from numpy import *
3 import operator
4
5 def creatDataset() :
6 group=array([[1.0,1.1],[1.0,1.0],[0.0,0.0],[0,0.1]])
7 labels=['A','A','B','B']
8 return group,labels
9
10 def classify0(inX,dataSet,labels,k) :
11 dataSetSize=dataSet.shape[0] #返回数组得到维数
12 # print(dataSetSize)
13 diffMat=tile(inX,(dataSetSize,1))-dataSet #把inX扩展datasetSize-1维然后做减法
14 #print(diffMat)
15 sqDiffMat=diffMat**2 #乘方
16 #print(sqDiffMat)
17 sqDistances=sqDiffMat.sum(axis=1) #把每一列加起来
18 #print(sqDistances)
19 distances=sqDistances**0.5 # 得到距离
20 sortedDistIndicies=distances.argsort() #得到排好序得到下标
21 classCount={}
22 for i in range(k) :
23 voteIlabel=labels[sortedDistIndicies[i]]
24 classCount[voteIlabel]=classCount.get(voteIlabel,0)+1
25 sortedClassCount=sorted(classCount.iteritems(),key=operator.itemgetter(1),reverse=True)
26 #print(sortedClassCount)
27 return sortedClassCount[0][0]
28
29 group,labels=creatDataset()
30 # print(group)
31 print(classify0([0,0],group,labels,3))