码迷,mamicode.com
首页 > 编程语言 > 详细

K近邻算法及其Python实现

时间:2016-07-12 19:31:20      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

技术分享

技术分享

技术分享

下面贴出Python代码

knnClassify.py

 1 from numpy import *
 2 import operator
 3 
 4 def creatDataSet():
 5     group = array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])
 6     labels = [A,A,B,B]
 7     return group, labels
 8 
 9 def classify(inX,dataSet,labels,k):
10     numSamples = dataSet.shape[0]
11     diffMat = tile(inX,(numSamples,1)) - dataSet
12     sqDiffMat = diffMat**2
13     sqDistances = sqDiffMat.sum(axis = 1)
14     distances = sqDistances ** 0.5
15     sortedDistIndicies = distances.argsort()
16     classCount = {}
17     for i in xrange(k):
18         voteILabel = labels[sortedDistIndicies[i]]
19         classCount[voteILabel] = classCount.get(voteILabel,0) + 1
20     maxLabel = sorted(classCount.iteritems(),
21                       key = operator.itemgetter(1),reverse = True)
22     return maxLabel[0][0]
23 
24 if __name__=="__main__":
25     g,l = creatDataSet()
26     labelOfinX = classify([1.0,1.2],g,l,1)
27     print labelOfinX

 

K近邻算法及其Python实现

标签:

原文地址:http://www.cnblogs.com/xzh0001/p/5664174.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!