机器学习 k-临近算法
Posted Chaz_Sun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了机器学习 k-临近算法相关的知识,希望对你有一定的参考价值。
程序清单一:
from numpy import * import operator def creatDataSet(): group = array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]]) labels = ["A","A","B","B"] return group,labels def classify0(inX,dataSet,labels,k): dataSetSize = dataSet.shape[0] diffMat = tile(inX,(dataSetSize,1)) - dataSet sqDiffMat = diffMat**2 sqDistances = sqDiffMat.sum(axis = 1) distances = sqDistances **0.5 sortedDistIndicis = distances.argsort() print(sortedDistIndicis) classCount = {} for i in range(k): voteIlabel = labels[sortedDistIndicis[i]] print(voteIlabel) classCount[voteIlabel] = classCount.get(voteIlabel,0)+1 print(classCount) print(classCount) sortedClassCount = sorted(classCount.iteritems(),key = operator.itemgetter(1),reverse = True) print(sortedClassCount) print(sortedClassCount[0][0]) return sortedClassCount[0][0] group,labels = creatDataSet() classify0([0,0],group,labels,3)
源代码中print是为了便于理解代码。
numpy 库中 :tile函数用法 :http://www.cnblogs.com/zibu1234/p/4210521.html
sorted:http://blog.163.com/zhuandi_h/blog/static/1802702882012111284632184/
python iteritems(),itemgetter(),sorted():http://blog.csdn.net/u013713637/article/details/39521187
以上是关于机器学习 k-临近算法的主要内容,如果未能解决你的问题,请参考以下文章
机器学习-临近取样(K-Nearest Nerghbor)KNN算法