首页 > 代码库 > j简单的分类实现-K近邻
j简单的分类实现-K近邻
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))
j简单的分类实现-K近邻
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。