标签:
下面贴出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
标签:
原文地址:http://www.cnblogs.com/xzh0001/p/5664174.html