# -*- coding: utf-8 -*- """ Created on Wed Jun 18 11:46:15 2014 @author: hp """ import numpy as np import operator def createDataSet(): group=np.random.rand(4,2) labels=[‘a‘,‘b‘,‘c‘,‘d‘] return group,labels def classify0(inX,dataSet,labels,k): dataSetSize=dataSet.shape[0] diffMat=np.tile(inX,(dataSetSize,1))-dataSet sqDiffMat=diffMat**2 sqDistances=sqDiffMat.sum(axis=1) distances=sqDistances**0.5 sortedDistIndicies=distances.argsort() classCount={} for i in range(k): voteLabel=labels[sortedDistIndicies[i]] classCount[voteLabel]=classCount.get(voteLabel,0)+1 sortedClassCount=sorted(classCount.iteritems(),key=operator.itemgetter(1),reverse=True) return sortedClassCount[0][0] group,labels=createDataSet() result=classify0([0,1],group,labels,3) print ‘对坐标0,0的分类为%s‘%(result)
[MachineLearning]KNN,布布扣,bubuko.com
原文:http://www.cnblogs.com/colipso/p/3794724.html